Suppose, you have a SharePoint list that stores images and its link URL. The image may also be attached to the list item. In order to get the attachment url we can use the following method or you can modify it as your own purposes:
public static List<string> GetAttachemntUrls(ClientContext context, Guid listId, int itemId)
{
List<string> urls = new List<string>();
try
{
context.Load(context.Web, w => w.Lists);
List list = context.Web.Lists.GetById(listId);
context.Load(list, l => l.RootFolder);
Folder rootFolder = list.RootFolder;
context.Load(rootFolder, rf => rf.Folders);
FolderCollection folders = rootFolder.Folders;
context.Load(folders);
context.ExecuteQuery();
foreach (Folder folder in folders)
{
if (folder.Name == "Attachments")
{
FolderCollection attachmentFolders = folder.Folders;
context.Load(attachmentFolders);
context.ExecuteQuery();
foreach (Folder itemFolder in attachmentFolders)
{
if (itemFolder.Name == itemId.ToString())
{
FileCollection files = itemFolder.Files;
context.Load(files);
context.ExecuteQuery();
foreach (File file in files)
{
urls.Add(file.ServerRelativeUrl);
}
break;
}
}
break;
}
}
}
catch (Exception ex)
{
throw ex;
}
return urls;
}
Happy SharePointing.....................:)
This comment has been removed by the author.
ReplyDelete