Share via

Service Manager custom management pack - error when adding related config items

RobertHogan 1 Reputation point
2022-02-20T05:50:36.247+00:00

I am creating a custom management pack for Service Manager and want to add related items between the custom config items. I am getting a null reference exception and not sure where I have gone wrong, any input would be appreciated. The error is:

Application: Microsoft.EnterpriseManagement.ServiceManager.UI.Console.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.NullReferenceException
at APOSystemAccessForms.Location.AddItemToListView(System.Windows.Controls.ListView, System.Guid)
at APOSystemAccessForms.Location.btnAddBusinessUnit_Click(System.Object, System.Windows.RoutedEventArgs)

I have debugged the code, the error is occurring at line 23 or maybe 24 and the debugger lists the listView.ItemSource as null.

internal static void AddItemToListView(ListView listView, Guid classId)
{
    if (listView != null && listView.Items != null)
    {
        /* NOTE: The use of the IDataItem and InstancePickerDialog interfaces here is not supported/documented.
         * This interface may change in the future and no migration path is guaranteed by Microsoft.
        */
        InstancePickerDialog ciPicker = new InstancePickerDialog();
        ciPicker.ClassId = classId;
        ciPicker.SelectionMode = SelectionMode.Multiple;

        if (listView.Items.Count > 0)
        {
            ciPicker.SetPickedInstances((Collection<IDataItem>)listView.ItemsSource);
        }

        bool? result = ciPicker.ShowDialog();
        if (result != null && result == true)
        {
            Collection<IDataItem> items = listView.ItemsSource as Collection<IDataItem>;
            foreach (IDataItem item in ciPicker.RemovedInstances)
                items.Remove(item);
            foreach (IDataItem item in ciPicker.PickedInstances)
                if (!items.Contains(item))
                    items.Add(item);
        }
    }

}

Classes:

<ClassType ID="APO.Class.BusinessUnit" Base="System!System.ConfigItem" Accessibility="Public" Abstract="false" Hosted="false" Singleton="false" Extension="false">
    <Property ID="BusinessUnitId" Key="true" AutoIncrement="true" Type="string" Required="true" DefaultValue="{0}" />
    <Property ID="BusinessUnitMnemonic" Type="string" Required="true" />
</ClassType>
<ClassType ID="APO.Class.Location" Base="System!System.ConfigItem" Accessibility="Public" Abstract="false" Hosted="false" Singleton="false" Extension="false">
    <Property ID="LocationId" Key="true" AutoIncrement="true" Type="string" Required="true" DefaultValue="{0}" />
    <Property ID="LocationMnemonic" Type="string" Required="true" />
</ClassType>

Relationship:

<RelationshipType ID="APO.Relationship.LocationToBusinessUnit" Base="System!System.Reference" Accessibility="Public" Abstract="false">
    <Source ID="APO.Source.LocationClass" Type="APO.Class.Location" MinCardinality="0" MaxCardinality="2147483647"/>
    <Target ID="APO.Target.BusinessUnitClass" Type="APO.Class.BusinessUnit" MinCardinality="0" MaxCardinality="2147483647"/>
</RelationshipType>

Type projection:

<TypeProjection ID="APO.TypeProjection.LocationClass" Accessibility="Public" Type="APO.Class.Location">
    <Component Path="$Target/Path[Relationship='APO.Relationship.LocationToBusinessUnit']$" Alias="RelatesToBusinessUnit" />
</TypeProjection>

Form reference:

<Form ID="APO.Form.Location" Accessibility="Public" Target="APO.TypeProjection.LocationClass" Assembly="Forms" TypeName="Forms.Location">

ListView:

<ListView Name="lvBusinessUnit" Height="150" ItemsSource="{Binding Path=RelatesToBusinessUnit, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
System Center Service Manager
0 comments No comments

1 answer

Sort by: Most helpful
  1. RobertHogan 1 Reputation point
    2022-02-22T09:24:57.38+00:00

    I found the issue with the help of https://social.technet.microsoft.com/Forums/en-US/4db386c3-6347-496a-819f-e7d13465f398/list-views-on-custom-forms-are-empty?forum=customization, the type projection component path was incorrect:

    <Component Path="$Context/Path[Relationship='APO.Relationship.LocationToBusinessUnit' SeedRole='Source']$" Alias="RelatesToBusinessUnit" />
    

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.