0

I am quite new here and, therefore, my question might be not as clarified as you are used to. Nevertheless, I hope you will be able to give me a hint for the solution of my issue. Currently I am developing a custom change request form for System Center Service Manager 2012 R2 wit UR 7 using Visual Studio 2013 with the Authoring Extensions. This form is based on a WPF User Control (Visual C#) with .Net-FrameWork 3.5. My aim is to develop a listview where I want to add with an add-button more than one file attachment to this view. The Code Looks like this:

The type projection within the panagement pack:

<TypeProjection ID="TypeProjection.RFCMinor" Accessibility="Public" Type="RFC.Minor.RFCMinor">
      <Component Path="$Target/Path[Relationship='WorkItem!System.WorkItemHasFileAttachment']$" Alias="FileAttachments">
        <Component Path="$Target/Path[Relationship='SupportingItem!System.FileAttachmentAddedByUser']$" Alias="FileAttachmentAddedBy" />
      </Component>
    </TypeProjection>

The list view item within the xaml-Code:

<ListView x:Name="lvAttachedRiskRelFiles" MinHeight="70" ItemsSource="{Binding Path=FileAttachments, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                                    MouseDoubleClick="lvAttachedRiskRelFiles_MouseDoubleClick">
                            <ListView.View>
                                <GridView>
                                    <scwpf:SortableGridViewColumn Header="File Name" DisplayMemberBinding="{Binding FileName}" Width="Auto"/>
                                    <scwpf:SortableGridViewColumn Header="Attached By" DisplayMemberBinding="{Binding AttachedBy}" Width="Auto"/>
                                    <scwpf:SortableGridViewColumn Header="File Size (KB)" DisplayMemberBinding="{Binding FileSize}" Width="Auto"/>
                                    <scwpf:SortableGridViewColumn Header="Attached Date" DisplayMemberBinding="{Binding AttachedDate}" Width="Auto"/>
                                </GridView>
                            </ListView.View>
                            <Button Content="Add" Name="btnAddRiskFile" Click="btnAddRiskFile_Click"/>

The C#-Code looks currently like:

private void btnAddRiskFile_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        // Create OpenFileDialog 
        Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
        Nullable<bool> result = dlg.ShowDialog();
        // Get the selected file name 
        if (result == true)
        {
            // Open document 
            string filename = dlg.FileName;
            AddFileToListView(filename);
        }
    }
internal void AddFileToListView(String sFile)
    {
        //emg: current EnterpriseManagementGroup

        //System.SupportingItem.Library; ManagementPack ID: 23e3ae8e-1981-8560-2e55-8730cbc04965
        ManagementPack mpSupporting =
           emg.ManagementPacks.GetManagementPack(new Guid("23e3ae8e-1981-8560-2e55-8730cbc04965"));

        //Get the System.FileAttachment class
        ManagementPackClass mpcAttachment = emg.EntityTypes.GetClass("System.FileAttachment", mpSupporting);

        //Get attachment details
        string sExt = Path.GetExtension(sFile);
        string sAttachmentName = Path.GetFileNameWithoutExtension(sFile);

        //Create new stream and read file into memory
        MemoryStream ms = new MemoryStream();

        using (FileStream fs = File.OpenRead(sFile))
        {
            ms.SetLength(fs.Length);
            fs.Read(ms.GetBuffer(), 0, (int)fs.Length);
        }

        //Did we get any data?
        if (ms != null && ms.Length != 0)
        {
            //Reset stream position
            ms.Position = 0;

            //Create a new attachment
            CreatableEnterpriseManagementObject cemoAttachment = new CreatableEnterpriseManagementObject(emg, mpcAttachment);

            //Create a new attachment relationship from SupportingItem -> File Attachment (via "HasFileAttachment: ID =aa8c26dc-3a12-5f88-d9c7-753e5a8a55b4)
            //DisplayName : Has File Attachment
            //Source      : System.WorkItem
            //Target      : System.FileAttachment

            ManagementPackRelationship relAttachment =
            emg.EntityTypes.GetRelationshipClass(new Guid("aa8c26dc-3a12-5f88-d9c7-753e5a8a55b4"));
            CreatableEnterpriseManagementRelationshipObject cemroAttachment =
                new CreatableEnterpriseManagementRelationshipObject(emg, relAttachment);

            //Create a new added by user relationship
            // ID: ffd71f9e-7346-d12b-85d6-7c39f507b7bb
            // DisplayName : Added By User
            //Source      : System.FileAttachment
            //Target      : System.User

            ManagementPackRelationship relAddedByUser =
            emg.EntityTypes.GetRelationshipClass(new Guid("ffd71f9e-7346-d12b-85d6-7c39f507b7bb"));
            CreatableEnterpriseManagementRelationshipObject cemroAddedByUser =
                new CreatableEnterpriseManagementRelationshipObject(emg, relAddedByUser);

            //Set properties of attachment
            string sFileName = sAttachmentName;
            if (sExt != "") sFileName += "." + sExt;
            cemoAttachment[mpcAttachment, "AddedDate"].Value = DateTime.UtcNow;
            cemoAttachment[mpcAttachment, "DisplayName"].Value = sFileName;
            cemoAttachment[mpcAttachment, "Extension"].Value = sExt;
            cemoAttachment[mpcAttachment, "Content"].Value = ms;
            cemoAttachment[mpcAttachment, "Size"].Value = ms.Length;
            cemoAttachment[mpcAttachment, "Description"].Value = sFileName;
            cemoAttachment[mpcAttachment, "Id"].Value = Guid.NewGuid().ToString();

            //Set the source and target for the attachment and save (this must be done first)
            cemroAttachment.SetSource(emoWorkItem); //Here I do not know how to get the current WorkItem?
            cemroAttachment.SetTarget(cemoAttachment);
            cemroAttachment.Commit();

            //Set the added by user relationship (this must done after the previous relationship)
            cemroAddedByUser.SetSource(cemoAttachment);
            cemroAddedByUser.SetTarget(emoUser); //Here I do not know how to get the current User?
            cemroAddedByUser.Commit();
        }
    }

The C#-Code for the attachment process is originally from this web page. My Problem is that I do not know how to announce the current User and the current work item for the relationship. When I push the add button, the File Dialog Closes and nothing appears. I thought this Problem is issued by the missing relationship reference.

Does anyone of you has an idea where to start fixing that issue? Furthermore, do you have an idea what i could do to fix it?

Best Regards and thank you in advance.

Leguan
  • 1
  • 4

1 Answers1

0

For others who will face the same issue: The solution was quite easy to find:

 IDataItem idiParentWorkItem = this.DataContext as IDataItem;
 EnterpriseManagementObject emoWorkItem = emg.EntityObjects.GetObject<EnterpriseManagementObject>((Guid)idiParentWorkItem["$Id$"], ObjectQueryOptions.Default);
 EnterpriseManagementObject emoUser = emg.EntityObjects.GetObject<EnterpriseManagementObject>((Guid)ConsoleContextHelper.Instance.CurrentUser["$Id$"], ObjectQueryOptions.Default);

Furthermore, I was searching for the wrong memberbindings which I wanted to display. Therefore, I needed to change the member binding.

All in all the code which is working for me, looks like this:

c#:

internal void AddFileToListView(String sFile)
    {
        IDataItem idiParentWorkItem = this.DataContext as IDataItem;
        EnterpriseManagementObject emoWorkItem = emg.EntityObjects.GetObject<EnterpriseManagementObject>((Guid)idiParentWorkItem["$Id$"], ObjectQueryOptions.Default);
        EnterpriseManagementObject emoUser = emg.EntityObjects.GetObject<EnterpriseManagementObject>((Guid)ConsoleContextHelper.Instance.CurrentUser["$Id$"], ObjectQueryOptions.Default);

        //System.SupportingItem.Library; ManagementPack ID: 23e3ae8e-1981-8560-2e55-8730cbc04965
        ManagementPack mpSupporting =
           emg.ManagementPacks.GetManagementPack(new Guid("23e3ae8e-1981-8560-2e55-8730cbc04965"));

        //Get the System.FileAttachment class
        ManagementPackClass mpcAttachment = emg.EntityTypes.GetClass("System.FileAttachment", mpSupporting);

        //Get attachment details
        string sExt = Path.GetExtension(sFile);
        string sAttachmentName = Path.GetFileNameWithoutExtension(sFile);

        //Create new stream and read file into memory
        MemoryStream ms = new MemoryStream();

        using (FileStream fs = File.OpenRead(sFile))
        {
            ms.SetLength(fs.Length);
            fs.Read(ms.GetBuffer(), 0, (int)fs.Length);
        }

        //Did we get any data?
        if (ms != null && ms.Length != 0)
        {
            //Reset stream position
            ms.Position = 0;

            //Create a new attachment
            CreatableEnterpriseManagementObject cemoAttachment = new CreatableEnterpriseManagementObject(emg, mpcAttachment);

            //Create a new attachment relationship from SupportingItem -> File Attachment (via "HasFileAttachment: ID =aa8c26dc-3a12-5f88-d9c7-753e5a8a55b4)
            //DisplayName : Has File Attachment
            //Source      : System.WorkItem
            //Target      : System.FileAttachment

            ManagementPackRelationship relAttachment =
            emg.EntityTypes.GetRelationshipClass(new Guid("aa8c26dc-3a12-5f88-d9c7-753e5a8a55b4"));
            CreatableEnterpriseManagementRelationshipObject cemroAttachment =
                new CreatableEnterpriseManagementRelationshipObject(emg, relAttachment);

            //Create a new added by user relationship
            // ID: ffd71f9e-7346-d12b-85d6-7c39f507b7bb
            // DisplayName : Added By User
            //Source      : System.FileAttachment
            //Target      : System.User

            ManagementPackRelationship relAddedByUser =
            emg.EntityTypes.GetRelationshipClass(new Guid("ffd71f9e-7346-d12b-85d6-7c39f507b7bb"));
            CreatableEnterpriseManagementRelationshipObject cemroAddedByUser =
                new CreatableEnterpriseManagementRelationshipObject(emg, relAddedByUser);

            //Set properties of attachment
            string sFileName = sAttachmentName;
            if (sExt != "") sFileName += sExt;
            cemoAttachment[mpcAttachment, "AddedDate"].Value = DateTime.UtcNow;
            cemoAttachment[mpcAttachment, "DisplayName"].Value = sFileName;
            cemoAttachment[mpcAttachment, "Extension"].Value = sExt;
            cemoAttachment[mpcAttachment, "Content"].Value = ms;
            cemoAttachment[mpcAttachment, "Size"].Value = ms.Length;
            cemoAttachment[mpcAttachment, "Description"].Value = sFileName;
            cemoAttachment[mpcAttachment, "Id"].Value = Guid.NewGuid().ToString();

            //Set the source and target for the attachment and save (this must be done first)
            cemroAttachment.SetSource(emoWorkItem);
            cemroAttachment.SetTarget(cemoAttachment);

            ////Set the added by user relationship (this must done after the previous relationship)
            cemroAddedByUser.SetSource(cemoAttachment);
            cemroAddedByUser.SetTarget(emoUser);

            cemroAttachment.Commit();
            cemroAddedByUser.Commit();

            EnterpriseManagementObjectDataType emoDataType = new EnterpriseManagementObjectDataType(mpcAttachment);
            IDataItem item = emoDataType.CreateProxyInstance(cemoAttachment);

            Collection<IDataItem> items = lvAttachedRiskRelFiles.ItemsSource as Collection<IDataItem>;
            if (!items.Contains(item))
                items.Add(item);
        }
    }


xaml:

<ListView x:Name="lvAttachedRiskRelFiles" ItemsSource="{Binding Path=FileAttachments, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                                    MouseDoubleClick="lvAttachedRiskRelFiles_MouseDoubleClick">
                            <ListView.View>
                                <GridView>
                                    <scwpf:SortableGridViewColumn Header="Display Name" DisplayMemberBinding="{Binding DisplayName}" Width="Auto"/>
                                    <scwpf:SortableGridViewColumn DisplayMemberBinding="{Binding FileAttachmentAddedBy.DisplayName}" Width="Auto" Header="Added By"/>
                                    <scwpf:SortableGridViewColumn DisplayMemberBinding="{Binding Size}" Width="Auto" Header="Size" />
                                    <scwpf:SortableGridViewColumn DisplayMemberBinding="{Binding AddedDate}" Width="Auto" Header="Added Date"/>
                                </GridView>
                            </ListView.View>
                        </ListView>
Leguan
  • 1
  • 4