Tuesday, July 14, 2015

Create Folder in SharePoint using REST

Here is the quick code snippet to create a folder in a Library or List using SharePoint REST.
var docLibraryName = "DocLibrary";
var folderName = "FolderTest";
var appWebUrl = "http://weburl";

var folderRelativePath = ''; //Populate with the path after folder creation to add files to folder
var folder = CreateFolder(appWebUrl, docLibraryName, folderName);
if (folder && folder.statusText && folder.statusText == 'Created') {
    if (folder.responseJSON && folder.responseJSON.d) {
        folderRelativePath = folder.responseJSON.d.ServerRelativeUrl;

////Method to create a folder and returns folder object with metadata
CreateFolder = function (url, libraryname, foldername) {
    var folderRelPath = libraryname + '/' + foldername;
    var requestUri = url + "/_api/web/folders";
    var data = $.ajax({
        url: requestUri,
        type: "POST",
        async: false,
        data: JSON.stringify({ '__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': folderRelPath }),
        headers: {
            "accept": "application/json; odata=verbose",
            "content-type": "application/json; odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        success: function (data) {
            alert('Folder created successfully.');
            return data;
        error: function (error) {
            alert('Folder creation failed!');
            return data;
    return data;
SharePoint REST Create Folder in Library
Creating Folder in Library using REST
REST Create Folder in SharePoint Library

No comments:

Post a Comment