in Service Manager

How to add a button to edit ListView item on a custom SCSM form

I recently had to work with ListViews on custom forms to represent related CIs. While working on this, I was asked if it would be possible to edit objects in this ListView directly from the form by just selecting the object and then clicking on a button to open the form for this object. The result should look something like this.

EntityForm

A click on the Edit button should open the form for the selected object

Entity_IdentityForm

I will first demonstrate how to create a ListView in your forms XAML code (see http://msdn.microsoft.com/en-us/library/ms750972.aspx for more information about the ListView Class) and afterwards show you what Code-Behind you have to add to your form to get this working.

To add a ListView to your form, no special references are needed. Just drag and drop the ListView element from your Visual Studio Toolbox to your custom form. Beside the common attributes like Name, you then have to define a View Mode for your ListView and a Binding to the TypeProjection defined for the relationship between your classes. As a View Mode you will probably use a GridView like in my example below.

The ItemsSource is bound to the name of the Alias for your TypeProjection. Furthermore the GridViewColumn binds to a property of the related CI.

This should do it for the ListView. Now the form yet lacks of a button to edit the selected ListView item. Add the following XAML code to your form to place a button beside your ListView. Notice the Click attribute which will reference to the button event handler we will create later on.

Your form should now basically look like this:

ListView with Edit button

Now we have to give life to that button 🙂

First you have to add some references to your Visual Studio project:

  • Microsoft.EnterpriseManagement.ServiceManager.Application.Common.dll (Contains ConsoleContextHelper)
  • Microsoft.EnterpriseManagement.UI.SdkDataAccess.dll (Contains BindableDataItems)
  • Microsoft.EnterpriseManagement.UI.Foundation.dll (Contains IDataItem)

Open your forms .cs file in Visual Studio and add the following namespaces:

Now create a button handler together with the corresponding method, which contains the action. Notice that the button event handler passes your ListView object to the method.

Build your form and deploy it to Service Manager to see if it works.

Hope this helps!

Write a Comment

Comment

  1. Hi! I try to create custom form and add ListView to it, If I bound List View to TypeProjection component which use existed relationship – List View work fine. But if I bound List View to TypeProjection which use my custom relationship – List View is empty.

    • Hi Alex

      Does your relationship actually work and does it link two objects? Your relationship has to link two objects so that something is shown in your ListView. For example if you want to display all accounts linked to a user in your ListView, these account actually have to be linked to the user before they are showing up in your ListView. No accounts linked to the user, nothing shows up in your list.
      Also check that the binding of your GridColumns is correct for the objects you want to display in your ListView.

      Cheers,
      Stefan

  2. I can’t post my XML management pack here. Relationship work fine, and I can see related object using PowerShell or SCSM View. And I can see related object property in TextBox. But ListView for my custom TypeProjection is empty. If I create ListView for TypeProjection which use existing relationship (for example Component Path=”$Target/Path[Relationship=’System!System.ConfigItemRelatesToConfigItem) – ListView work fine.

    • Hi Alex,

      Sorry for the late reply. Beneath you find a sample TypeProjection together with the corresponding relationship I use for a ListView. Maybe this helps.

      [code language=”xml”]
      <TypeProjection ID="itnetxclasscostcenter.typeprojection" Accessibility="Public" Type="itnetxclasscostcenter">
      <Component Path="$Context/Path[Relationship=’itnetxclasscostcentercompany’]$" Alias="CostCenterBelongsToCompany" />
      </TypeProjection>
      [/code]
      [code language=”xml”]
      <RelationshipType ID="itnetxclasscostcentercompany" Accessibility="Public" Abstract="false" Base="System!System.Reference">
      <Source ID="Source_69d6bf06_7579_45d5_b447_42520fcfd204" MinCardinality="0" MaxCardinality="2147483647" Type="itnetxclasscostcenter" />
      <Target ID="Target_0ef33fe0_d279_4aa0_b4f7_4a3873ed62b6" MinCardinality="0" MaxCardinality="1" Type="itnetxclasscompany" />
      </RelationshipType>
      [/code]

      Cheers,
      Stefan

  3. What if I need to modify OOB incident form. As there is no “list view” object in Authoring tool and If I use the .mp file in visual studio it only shows the xml file rather than design.

  4. Hi Stefan,

    Can I please request for your help with the listview not keeping it’s values after being created. I’ve got 2 basic ‘System!System.ConfigItem’ class types with the following relationship and type projections. It’s meant to be a 1 to many relationship between the UserClass and ApplicationClass.

    I have an associated ListView to display the items in the Type Projection. I can update values in the list and the data saves values in the ApplicationClass but the list contains no data when I open the item again. My listview XAML code is given below:

    If you can please have a look and let me know what I need to do to save the items in the listview to project correctly when the item is opened up again.

    My c# code associated with the add button is:

    Collection items = lv1.ItemsSource as Collection;
    lv1.Items.Add(new ApplicationClass { UserID = tboxUserID.Text, Client = this.lstpclient.SelectedItem.ToString(), System = this.lstpsystem.SelectedItem.ToString(), Access = this.lstpAccess.SelectedItem.ToString() });

    Thanks

    Sue.