如何为 SCSM 2012 R2 实现文件附件列表视图
How do I implement a file attachment listview for SCSM 2012 R2
我是新来的,因此,我的问题可能不像您习惯的那样清楚。不过,我希望您能给我一些解决问题的提示。
目前,我正在使用 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# 代码最初来自 this web page。
我的问题是我不知道如何公布当前用户和关系的当前工作项。
当我按下添加按钮时,文件对话框关闭并且什么也没有出现。我认为这个问题是由缺失的关系参考发出的。
你们中有人知道从哪里开始解决这个问题吗?
此外,您知道我可以做些什么来修复它吗?
此致,并提前致谢。
对于将面临同样问题的其他人:
解决方案很容易找到:
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);
此外,我正在搜索我想要显示的错误成员绑定。因此,我需要更改成员绑定。
总而言之,为我工作的代码如下所示:
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>
我是新来的,因此,我的问题可能不像您习惯的那样清楚。不过,我希望您能给我一些解决问题的提示。
目前,我正在使用 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# 代码最初来自 this web page。
我的问题是我不知道如何公布当前用户和关系的当前工作项。
当我按下添加按钮时,文件对话框关闭并且什么也没有出现。我认为这个问题是由缺失的关系参考发出的。
你们中有人知道从哪里开始解决这个问题吗? 此外,您知道我可以做些什么来修复它吗?
此致,并提前致谢。
对于将面临同样问题的其他人: 解决方案很容易找到:
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);
此外,我正在搜索我想要显示的错误成员绑定。因此,我需要更改成员绑定。
总而言之,为我工作的代码如下所示:
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>