3

I'm trying to upload a file to a document set which is located in a library.

Im using C# and the Sharepoint Client Object Model - Library, but I can not find any documentation to do this task.

I know how to upload the file itself to the library's root, but I did not find any possibility to upload it to a document set yet.

Thanks in advance

3

How to upload file into Document Set using SharePoint 2010/2013 CSOM

/// <summary>
/// Upload file into Document Set
/// </summary>
/// <param name="list">List Title</param>
/// <param name="docSetName">DocumentSet Name</param>
/// <param name="fileName">File name to upload</param>
private static void UploadFileIntoDocumentSet(List list,string docSetName, string fileName)
{
    var ctx = list.Context;
    ctx.Load(list.RootFolder);
    ctx.ExecuteQuery();
    using (var fs = new FileStream(fileName, FileMode.Open))
    {
        var fi = new FileInfo(fileName);
        var fileUrl = String.Format("{0}/{1}/{2}", list.RootFolder.ServerRelativeUrl,docSetName, fi.Name);
        Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, fileUrl, fs, true);
     }
}

Usage:

 using (var ctx = new ClientContext(url))
 {
     var list = ctx.Web.Lists.GetByTitle(listTitle);
     UploadFileIntoDocumentSet(list,"Orders", @"C:\Order.docx");
 }
0

Document sets are somewhat similar to Folders in Document library. In below code we set the path to document set and upload Files to it:

//Declare path to document set
            SPFolder myLibraryToUploadDocument = spWeb.Folders[documentLibraryName].SubFolders[txtTitle.Text];
            // Prepare to upload
            Boolean replaceExistingFiles = true;
            String fileName = System.IO.Path.GetFileName(fullPathFileName);
            FileStream fileStream = File.OpenRead(fullPathFileName);
            // Upload document
            SPFile spfile = myLibraryToUploadDocument.Files.Add(fileName, fileStream, replaceExistingFiles);
            // Commit
            myLibraryToUploadDocument.Update();

[Source]http://www.microsofttechnology.net/2012/03/article-3-create-new-document-sets-and.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.