Client Objet Model (OM) is a great new addition in SharePoint 2010. I have discussed before how to manipulate lists and list items using Managed Object Model. Today I’ll discuss on how to attach file to list item or add file to library using Managed Client Object Model.
Upload File in Library
The following code snippet shows how to upload file in document library:
public void UploadFileInLibrary(string siteUrl, string webName, string libraryName, string subfolderPath, string fileName) { using (ClientContext clientContext = new ClientContext(siteUrl)) { string uploadLocation = Path.GetFileName(fileName); if (!string.IsNullOrEmpty(subfolderPath)) { uploadLocation = string.Format("{0}/{1}", subfolderPath, uploadLocation); } uploadLocation = string.Format("/{0}/{1}/{2}", webName, libraryName, uploadLocation); var list = clientContext.Web.Lists.GetByTitle(libraryName); var fileCreationInformation = new FileCreationInformation(); fileCreationInformation.Content = System.IO.File.ReadAllBytes(fileName); fileCreationInformation.Overwrite = true; fileCreationInformation.Url = uploadLocation; list.RootFolder.Files.Add(fileCreationInformation); clientContext.ExecuteQuery(); } }
In the above code snippet, I have constructed the full path to the update location by concatenating the subfolderpath with library location. The site url is the spsite location whereas web name is name of the web site. To upload a file I have use FileCreationInformation object which is part of Client Object Model. To use the above method you can call the method as shown below. The instance is the class instance which has the method. Here the subfolderpath is the folder location inside the library where the document will be uploaded.
Instance.UploadFileInLibrary("http://mysite","mywebname", LibraryName,"folder1/folder2", @"c:\myfiles\LibraryItem.xlsx");
Download File From Library
The following code snippets shows how to download file from library:
public void DownloadFileFromLibrary(string siteUrl, string webName, string libraryName, string subfolderPath, string fileName, string downloadPath) { using (ClientContext clientContext = new ClientContext(siteUrl)) { string filePath = string.Empty; if (!string.IsNullOrEmpty(subfolderPath)) { filePath = string.Format("/{0}/{1}/{2}/{3}", webName, libraryName, subfolderPath, fileName); } else { filePath = string.Format("/{0}/{1}/{2}", webName, subfolderPath, fileName); } var fileInformation = File.OpenBinaryDirect(clientContext, filePath); var stream = fileInformation.Stream; IList<byte> content = new List<byte>(); int b; while ((b = fileInformation.Stream.ReadByte()) != -1) { content.Add((byte)b); } var downloadFileName = Path.Combine(downloadPath, fileName); System.IO.File.WriteAllBytes(downloadFileName, content.ToArray()); fileInformation.Stream.Close(); } }
As above code shows, we can use Microsoft.SharePoint.Client.File.OpenBinaryDirect method to download file directly from SharePoint. However, SharePoint open the file as binary stream so you need read the full stream before processing the file. The subfolderpath is same as described in ‘Upload File in Library’ section.
Delete File From Library
The following code snippet shows how to delete file from library:
public void DeleteFileFormLibrary(string siteUrl, string webName, string listName, string subfolder, string attachmentFileName) { using (ClientContext clientContext = new ClientContext(siteUrl)) { string attachmentPath = string.Empty; if (string.IsNullOrEmpty(subfolder)) { attachmentPath = string.Format("/{0}/{1}/{2}", webName, listName, Path.GetFileName(attachmentFileName)); } else { attachmentPath = string.Format("/{0}/{1}/{2}/{3}", webName, listName, subfolder, Path.GetFileName(attachmentFileName)); } var file = clientContext.Web.GetFileByServerRelativeUrl(attachmentPath); file.DeleteObject(); clientContext.ExecuteQuery(); } }
As the above code , the DeleteObject method is invoked on file object to delete the file.
Attach File to ListItem
The following code snippet shows how to attach file to list item. The attachments in list are places in a folder whose location is like “http://siteurl/lists/[listname]/Attachments/[ListItemID]/[filename]”. So I have constructed the attchment location first and use Microsoft.SharePoint.Client.File.SaveBinaryDirect method to upload the file. Here in the method, Item id is the list item id that I want to attach the file to.
public void AttachFileToListItem(string siteUrl, string webName, string listName, int itemId, string fileName, bool overwrite) { using (ClientContext clientContext = new ClientContext(siteUrl)) { FileStream fileStream = new FileStream(fileName, FileMode.Open); string attachmentPath = string.Format("/{0}/Lists/{1}/Attachments/{2}/{3}", webName, listName, itemId, Path.GetFileName(fileName)); File.SaveBinaryDirect(clientContext, attachmentPath, fileStream, overwrite); } }
Download File from ListItem
The following code snippet shows how to download file that is attached with list item.
public void DownloadAttachedFileFromListItem(string siteUrl, string webName, int itemId, string attachmentName, string listName, string downloadLocation) { using (ClientContext clientContext = new ClientContext(siteUrl)) { string attachmentPath = string.Format("/{0}/lists/{1}/Attachments/{2}/{3}", webName, listName, itemId, Path.GetFileName(attachmentName)); var fileInformation = File.OpenBinaryDirect(clientContext, attachmentPath); IList<byte> content = new List<byte>(); int b; while ((b = fileInformation.Stream.ReadByte()) != -1) { content.Add((byte)b); } var downloadFileName = Path.Combine(downloadLocation, attachmentName); System.IO.File.WriteAllBytes(downloadFileName, content.ToArray()); fileInformation.Stream.Close(); } }
As said before, the attachment location is “http://siteurl/lists/[listname]/Attachments/[itemid]/[filename]”. So I have used Microsoft.SharePoint.Client.File.OpenBinraryDirect to download the file.
Delete Attached File From ListItem
The following code shows how to delete a file that is attached with list item.
public void DeleteAttachedFileFromListItem(string siteUrl, string webName, int itemId, string attachmentFileName, string listName) { using (ClientContext clientContext = new ClientContext(siteUrl)) { //http://siteurl/lists/[listname]/attachments/[itemid]/[filename] string attachmentPath = string.Format("/{0}/lists/{1}/Attachments/{2}/{3}", webName, listName, itemId, Path.GetFileName(attachmentFileName)); var file = clientContext.Web.GetFileByServerRelativeUrl(attachmentPath); file.DeleteObject(); clientContext.ExecuteQuery(); } }
Conclusion
The code snippet above just provide you the gist. One point to notice that the File class used in the code not from System.IO. This is from Microsoft.SharePoint.Client and the full name is “Microsoft.SharePoint.Client.File”.