16

I know this can be done using the JSOM, but is it possible via the REST API to create a document set?

4
  • Great question, Robert! Commented Sep 12, 2014 at 7:01
  • Thanks Vadim. I know it must be if the JSOM can do it. I'm hoping to be able to avoid using Fiddler or similar to figure it out. But it looks like I may have to. Commented Sep 12, 2014 at 13:19
  • So you had created document set using rest api? I know its an old post but still curious to know the answer. If not, have you tried to include property 'ContentTypeId':'DocumentSetId' in metadata while making ajax request? Maybe that work for you?
    – ateet
    Commented Sep 20, 2014 at 16:23
  • I've not tried yet. Going to use JSOM and see what it calls on the backend. Then I'll update this with an answer. Commented Sep 20, 2014 at 22:00

1 Answer 1

15

How to create a Document Set using SharePoint REST Interface

The following example demonstrates how to create a Document Set in SharePoint 2013:

function getListUrl(webUrl,listName,success, error) 
{
    var headers = {};
    $.ajax({       
       url: webUrl + "/_api/lists/getbytitle('" + listName + "')/rootFolder?$select=ServerRelativeUrl",   
       type: "GET",   
       contentType: "application/json;odata=verbose",
       headers: { 
          "Accept": "application/json;odata=verbose"
       },   
       success: function(data){
           success(data.d.ServerRelativeUrl);
       },
       error: error
    });
}

function createFolder(webUrl,listName,folderName,folderContentTypeId, success, error) 
{  
    getListUrl(webUrl,listName,
      function(listUrl) {
          var folderPayload = {
             'Title' : folderName,
             'Path' : listUrl
          };

          //Create Folder resource
          $.ajax({
                url: webUrl + "/_vti_bin/listdata.svc/" + listName,
                type: "POST",
                contentType: "application/json;odata=verbose",
                data: JSON.stringify(folderPayload),
                headers: {
                   "Accept": "application/json;odata=verbose",
                   "Slug": listUrl + "/" + folderName + "|" + folderContentTypeId
                },
                success: function (data) {
                    success(data.d);
                },
                error: error
          });
      },
      error);
}


function createDocumentSet(webUrl,listName,folderName, success, error)
{
   createFolder(webUrl,listName,folderName,'0x0120D520', success, error);
}

Comments:

  • getListUrl function is used for retrieving List/Library Url

  • With some modifications applied (in fact it concerns getListUrl function only) it could be used in SharePoint 2010

Example

Create Document Set named Orders in Documents library:

createDocumentSet(_spPageContextInfo.webAbsoluteUrl,'Documents','Orders',
  function(folder){
    console.log('Document Set ' + folder.Name + ' has been created succesfully'); 
  },
  function(error){
    console.log(JSON.stringify(error));
  }
);
7
  • 1
    Thank you! I was able to use this example to make a web service call from within a SharePoint Designer WorkFlow!! The "Slug" was a key factor!!!! So happy... Commented Oct 8, 2014 at 20:43
  • 1
    Thanks, Vadim. Even though this uses ListData.svc and not _API, it was exactly what we needed. Commented Oct 10, 2014 at 13:12
  • It seems to ignore the folderPayload setting, is it possible at all to set metadata on creation?? I have a custom sitecolumn with a validation rule which causes an error on creation, if I remove the validation rule everything works fin. Commented Apr 4, 2015 at 15:12
  • @Vadim Does this still work? When I run this code I get a 500 Internal Server error that just says "An error occurred while processing this request.
    – James11
    Commented Nov 5, 2015 at 16:50
  • @James11, hm, it works for me, is Document Set content type is added into library? Commented Nov 5, 2015 at 17:26

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.