I'm trying to generate document sets on the fly with the following code. It is using /_layouts/15/SP.DocumentManagement.js for the SP.DocumentSet.DocumentSet.create method which takes four parameters.

The last parameter is supposed to be a content type id object but I'm getting an exception in the console that I haven't been able to unravel. Uncaught Error: Object reference not set to an instance of an object on server. The object is associated with method GetById.

I'm following these guidelines for creating it. Can anyone point me in the right direction for this error?

function CreateDocumentSet() {
    var ctx = new SP.ClientContext(siteUrl);
    var parentFolder;
    var docSetContentTypeID = "0x0120D520008C04C8264B92224699E49B9BE493F460";

    var web = ctx.get_web();
    var list = web.get_lists().getByTitle('Source Files');

    parentFolder = list.get_rootFolder();

    var docsetContentType = web.get_contentTypes().getById(docSetContentTypeID);

    ctx.executeQueryAsync(function () {
        SP.DocumentSet.DocumentSet.create(ctx, parentFolder, thisItemID, docsetContentType.get_id());
            SuccessHandler('Document Set creation successful'),
            FailureHandler("Document Set creation failed")
    }, FailureHandler("Folder loading failed"));
  • Just one question. I have tried your code and it seems to work fine except one issue. When the Document-Set is created and modern experience is turned on the document set is shown as a folder. When you click on that folder you will not get redirected to DocSetHome.aspx... When you switch back to classic it is shown as a document-set and everythings seems to work as expected. Any ideas? Maybe there is a new method we should use in Sharepoint-Online? Thanks in advance.
    – Lutz
    Sep 23, 2018 at 15:36

1 Answer 1


Most probably the specified error occurs since you are trying to get site-level Content Type while web is not a root web.

Quick fix

Replace the line:

var docsetContentType = web.get_contentTypes().getById(docSetContentTypeID);


var docsetContentType = ctx.get_site().get_rootWeb().get_contentTypes().getById(docSetContentTypeID); 

How to create a Document Set using JSOM

The following function demonstrates how to create a Document Set:

function createDocumentSet(siteUrl,listTitle,docSetName,success,error) {
    var ctx = new SP.ClientContext(siteUrl);
    var web = ctx.get_web();
    var list = web.get_lists().getByTitle(listTitle);

    var parentFolder = list.get_rootFolder();

    var docSetContentTypeID = "0x0120D520";
    var docSetContentType = ctx.get_site().get_rootWeb().get_contentTypes().getById(docSetContentTypeID);

    ctx.executeQueryAsync(function (){ 
        SP.DocumentSet.DocumentSet.create(ctx, parentFolder, docSetName, docSetContentType.get_id());


SP.SOD.executeFunc('sp.documentmanagement.js', 'SP.DocumentSet.DocumentSet', function() {


   function printInfo()
      console.log('Doc Set has been created');  

   function logError(sender,args)

  • 1
    That was exactly it - I was doing this a couple sites deep - thanks again Vadim.
    – Aaron
    Sep 11, 2014 at 0:06

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.