Upload/Download A Document In SharePoint2013 Document List Using Client Object Model

Introduction

In this blog, I will explain how to upload a document into SharePoint Library and upload a new document to the library, using Client Object Model. The blog, given below, describes how you can completely programmatically demonstrate CSOM operations.

SharePoint Client API

We can use the SharePoint Client Object model to retrieve, update and manage the data in SharePoint 2013.

When you create an Add-in for SharePoint 2013 project in Visual Studio 2012, we need to include Microsoft.SharePoint.Client.Runtime.dll and Microsoft.SharePoint.Client.dll are automatically added to the project.

Upload/Download a document in SharePoint2013 Document List

Open your Visual Studio, add a new project and copy/paste the code, given below and call the function.

  1. string siteURL = ”https: //Gowtham.sharepoint.com/teams”;  
  2.     string documentListName = ”documents”;  
  3. string documentListURL = ”https: //Gowtham.sharepoint.com/teams/documents”;  
  4.     string documentName = ”Test.docx”;  
  5. public void DocumentUpload(siteURL, documentListNamel, documentListURL, documentName, byte[] documentStream) {  
  6.     using(ClientContext clientContext = new ClientContext(siteURL)) {  
  7.         List documentsList = clientContext.Web.Lists.GetByTitle(documentListName);  
  8.         var fileCreationInformation = new FileCreationInformation();  
  9.         //Assign to content byte[] i.e. documentStream  
  10.         fileCreationInformation.Content = documentStream;  
  11.         fileCreationInformation.Overwrite = true;  
  12.         fileCreationInformation.Url = siteURL + documentListURL + documentName;  
  13.         Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(fileCreationInformation);  
  14.         uploadFile.ListItemAllFields["DocType"] = "Favourites";  
  15.         uploadFile.ListItemAllFields.Update();  
  16.         clientContext.ExecuteQuery();  
  17.     }  
  18. }  
  19. Download a document in SharePoint2013 Document List  
  20. string siteURL = "https://Gowtham.sharepoint.com/teams";  
  21. string documentName = "Test.docx";  
  22. public Stream DocumentDownload(siteURL, documentName) {  
  23.     ListItem item = GetDocumentFromSP(documentName);  
  24.     if (item != null) {  
  25.         using(ClientContext clientContext = new ClientContext(siteURL)) {  
  26.             FileInformation fInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, item["FileRef"].ToString());  
  27.             return fInfo.Stream;  
  28.         }  
  29.     }  
  30.     return null;  
  31. }  
  32. private static ListItem GetDocumentFromSP(string documentName) {  
  33.     ListItemCollection listItems = GetListItemCollectionFromSP("FileLeafRef", documentName, "newtext", 1);  
  34.     return (listItems != null && listItems.Count == 1) ? listItems[0] : null;  
  35. }  
File or Folder exist in SharePoint document library

The extension method, given below demonstrates how to determine whether file exists or not-
  1. if (ctx.Web.TryGetFileByServerRelativeUrl("/documents/SharePointTest.Pdf", out file))  
  2. {  
  3.     console.WriteLine("File Exits");  
  4. else {  
  5.     console.WriteLine("File does not Exits");  
  6. }  
  7. public static bool TryGetFileByServerRelativeUrl(this Web web, string serverRelativeUrl, out File file)  
  8. {  
  9.     var ctx = web.Context;  
  10.     try {  
  11.         file = web.GetFileByServerRelativeUrl(serverRelativeUrl);  
  12.         ctx.Load(file);  
  13.         ctx.ExecuteQuery();  
  14.         return true;  
  15.     } catch (ServerException ex) {  
  16.         if (ex.ServerErrorTypeName == "System.IO.FileNotFoundException") {  
  17.             file = null;  
  18.             return false;  
  19.         }  
  20.         throw;  
  21.     }  
  22. }  
Source - MSDN