title: Attachments using C# to Sharepoint
body: <p>I have been trying to add new <code>list items with attachments</code> to my list .
  The list items are added fine, however I cannot figure out how to attach files to those items </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using ConsoleApplication3.TestReference;
  using System.IO;
  
  namespace ConsoleApplication3
  {
      class Program
      {
          static void Main(string[] args)
          {
              MVSDataContext dc = new MVSDataContext(
                  new Uri("https:xxx_vti_bin/ListData.svc"));
              dc.Credentials = System.Net.CredentialCache.DefaultCredentials;
  
              TestListItem newItem = new TestListItem { Title = "test6" };
              dc.AddToTestList(newItem);
              dc.SaveChanges();
  
              /* up to this point everything works fine and i am able to add a new list item */                 
  
              FileStream fStream = File.OpenRead("C:\\xxxxx.xlsx");
              string fileName = fStream.Name.Substring(3);
              byte[] contents = new byte[fStream.Length];
              fStream.Read(contents, 0, (int)fStream.Length);
              fStream.Close();
  
              /* the file read  successfuly . Now I should use `contents` to upload the file */ 
  
              newItem.Attachments.Add(fStream.Name, contents); /* This line does not compile */
  
  
              dc.SaveChanges();
  
              }
          }
      }
  }
  </code></pre>
  
  <hr>
  
  <p>I also tried approach from <a href="https://stackoverflow.com/questions/1334695/sharepoint-how-to-add-an-attachment-to-a-list-item-programatically/1334860#1334860">here</a></p>
  
  <pre><code>DirectoryInfo  attachmentDirectory = new DirectoryInfo(@"C:\xxx");
              FileInfo[] attachments = attachmentDirectory.GetFiles();
              foreach (FileInfo attachment in attachments)
              {
                  FileStream fs = new FileStream(attachment.FullName, FileMode.Open, FileAccess.Read);
  
                  // Create a byte array of file stream length
                  byte[] ImageData = new byte[fs.Length];
  
                  //Read block of bytes from stream into the byte array
                  fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length));
  
                  //Close the File Stream
                  fs.Close();
  
                  newItem.Attachments.Add(attachment.Name, ImageData); 
  
  
              }
  </code></pre>
  
  <p>However got the same error</p>
  
  <p>How can I make an attachment to a list item ?</p>
  
  <hr>
  
  <p>Here is the TestListItem class</p>
  
  <pre><code>      //------------------------------------------------------------------------------
      // &lt;auto-generated&gt;
      //     This code was generated by a tool.
      //     Runtime Version:4.0.30319.18052
      //
      //     Changes to this file may cause incorrect behavior and will be lost if
      //     the code is regenerated.
      // &lt;/auto-generated&gt;
      //------------------------------------------------------------------------------
  
      // Original file name:
      // Generation date: 1/30/2014 11:00:30 AM
      namespace ConsoleApplication3.TestReference
      {
  
          /// &lt;summary&gt;
          /// There are no comments for MVSDataContext in the schema.
          /// &lt;/summary&gt;
          public partial
  
   class MVSDataContext : global::System.Data.Services.Client.DataServiceContext
      {
          /// &lt;summary&gt;
          /// Initialize a new MVSDataContext object.
          /// &lt;/summary&gt;
          [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
          public MVSDataContext(global::System.Uri serviceRoot) : 
                  base(serviceRoot)
          {
              this.ResolveName = new global::System.Func&lt;global::System.Type, string&gt;(this.ResolveNameFromType);
              this.ResolveType = new global::System.Func&lt;string, global::System.Type&gt;(this.ResolveTypeFromName);
              this.OnContextCreated();
          }
          partial void OnContextCreated();
          /// &lt;summary&gt;
          /// Since the namespace configured for this service reference
          /// in Visual Studio is different from the one indicated in the
          /// server schema, use type-mappers to map between the two.
          /// &lt;/summary&gt;
          [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
          protected global::System.Type ResolveTypeFromName(string typeName)
          {
              if (typeName.StartsWith("Microsoft.SharePoint.DataService", global::System.StringComparison.Ordinal))
              {
                  return this.GetType().Assembly.GetType(string.Concat("ConsoleApplication3.TestReference", typeName.Substring(32)), false);
              }
              return null;
          }
          /// &lt;summary&gt;
          /// Since the namespace configured for this service reference
          /// in Visual Studio is different from the one indicated in the
          /// server schema, use type-mappers to map between the two.
          /// &lt;/summary&gt;
          [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
          protected string ResolveNameFromType(global::System.Type clientType)
          {
              if (clientType.Namespace.Equals("ConsoleApplication3.TestReference", global::System.StringComparison.Ordinal))
              {
                  return string.Concat("Microsoft.SharePoint.DataService.", clientType.Name);
              }
              return null;
          }
          /// &lt;summary&gt;
          /// There are no comments for Attachments in the schema.
          /// &lt;/summary&gt;
          [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
          public global::System.Data.Services.Client.DataServiceQuery&lt;AttachmentsItem&gt; Attachments
          {
              get
              {
                  if ((this._Attachments == null))
                  {
                      this._Attachments = base.CreateQuery&lt;AttachmentsItem&gt;("Attachments");
                  }
                  return this._Attachments;
              }
          }
          [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
          private global::System.Data.Services.Client.DataServiceQuery&lt;AttachmentsItem&gt; _Attachments;
          /// &lt;summary&gt;
          /// There are no comments for MasterPageGallery in the schema.
          /// &lt;/summary&gt;
          [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
          public global::System.Data.Services.Client.DataServiceQuery&lt;MasterPageGalleryItem&gt; MasterPageGallery
          {
              get
              {
                  if ((this._MasterPageGallery == null))
                  {
                      this._MasterPageGallery = base.CreateQuery&lt;MasterPageGalleryItem&gt;("MasterPageGallery");
                  }
                  return this._MasterPageGallery;
              }
          }
          [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
          private global::System.Data.Services.Client.DataServiceQuery&lt;MasterPageGalleryItem&gt; _MasterPageGallery;
          /// &lt;summary&gt;
          /// There are no comments for MasterPageGalleryCompatibleUIVersionS in the schema.
          /// &lt;/summary&gt;
          [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
          public global::System.Data.Services.Client.DataServiceQuery&lt;MasterPageGalleryCompatibleUIVersionSValue&gt; MasterPageGalleryCompatibleUIVersionS
          {
              get
              {
                  if ((this._MasterPageGalleryCompatibleUIVersionS == null))
                  {
                      this._MasterPageGalleryCompatibleUIVersionS = base.CreateQuery&lt;MasterPageGalleryCompatibleUIVersionSValue&gt;("MasterPageGalleryCompatibleUIVersionS");
                  }
                  return this._MasterPageGalleryCompatibleUIVersionS;
              }
          }
          [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
          private global::System.Data.Services.Client.DataServiceQuery&lt;MasterPageGalleryCompatibleUIVersionSValue&gt; _MasterPageGalleryCompatibleUIVersionS;
          /// &lt;summary&gt;
          /// There are no comments for TestList in the schema.
          /// &lt;/summary&gt;
          [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
          public global::System.Data.Services.Client.DataServiceQuery&lt;TestListItem&gt; TestList
          {
              get
              {
                  if ((this._TestList == null))
                  {
                      this._TestList = base.CreateQuery&lt;TestListItem&gt;("TestList");
                  }
                  return this._TestList;
              }
          }
          [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
          private global::System.Data.Services.Client.DataServiceQuery&lt;TestListItem&gt; _TestList;
          /// &lt;summary&gt;
          /// There are no comments for UserInformationList in the schema.
          /// &lt;/summary&gt;
          [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
          public global::System.Data.Services.Client.DataServiceQuery&lt;UserInformationListItem&gt; UserInformationList
          {
              get
              {
                  if ((this._UserInformationList == null))
                  {
                      this._UserInformationList = base.CreateQuery&lt;UserInformationListItem&gt;("UserInformationList");
                  }
                  return this._UserInformationList;
              }
          }
          [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
          private global::System.Data.Services.Client.DataServiceQuery&lt;UserInformationListItem&gt; _UserInformationList;
          /// &lt;summary&gt;
          /// There are no comments for Attachments in the schema.
          /// &lt;/summary&gt;
          [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
          public void AddToAttachments(AttachmentsItem attachmentsItem)
          {
              base.AddObject("Attachments", attachmentsItem);
          }
          /// &lt;summary&gt;
          /// There are no comments for MasterPageGallery in the schema.
          /// &lt;/summary&gt;
          [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
          public void AddToMasterPageGallery(MasterPageGalleryItem masterPageGalleryItem)
          {
              base.AddObject("MasterPageGallery", masterPageGalleryItem);
          }
          /// &lt;summary&gt;
          /// There are no comments for MasterPageGalleryCompatibleUIVersionS in the schema.
          /// &lt;/summary&gt;
          [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
          public void AddToMasterPageGalleryCompatibleUIVersionS(MasterPageGalleryCompatibleUIVersionSValue masterPageGalleryCompatibleUIVersionSValue)
          {
              base.AddObject("MasterPageGalleryCompatibleUIVersionS", masterPageGalleryCompatibleUIVersionSValue);
          }
          /// &lt;summary&gt;
          /// There are no comments for TestList in the schema.
          /// &lt;/summary&gt;
  ..............
  </code></pre>