如何实现SCSM 2012 R2的文件附件列表视图

本文关键字:文件 视图 列表 R2 2012 何实现 实现 SCSM | 更新日期: 2023-09-27 18:19:36

我对这里很陌生,因此,我的问题可能不像你习惯的那样澄清。尽管如此,我希望你能给我一个解决问题的提示。目前,我正在使用带有创作扩展的Visual Studio 2013为System Center Service Manager 2012 R2 wit UR 7开发一个自定义更改请求表。此表单基于带有.Net FrameWork 3.5的WPF用户控件(Visual C#)。我的目标是开发一个列表视图,我想用添加按钮向该视图添加多个文件附件。代码如下所示:

包装内的类型投影:

<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>

xaml代码中的列表视图项:

<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"/>

C#代码目前看起来像:

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();
        }
    }

附件过程的C#代码最初来自这个网页。我的问题是,我不知道如何公布关系的当前用户和当前工作项。当我按下添加按钮时,"文件"对话框将关闭,并且不会显示任何内容。我认为这个问题是由缺失的关系参考发布的。

你们中有人知道从哪里开始解决这个问题吗?此外,你知道我能做些什么来修复它吗?

致以最良好的问候和感谢。

如何实现SCSM 2012 R2的文件附件列表视图

对于将面临相同问题的其他人:解决方案很容易找到:

 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);

此外,我正在搜索我想要显示的错误的memberbindings。因此,我需要更改成员绑定。

总而言之,为我工作的代码看起来是这样的:

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>