7

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');
    ctx.load(list);

    parentFolder = list.get_rootFolder();
    ctx.load(parentFolder);

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

    ctx.executeQueryAsync(function () {
        SP.DocumentSet.DocumentSet.create(ctx, parentFolder, thisItemID, docsetContentType.get_id());
        ctx.executeQueryAsync(
            SuccessHandler('Document Set creation successful'),
            FailureHandler("Document Set creation failed")
        );
    }, FailureHandler("Folder loading failed"));
}
8

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);

with:

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);
    ctx.load(list);

    var parentFolder = list.get_rootFolder();
    ctx.load(parentFolder);

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

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

Usage

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

   createDocumentSet(_spPageContextInfo.webAbsoluteUrl,'Documents','Orders',printInfo,logError);

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

   function logError(sender,args)
   { 
      console.log(args.get_message());
   }

});
  • 1
    That was exactly it - I was doing this a couple sites deep - thanks again Vadim. – Aaron Sep 11 '14 at 0:06
0

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.

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.