Create a document set using JSOM and REST

31 Mar

To create a document set in a document library its necessary to add a Document Set content type to the library. After doing this a document set can be created using various techniques. In this post I’ll show you how to do so using JSOM or REST.


To use JSOM the appropriate references have to be added to the HTML, but the most important one is SP.DocumentManagement.js. Of course a nice UI can be set up to pass in values for necessary input parameters, but for demo purposes filled in variables are used as shown below.

var url = "";
var listTitle = "";
var docSetContentTypeID = "";
var docSetName = "";

var context = new SP.ClientContext(url);
var web = context.get_web();
var list = web.get_lists().getByTitle(listTitle);

var parentFolder = list.get_rootFolder();

var docSetContentType = context.get_site().get_rootWeb().get_contentTypes().getById(docSetContentTypeID);

  function () {
    SP.DocumentSet.DocumentSet.create(context, parentFolder, docSetName, docSetContentType.get_id());
    function () {
      logtoconsole("document set created");
    function (sender, args) {
      logtoconsole("document set error");

That’s all there is!


I found it more of a challenge using REST to add a document set to a library, because I couldn’t find any appropriate SharePoint 2013 REST endpoint to use.

I tried for example a POST to ‘/_api/web/folders’ with { ‘__metadata’: { ‘type’: ‘SP.Folder’ }, ‘ServerRelativeUrl’: ‘Documents/test1’ , ‘ContentTypeId’: ‘0x0120D520009403DDAFA2D9F54E885F81B4DA488BA00101’}
The following message was returned as result:

‘The property ‘ContentTypeId’ does not exist on type ‘SP.Folder’. Make sure to only use property names that are defined by the type.’

So I wasn’t able to specify a content type id when adding a folder, assuming a document set is a special type of folder.

Another option could be trying to add an item using a POST method to ‘/_api/web/lists/getbytitle(‘” + listTitle + “‘)/items with  { ‘__metadata’: { ‘type’: ‘SP.Data.Gedeelde_x0020__x0020_documentenItem’ } , ‘ContentTypeId’: ‘0x0120D520009403DDAFA2D9F54E885F81B4DA488BA00101’ };
The result was:

‘To add an item to a document library, use SPFileCollection.Add()’

The only way I could find was to use the good old (SP2010) listdata service…

  method: "POST",
  url: url + "/_vti_bin/listdata.svc/" + listTitle,
  data: JSON.stringify(docSetOptions),
  headers: {
    "Accept": "application/json;odata=verbose",
    "content-type": "application/json;odata=verbose",
    "X-RequestDigest": $('#__REQUESTDIGEST').val(),
    "Slug": _spPageContextInfo.siteServerRelativeUrl + "/" + url + docSetOptions.Path + "/" + docSetOptions.Title + "|" + docSetOptions.ContentTypeId,
}).success(function (data, status, headers, config) {
  logtoconsole("document set created");
}).error(function (data, status, headers, config) {
  logtoconsole("document set error");


With docsetOptions declared like:
var docSetOptions = {
‘Title’: myTitle,
‘Path’: ‘/Documents’,
‘ContentTypeId’: contentTypeId,
‘ContentType’: contentType};


Creating a document set using CSOM or REST can be done, sometimes with a nice challenge!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.