Forum Discussion

Ross McLean's avatar
Ross McLean
Copper Contributor
Jan 16, 2017
Solved

Creating a Document Set with the Sharepoint 2013 REST API

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

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

  • 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

    • Ross McLean's avatar
      Ross McLean
      Copper Contributor

      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.

      • Clint Lechner's avatar
        Clint Lechner
        Steel Contributor
        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));
        }
        );

Resources