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.
Reference - Create Document Set In Document Library Using CSOM : SharePoint 2010