2

I'm currently using the following code to create a folder.

url: http://site url/_api/web/folders
method: POST
body: { '__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': '/document library relative url/folder name'}
Headers: 
Authorization: "Bearer " + accessToken
X-RequestDigest: form digest value
accept: "application/json;odata=verbose"
content-type: "application/json;odata=verbose"
content-length:length of post body

When I create the folder I can get the UniqueId from the response, but would also like to get the item number as I need this to perform another operation. The only way I can see to get this information is by changing the following:

request.Accept = "application/json;odata=fullmetadata";

I'm not sure if this is the correct way as it brings back a lot of information?

2 Answers 2

3

After the first query that creates Folder object you could perform the second REST request:

http://<sitecollection>/<site>/_api/web/folders/getbyurl(folderrelativeurl)/listItemAllFields

to retrieve the associated List Item with a Folder.

The following JavaScript example demonstrates that approach:

function executeJson(url,method,additionalHeaders,payload) 
{
    var headers = {};
    headers["Accept"] = "application/json;odata=verbose";
    if(method == "POST") {
        headers["X-RequestDigest"] = $("#__REQUESTDIGEST").val();
    }   
    if (typeof additionalHeaders != 'undefined') {
        for(var key in additionalHeaders){
            headers[key] = additionalHeaders[key];
        }    
    }    

    var ajaxOptions = 
    {       
       url: url,   
       type: method,  
       contentType: "application/json;odata=verbose",
       headers: headers
    };
    if(method == "POST") {
      ajaxOptions.data = JSON.stringify(payload);
    }  

    return $.ajax(ajaxOptions);
}

function createFolder(webUrl,folderUrl) 
{    
    var url = webUrl + "/_api/web/folders";   
    var folderPayload = { '__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': folderUrl}; 
    return executeJson(url,'POST',null,folderPayload).then(function(data){
            var url = webUrl + "/_api/web/GetFolderByServerRelativeUrl('" + folderUrl  + "')/ListItemAllFields";
            return executeJson(url,'GET');
        });
}

Usage

createFolder(_spPageContextInfo.webAbsoluteUrl,'/Shared Documents/Archive')
.done(function(data)
{
    var folderItem = data.d;
    console.log(folderItem.Id); //print ListItem.Id property
})
.fail(
function(error){
    console.log(JSON.stringify(error));
});
0

I've found using the following GetByUrl or GetFolderByServerRelativeUrl with a number of folders can lead to the below.

"The length of the URL for this request exceeds the configured maxUrlLength value."

By using GetFolderById('guid')/listItemAllFields I was able to accomplish my task of assigning permissions and updating a field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.