Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload a document to a SharePoint list from Client Side Object Model

I need to upload a document to a SharePoint list or folder using the Client Side Object Model from .NET (C#). What is the best way to do this?

The requirements are as follows:

  • Set metadata values

  • No limitation on file size

  • Must work with libraries that exceed the List View Threshold

like image 456
Martijn Boeker Avatar asked Mar 24 '12 00:03

Martijn Boeker


People also ask

Can you upload documents to a SharePoint list?

SharePoint has an attachment column which is available OOB. You can use that to upload a document in the list. Click on New item in the list and you can see in the form the option to Attach File. In this way you can upload a document to a list OOB.


1 Answers

For Uploading Document to Sharepoint Document Library use Following function in Client Object Model:

public void UploadDocument(string siteURL, string documentListName, string documentListURL, string documentName, byte[] documentStream) {     using (ClientContext clientContext = new ClientContext(siteURL))     {         //Get Document List         List documentsList = clientContext.Web.Lists.GetByTitle(documentListName);          var fileCreationInformation = new FileCreationInformation();         //Assign to content byte[] i.e. documentStream          fileCreationInformation.Content = documentStream;         //Allow owerwrite of document          fileCreationInformation.Overwrite = true;         //Upload URL          fileCreationInformation.Url = siteURL + documentListURL + documentName;         Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(             fileCreationInformation);          //Update the metadata for a field having name "DocType"         uploadFile.ListItemAllFields["DocType"] = "Favourites";          uploadFile.ListItemAllFields.Update();         clientContext.ExecuteQuery();     } } 

Following link is Also Helpful For you 1) http://blogs.msdn.com/b/sridhara/archive/2010/03/12/uploading-files-using-client-object-model-in-sharepoint-2010.aspx

2)http://msdn.microsoft.com/en-us/library/ee956524.aspx

3)http://www.codeproject.com/Articles/103503/How-to-upload-download-a-document-in-SharePoint-20

like image 155
Rony SP Avatar answered Sep 25 '22 23:09

Rony SP