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.
- string siteURL = ”https:
- string documentListName = ”documents”;
- string documentListURL = ”https:
- string documentName = ”Test.docx”;
- public void DocumentUpload(siteURL, documentListNamel, documentListURL, documentName, byte[] documentStream) {
- using(ClientContext clientContext = new ClientContext(siteURL)) {
- List documentsList = clientContext.Web.Lists.GetByTitle(documentListName);
- var fileCreationInformation = new FileCreationInformation();
-
- fileCreationInformation.Content = documentStream;
- fileCreationInformation.Overwrite = true;
- fileCreationInformation.Url = siteURL + documentListURL + documentName;
- Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(fileCreationInformation);
- uploadFile.ListItemAllFields["DocType"] = "Favourites";
- uploadFile.ListItemAllFields.Update();
- clientContext.ExecuteQuery();
- }
- }
- Download a document in SharePoint2013 Document List
- string siteURL = "https://Gowtham.sharepoint.com/teams";
- string documentName = "Test.docx";
- public Stream DocumentDownload(siteURL, documentName) {
- ListItem item = GetDocumentFromSP(documentName);
- if (item != null) {
- using(ClientContext clientContext = new ClientContext(siteURL)) {
- FileInformation fInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, item["FileRef"].ToString());
- return fInfo.Stream;
- }
- }
- return null;
- }
- private static ListItem GetDocumentFromSP(string documentName) {
- ListItemCollection listItems = GetListItemCollectionFromSP("FileLeafRef", documentName, "newtext", 1);
- return (listItems != null && listItems.Count == 1) ? listItems[0] : null;
- }
File or Folder exist in SharePoint document library
The extension method, given below demonstrates how to determine whether file exists or not-
- if (ctx.Web.TryGetFileByServerRelativeUrl("/documents/SharePointTest.Pdf", out file))
- {
- console.WriteLine("File Exits");
- } else {
- console.WriteLine("File does not Exits");
- }
- public static bool TryGetFileByServerRelativeUrl(this Web web, string serverRelativeUrl, out File file)
- {
- var ctx = web.Context;
- try {
- file = web.GetFileByServerRelativeUrl(serverRelativeUrl);
- ctx.Load(file);
- ctx.ExecuteQuery();
- return true;
- } catch (ServerException ex) {
- if (ex.ServerErrorTypeName == "System.IO.FileNotFoundException") {
- file = null;
- return false;
- }
- throw;
- }
- }
Source - MSDN