2

I am trying to create a folder inside a custom list using REST API with with type SP.Folder. I am able to create the folder successfully. Below is the code i used. enter image description here English folder has been created successfully under Mech1. And i can see its relative path using REST Client. But i can't able to see the same in sharepoint list UI. enter image description here

Any help on this will be appreciable.

2

When you add a folder in SharePoint using the Web UI, it actually adds two things: the folder and a list item. It's the list item that shows up in the List View.

Also, there are two names. The name of the list item and the name of the folder. You set the name of the list item by setting the Title field value and you set the name of the folder by setting the FileLeafRef field value.

When you add a folder using the REST API, it just adds the folder.

I've gotten folder creation to work but it's sort of a hack. I add the list item and set the content type to folder. This adds the list item and the folder. The problem is that trying to set the name of the folder on creation does not work. You need to create the list item and then set the name of the folder as a second step.

I've reported this as an issue on the Office 365 Developer User Voice: Improve Folder support in the REST API

function createFolder() {
    UpdateFormDigest(_spPageContextInfo.webServerRelativeUrl, _spFormDigestRefreshInterval);

    var appUrl = GetUrlKeyValue("SPAppWebUrl");
    var hostUrl = GetUrlKeyValue("SPHostUrl");

    var url = appUrl + "/_api/SP.AppContextSite(@target)/Web/Lists/getByTitle('Demo')/Items?@target='" + hostUrl + "'";

    var call = createFolderInternal().then(renameFolder);
    call.done(function (data, textStatus, jqXHR) {
        var message = jQuery("#message");
        message.text("Folder added");
    });
    call.fail(failHandler);

    function createFolderInternal() {
        var url = appUrl + "/_api/SP.AppContextSite(@target)/Web/Lists/getByTitle('Demo')/Items?@target='" + hostUrl + "'";

        var call = jQuery.ajax({
            url: url,
            type: "POST",
            data: JSON.stringify({
                "__metadata": { type: "SP.Data.DemoListItem" },
                Title: "Test Folder",
                FileLeafRef: "Test Folder",     // No effect here
                FileSystemObjectType: SP.FileSystemObjectType.folder,
                ContentTypeId: "0x0120"
            }),
            headers: {
                Accept: "application/json;odata=verbose",
                "Content-Type": "application/json;odata=verbose",
                "X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
            }
        });

        return call;
    }

    function renameFolder(data) {
        var url = appUrl + "/_api/SP.AppContextSite(@target)/Web/Lists/getByTitle('Demo')/Items('" + data.d.Id + "')?@target='" + hostUrl + "'";

        var call = jQuery.ajax({
            url: url,
            type: "POST",
            data: JSON.stringify({
                "__metadata": { type: "SP.Data.DemoListItem" },
                Title: "Test Folder",
                FileLeafRef: "Test Folder"
            }),
            headers: {
                Accept: "application/json;odata=verbose",
                "Content-Type": "application/json;odata=verbose",
                "X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
                "IF-MATCH": "*",
                "X-Http-Method": "PATCH"
            }
        });
    }
}
0

Not sure if it is different for folders in Custom List SPLists as opposed to Document Library SPLists but using the below code works for Document Libraries by using the SP.Folder.folders collection .add() endpoint (MSDN link: https://msdn.microsoft.com/en-us/library/office/jj246631.aspx)

Using the following code:

var folderName = 'TestFolder';    
var url = siteUrl + '/_api/web/folders/getbyurl(\''+folderName+'\')/folders'

$.ajax({
    method: 'POST',
    url: url,
    headers: {
        'Accept': 'application/json; odata=verbose',
        'Content-Type': 'application/json; odata=verbose',
        'X-RequestDigest' : $("#__REQUESTDIGEST").val()
    },
    data: JSON.stringify(
    {'__metadata': { 'type': 'SP.Folder' }, 
        'Name': folderName,
        'ServerRelativeUrl': siteUrl + '/' + folderName + '/' + folder, 
    })
})
.done(function(result){
    // Something

})
.fail(function(error){        
    // Something
});    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.