Below function uploads any file to a docset using Client object modeling.
public void UploadDocument(string siteurl, string listname, string docsetname, string filename)
{
using (ClientContext clientContext = new ClientContext(siteurl))
{
//Get Document List
List documentsList = clientContext.Web.Lists.GetByTitle(HttpUtility.UrlDecode(listname));
var fileCreationInformation = new FileCreationInformation();
//Assign to content byte[] i.e. documentStream
fileCreationInformation.Content = System.IO.File.ReadAllBytes( filename )); ;
//Allow owerwrite of document
fileCreationInformation.Overwrite = true;
//Upload URL
fileCreationInformation.Url = siteurl + "/" + listname + "/" + docsetname + "/" + filename";
Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(fileCreationInformation);
uploadFile.ListItemAllFields.Update();
clientContext.ExecuteQuery();
}
}
public void UploadDocument(string siteurl, string listname, string docsetname, string filename)
{
using (ClientContext clientContext = new ClientContext(siteurl))
{
//Get Document List
List documentsList = clientContext.Web.Lists.GetByTitle(HttpUtility.UrlDecode(listname));
var fileCreationInformation = new FileCreationInformation();
//Assign to content byte[] i.e. documentStream
fileCreationInformation.Content = System.IO.File.ReadAllBytes( filename )); ;
//Allow owerwrite of document
fileCreationInformation.Overwrite = true;
//Upload URL
fileCreationInformation.Url = siteurl + "/" + listname + "/" + docsetname + "/" + filename";
Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(fileCreationInformation);
uploadFile.ListItemAllFields.Update();
clientContext.ExecuteQuery();
}
}
Call the above function as below
UploadDocument("https://testsite", "test Library", "My Doc Set", "C:\test.ppt");
if you look at the code above we are reading the bytes from the file(which is in the client machine) and uploading it using the Microsoft.SharePoint.Client.File class.
Hi Suresh, thank you for posting this. Very helpful.
ReplyDeleteThanks Shane. Glad that it was useful.
ReplyDelete