SOLVED

Creating a Document Set with the Sharepoint 2013 REST API

%3CLINGO-SUB%20id%3D%22lingo-sub-39445%22%20slang%3D%22en-US%22%3ECreating%20a%20Document%20Set%20with%20the%20Sharepoint%202013%20REST%20API%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-39445%22%20slang%3D%22en-US%22%3E%3CP%3EIs%20there%20(or%20does%20anyone%20at%20Microsoft%20know%20if%20there%20will%20be)%20support%20for%20creating%20Document%20Sets%20using%20the%202013%20version%20of%20the%20REST%20API%3F%3C%2FP%3E%3CP%3EI've%20successfully%20used%20the%202010%20%3CSPAN%3E%2F_vti_bin%2Flistdata.svc%2F%3C%2FSPAN%3E%20service%2C%20but%20for%20future%20proofing%2C%20wonder%20if%20I%20should%20be%20moving%20on%20to%20something%20like%20like%20the%20%3CSPAN%3E%2F_api%2Fweb%2Ffolders%20endpoints.%20%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%2C%3C%2FP%3E%3CP%3ERoss%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-39577%22%20slang%3D%22en-US%22%3ERe%3A%20Creating%20a%20Document%20Set%20with%20the%20Sharepoint%202013%20REST%20API%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-39577%22%20slang%3D%22en-US%22%3Efunction%20getListUrl(webUrl%2ClistName%2Csuccess%2C%20error)%3CBR%20%2F%3E%7B%3CBR%20%2F%3Evar%20headers%20%3D%20%7B%7D%3B%3CBR%20%2F%3E%24.ajax(%7B%3CBR%20%2F%3Eurl%3A%20webUrl%20%2B%20%22%2F_api%2Flists%2Fgetbytitle('%22%20%2B%20listName%20%2B%20%22')%2FrootFolder%3F%24select%3DServerRelativeUrl%22%2C%3CBR%20%2F%3Etype%3A%20%22GET%22%2C%3CBR%20%2F%3EcontentType%3A%20%22application%2Fjson%3Bodata%3Dverbose%22%2C%3CBR%20%2F%3Eheaders%3A%20%7B%3CBR%20%2F%3E%22Accept%22%3A%20%22application%2Fjson%3Bodata%3Dverbose%22%3CBR%20%2F%3E%7D%2C%3CBR%20%2F%3Esuccess%3A%20function(data)%7B%3CBR%20%2F%3Esuccess(data.d.ServerRelativeUrl)%3B%3CBR%20%2F%3E%7D%2C%3CBR%20%2F%3Eerror%3A%20error%3CBR%20%2F%3E%7D)%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%3CBR%20%2F%3Efunction%20createFolder(webUrl%2ClistName%2CfolderName%2CfolderContentTypeId%2C%20success%2C%20error)%3CBR%20%2F%3E%7B%3CBR%20%2F%3EgetListUrl(webUrl%2ClistName%2C%3CBR%20%2F%3Efunction(listUrl)%20%7B%3CBR%20%2F%3Evar%20folderPayload%20%3D%20%7B%3CBR%20%2F%3E'Title'%20%3A%20folderName%2C%3CBR%20%2F%3E'Path'%20%3A%20listUrl%3CBR%20%2F%3E%7D%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%2F%2FCreate%20Folder%20resource%3CBR%20%2F%3E%24.ajax(%7B%3CBR%20%2F%3Eurl%3A%20webUrl%20%2B%20%22%2F_vti_bin%2Flistdata.svc%2F%22%20%2B%20listName%2C%3CBR%20%2F%3Etype%3A%20%22POST%22%2C%3CBR%20%2F%3EcontentType%3A%20%22application%2Fjson%3Bodata%3Dverbose%22%2C%3CBR%20%2F%3Edata%3A%20JSON.stringify(folderPayload)%2C%3CBR%20%2F%3Eheaders%3A%20%7B%3CBR%20%2F%3E%22Accept%22%3A%20%22application%2Fjson%3Bodata%3Dverbose%22%2C%3CBR%20%2F%3E%22Slug%22%3A%20listUrl%20%2B%20%22%2F%22%20%2B%20folderName%20%2B%20%22%7C%22%20%2B%20folderContentTypeId%3CBR%20%2F%3E%7D%2C%3CBR%20%2F%3Esuccess%3A%20function%20(data)%20%7B%3CBR%20%2F%3Esuccess(data.d)%3B%3CBR%20%2F%3E%7D%2C%3CBR%20%2F%3Eerror%3A%20error%3CBR%20%2F%3E%7D)%3B%3CBR%20%2F%3E%7D%2C%3CBR%20%2F%3Eerror)%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Efunction%20createDocumentSet(webUrl%2ClistName%2CfolderName%2C%20success%2C%20error)%3CBR%20%2F%3E%7B%3CBR%20%2F%3EcreateFolder(webUrl%2ClistName%2CfolderName%2C'0x0120D520'%2C%20success%2C%20error)%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EcreateDocumentSet(_spPageContextInfo.webAbsoluteUrl%2C'Documents'%2C'Orders'%2C%3CBR%20%2F%3Efunction(folder)%7B%3CBR%20%2F%3Econsole.log('Document%20Set%20'%20%2B%20folder.Name%20%2B%20'%20has%20been%20created%20succesfully')%3B%3CBR%20%2F%3E%7D%2C%3CBR%20%2F%3Efunction(error)%7B%3CBR%20%2F%3Econsole.log(JSON.stringify(error))%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3E)%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-39453%22%20slang%3D%22en-US%22%3ERe%3A%20Creating%20a%20Document%20Set%20with%20the%20Sharepoint%202013%20REST%20API%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-39453%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Kushan%2C%20thanks%20for%20the%20reply.%20Using%20the%20%3CSTRONG%3E%2F_api%2FWeb%2FFolders%2Fadd%3C%2FSTRONG%3E%20I%20can%20create%20a%20standard%20folder.%20I%20can%20then%20use%20the%20endpoint%20%3CSTRONG%3E%2F_api%2FWeb%2FLists%2FGetByTitle%3C%2FSTRONG%3E%20to%20modify%20the%20ContentTypeId%20to%20that%20of%20the%20Document%20set.%20The%20last%20piece%20of%20the%20puzzle%20seems%20to%20either%20be%20setting%20the%20ProgId%20to%20SharePoint.DocumentSet%2C%20or%20HTML_x0020_File_x0020_Type%20to%20SharePoint.DocumentSet%2C%20however%20both%20of%20these%20properties%20seems%20to%20resist%20being%20updated%20by%20REST.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-39446%22%20slang%3D%22en-US%22%3ERe%3A%20Creating%20a%20Document%20Set%20with%20the%20Sharepoint%202013%20REST%20API%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-39446%22%20slang%3D%22en-US%22%3E%3CP%3EYou%20may%20use%20following%20end%20point%20which%20allows%20POST%20request.%26nbsp%3B%3C%2FP%3E%3CPRE%3E%22%26lt%3Bapp%20web%20url%26gt%3B%2F_api%2FSP.AppContextSite(%40target)%2Fweb%0A%20%20%20%20%2Fgetfolderbyserverrelativeurl('%2FShared%20Documents')%2Ffolders%0A%20%20%20%20%3F%40target%3D'%26lt%3Bhost%20web%20url%26gt%3B'%22%3C%2FPRE%3E%3CP%3EIf%20you%20finding%20more%20information%20on%20this%2C%20following%20link%20would%20be%20beneficial.%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Foffice%2Fjj860569.aspx%23Reference%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EREST%20API%20Reference%20%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Is there (or does anyone at Microsoft know if there will be) support for creating Document Sets using the 2013 version of the REST API?

I've successfully used the 2010 /_vti_bin/listdata.svc/ service, but for future proofing, wonder if I should be moving on to something like like the /_api/web/folders endpoints.

 

Thanks,

Ross

3 Replies

You may use following end point which allows POST request. 

"<app web url>/_api/SP.AppContextSite(@target)/web
    /getfolderbyserverrelativeurl('/Shared Documents')/folders
    ?@target='<host web url>'"

If you finding more information on this, following link would be beneficial. 

REST API Reference

Hi Kushan, thanks for the reply. Using the /_api/Web/Folders/add I can create a standard folder. I can then use the endpoint /_api/Web/Lists/GetByTitle to modify the ContentTypeId to that of the Document set. The last piece of the puzzle seems to either be setting the ProgId to SharePoint.DocumentSet, or HTML_x0020_File_x0020_Type to SharePoint.DocumentSet, however both of these properties seems to resist being updated by REST.

best response confirmed by Ross McLean (Occasional Contributor)
Solution
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);
}



createDocumentSet(_spPageContextInfo.webAbsoluteUrl,'Documents','Orders',
function(folder){
console.log('Document Set ' + folder.Name + ' has been created succesfully');
},
function(error){
console.log(JSON.stringify(error));
}
);