Xavierk
Xavierk

Reputation: 219

Create DocumentSet with REST api

How would one format the rest uri to create a document set in SP?

The JSOM code below works fine, but I would prefer to use the REST in order to be able to call it from a workflow.

var dsresult = SP.DocumentSet.DocumentSet.create(context, parentFolder, "docsetfromjsom", docsetCtId);

I tried this format based on this MSDN article

var restQueryUrl = spAppWebUrl + "/_api/SP.AppContextSite(@target)/SP.DocumentSet.DocumentSet.create('serverrelativeurl','docsetname','ctid')?@target='spHostUrl'";

Tried other formats as well but none successful. In jsom you also need to include the context, but I am assuming that for the rest call you don't need to use it (i think). Anyone tried this before?

Thanks!

Upvotes: 0

Views: 3405

Answers (1)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59358

I've already answered a similar question at SharePoint StackExchange.

To summarize, it does not seem possible to create Document Set using SharePoint 2013 REST API since SP.DocumentSet.DocumentSet.create function is not accessible via REST. But you could utilize SharePoint 2010 REST API instead for that purpose.

The following example demonstrates how to create a Document Set using SharePoint 2010 REST Interface:

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


function createFolder(webUrl,listName,folderUrl,folderContentTypeId) 
{                
     return $.ajax({
           url: webUrl + "/_vti_bin/listdata.svc/" + listName,
           type: "POST",
           contentType: "application/json;odata=verbose",
           headers: {
              "Accept": "application/json;odata=verbose",
              "Slug": folderUrl + "|" + folderContentTypeId
           }
     });
}


function createDocumentSet(webUrl,listName,docSetName)
{
    return getListUrl(webUrl,listName)
           .then(function(data){
               var folderUrl = data.d.ServerRelativeUrl + '/' + docSetName;
               return createFolder(webUrl,listName,folderUrl,'0x0120D520');
           }); 
}

Usage

Create Document Set named Orders in Documents library:

createDocumentSet(webUrl,'Documents','Orders')
.done(function(data)
{
    console.log('Document Set ' + data.d.Name + ' has been created succesfully'); 
})
.fail(
function(error){
    console.log(JSON.stringify(error));
});

Upvotes: 2

Related Questions