Search This Blog

Saturday, June 10, 2017

How to read listitem attachments using sharepoint client object model

The below method is used to get the files which is attached in the listitem in the custom list

public static FileCollection GetListItemAttachments( string sitePath, string listName, string itemID)
{
   try
   {
      FileCollection oFileCollection=null;
      Folder attachmentsFolder = null;
      ClientContext clientContext = new ClientContext(sitePath);
      Web web = clientContext.Web;
      clientContext.Load(web);
      clientContext.ExecuteQuery();

      attachmentsFolder = web.GetFolderByServerRelativeUrl(sitePath + "/Lists/" + listName + "/attachments/" + itemID);
      clientContext.Load(attachmentsFolder);
      oFileCollection = attachmentsFolder.Files;
      clientContext.Load(oFileCollection);
      clientContext.ExecuteQuery();
   
      return oFileCollection;
   }
   catch (Exception ex)
   {
     throw ex;
   }
 }

No comments:

Post a Comment