2

I am using the following code to create a document set,but each time it creates a folder instead.

        string dsName = "Vijai Documents";



        //// Get the document library in which the document set has to be created
        List list = ctx.Web.Lists.GetByTitle("Companies");
        log.Info("this works");

       ContentType ct = ctx.Site.RootWeb.ContentTypes.GetById("0x0120D520");
        ctx.Load(ct);
        ctx.ExecuteQuery();


        //// Create a new document set
        //// A new document set will be created in "Documents" library as "Vijai Documents" under which you can add the documents
        DocumentSet.Create(ctx, folder, dsName, ct.Id);
        ctx.ExecuteQuery();

2 Answers 2

3

The trick is to use the long id of the content type i.e use the content type id from the list. Not really sure, why the content type id at web level is not working.

You can try either of the 3 options mentioned below:

1)

Web oWeb = context.Site.RootWeb;
context.Load(oWeb);
context.ExecuteQuery();

var list = context.Web.Lists.GetByTitle("Documents");
context.Load(list,l => l.RootFolder,l => l.ContentTypes);                
context.ExecuteQuery();

//use the id of the list content type
var ct = list.ContentTypes.GetById("0x0120D52000A623EADA52BAF246AAD86D3E8816B2FE");
context.Load(ct);
context.ExecuteQuery();

DocumentSet.Create(context, list.RootFolder, "Vijai Documents", ct.Id);
context.ExecuteQuery();

2) Reference from Aakash's answer from the link below, it was for 2010 but it works in 2013 and SPO as well -

Or, you can use the LINQ method, if too bothered about performance as below:

var list = context.Web.Lists.GetByTitle("Documents");
context.Load(list,l => l.RootFolder,l => l.ContentTypes);                
context.ExecuteQuery();

var ctData = list.ContentTypes.Where(c => c.Name == "Document Set");
var contentType = ctData.FirstOrDefault();
context.Load(contentType);
context.ExecuteQuery();

DocumentSet.Create(context, list.RootFolder, "Vijai Documents", contentType.Id);
context.ExecuteQuery();

3)

Also, my personal favorite , use the PnP extension method, list.ContentTypes.GetByName as well.

var ct = list.ContentTypes.GetByName("Document Set");
context.Load(ct);
context.ExecuteQuery();

DocumentSet.Create(context, list.RootFolder, "Vijai Document set", ct.Id);
context.ExecuteQuery();

Use the nuget to download the PnP core dll for SPO as below.

enter image description here

Reference - Create Document Set In Document Library Using CSOM : SharePoint 2010

3
  • how to manage the metadata of document sets using C#+CSOM. can you please elaborate with any sample code..
    – samolpp2
    Commented Dec 6, 2018 at 14:47
  • can you ask that as a separate question here, cant elaborate in comments ? Basically its just a list item, so you can update it properties like you update the props of a list item :) Commented Dec 7, 2018 at 17:01
  • yes, i can ask in a separate thread.
    – samolpp2
    Commented Dec 11, 2018 at 9:19
0

You can also create the document set using rest API easily. May below link is helpful to you.

Is it possible to create a Document Set using REST API

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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