title: How do I implement a file attachment listview for SCSM 2012 R2
body: <p>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:
  <br>
  <br>The type projection within the panagement pack:<br></p>
  
  <pre><code>&lt;TypeProjection ID="TypeProjection.RFCMinor" Accessibility="Public" Type="RFC.Minor.RFCMinor"&gt;
        &lt;Component Path="$Target/Path[Relationship='WorkItem!System.WorkItemHasFileAttachment']$" Alias="FileAttachments"&gt;
          &lt;Component Path="$Target/Path[Relationship='SupportingItem!System.FileAttachmentAddedByUser']$" Alias="FileAttachmentAddedBy" /&gt;
        &lt;/Component&gt;
      &lt;/TypeProjection&gt;
  </code></pre>
  
  <p>The list view item within the xaml-Code:</p>
  
  <pre><code>&lt;ListView x:Name="lvAttachedRiskRelFiles" MinHeight="70" ItemsSource="{Binding Path=FileAttachments, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                                      MouseDoubleClick="lvAttachedRiskRelFiles_MouseDoubleClick"&gt;
                              &lt;ListView.View&gt;
                                  &lt;GridView&gt;
                                      &lt;scwpf:SortableGridViewColumn Header="File Name" DisplayMemberBinding="{Binding FileName}" Width="Auto"/&gt;
                                      &lt;scwpf:SortableGridViewColumn Header="Attached By" DisplayMemberBinding="{Binding AttachedBy}" Width="Auto"/&gt;
                                      &lt;scwpf:SortableGridViewColumn Header="File Size (KB)" DisplayMemberBinding="{Binding FileSize}" Width="Auto"/&gt;
                                      &lt;scwpf:SortableGridViewColumn Header="Attached Date" DisplayMemberBinding="{Binding AttachedDate}" Width="Auto"/&gt;
                                  &lt;/GridView&gt;
                              &lt;/ListView.View&gt;
                              &lt;Button Content="Add" Name="btnAddRiskFile" Click="btnAddRiskFile_Click"/&gt;
  </code></pre>
  
  <p>The C#-Code looks currently like:</p>
  
  <pre><code>private void btnAddRiskFile_Click(object sender, System.Windows.RoutedEventArgs e)
      {
          // Create OpenFileDialog 
          Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
          Nullable&lt;bool&gt; 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 &amp;&amp; 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 -&gt; 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();
          }
      }
  </code></pre>
  
  <p>The C#-Code for the attachment process is originally from <a href="http://scsmnz.net/c-code-snippets-for-service-manager-1/" rel="nofollow">this web page</a>. 
  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.
  <br><br></p>
  
  <p>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?</p>
  
  <p>Best Regards and thank you in advance.</p>