Wednesday, 9 September 2015

How to read SharePoint list items attachments programmatically

SPSite mySite = new SPSite(http://Mohan:8899);
  SPWeb myweb = mySite.OpenWeb();
  SPList myList = myweb.Lists["Employees"];
  SPListItem myListItem = myList.GetItemById(1);
  foreach (String attachmentname in myListItem.Attachments)
  {
    // gets the containing directory URL

String attachmentAbsoluteURL =
   myListItem.Attachments.UrlPrefix 
+ attachmentname;

   // To get the SPSile reference to the attachment    SPFile attachmentFile = myweb.GetFile(attachmentAbsoluteURL);
 
   // To read the file content 
   Stream stream = attachmentFile.OpenBinaryStream();
   StreamReader reader = new StreamReader(stream);
   String fileContent = reader.ReadToEnd();
 
  
   }

No comments:

Post a Comment