5

I am using REST to update folder name via sharepoint apps. My code as follows:

executor = new SP.RequestExecutor(appweburl);
executor.executeAsync({
        url: appweburl + "/_api/SP.AppContextSite(@target)/web/GetFolderByServerRelativeUrl('" + libraryToSearch + "/" + oldFolderName +"')?@target='" + mysiteUrl + "'";
        headers: {
            Accept: "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-HTTP-Method": "MERGE",
            "IF-MATCH":"*"
        },
        method: "POST",
        body: '{ "__metadata": { "type": "SP.Folder" }, "Name": "Test Update" }',        
        success: FolderNameUpdateSuccess,
        error: FolderNameUpdateFailure
    });

Finally it is coming to the success block, but folder name is not updating... I searched for long time but no result. What could be the problem here? In success block data is retreiving like below. enter image description here

Status Text comes out as "No Content", what does it mean?

8
  • What's the value of libraryToSearch? Does it include the initial slash (/)? Server relative url's begin with a slash. If the url is as follows: Documents/MyFolderToRename, it will fail. However, /Documents/MyFolderToRename will not.
    – wjervis
    Jul 1, 2014 at 12:32
  • That url is correct becuase it i am using same format for another operations like get, delete. However format is Documents/MyFolderToRename
    – Mihir
    Jul 1, 2014 at 13:02
  • When I try to update the welcomepage for a folder it is updating but only for Name it is not working? What could be the reason?
    – Mihir
    Jul 1, 2014 at 13:02
  • Hmm, how misleading, did not know site relative url's were valid (no leading slash means site relative). This is a stab in the dark, but try adding /ListItemAllFields to the end, and changing Title.
    – wjervis
    Jul 1, 2014 at 13:21
  • You mean to say, I have to add like /Documents/MyFolderToRename ?
    – Mihir
    Jul 1, 2014 at 13:57

1 Answer 1

7

SP.Folder.name property is read-only and hence it could not be used in update operation.

How to rename Folder using SharePoint 2013 REST

The following example demonstrates how to rename Folder using SharePoint 2013 REST API:

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 renameFolder(webUrl,folderUrl,name)
{
     var folderItemUrl =  webUrl + "/_api/web/GetFolderByServerRelativeUrl('" + folderUrl + "')/ListItemAllFields";
     return executeJson(folderItemUrl,"GET").then(
         function(data){         
             var itemPayload = {};
             itemPayload['__metadata'] = {'type': data.d['__metadata']['type']};
             itemPayload['Title'] = name;
             itemPayload['FileLeafRef'] = name;
             var itemUrl = data.d['__metadata']['uri'];
             var additionalHeaders = {};
             additionalHeaders["X-HTTP-Method"] = "MERGE";
             additionalHeaders["If-Match"] =  "*";
             return executeJson(itemUrl,"POST",additionalHeaders,itemPayload);
         });
}

Usage

How to rename folder named 2014 located under Documents library to Archive:

renameFolder(_spPageContextInfo.webAbsoluteUrl,'/Shared Documents/2014','Archive')
.done(function()
{
    console.log('Folder has been renamed');
})
.fail(
function(error){
    console.log(JSON.stringify(error));
});

Update

How to rename Folder using SharePoint 2013 REST (ver 2)

function renameFolder(webUrl,listTitle,itemId,name)
{
     var itemUrl =  webUrl + "/_api/Web/Lists/GetByTitle('" + listTitle + "')/Items(" + itemId + ")";
     var itemPayload = {};
     itemPayload['__metadata'] = {'type': getItemTypeForListName(listTitle)};
     itemPayload['Title'] = name;
     itemPayload['FileLeafRef'] = name;
     var additionalHeaders = {};
     additionalHeaders["X-HTTP-Method"] = "MERGE";
     additionalHeaders["If-Match"] =  "*";
     return executeJson(itemUrl,"POST",additionalHeaders,itemPayload);
}


function getItemTypeForListName(name) {
   return"SP.Data." + name.charAt(0).toUpperCase() + name.slice(1) + "ListItem";
}
5
  • Thanks and I will try your solution. But why microsoft is giving the supported method as like this msdn.microsoft.com/en-us/library/office/… ?
    – Mihir
    Nov 17, 2014 at 10:57
  • You could even use this approach if Folder contains files ;) Nov 17, 2014 at 11:21
  • I have one doubt. When these then and done functions should be use? Can be use without deferred object? Because I didn't saw any deferred object reference with your example.
    – Mihir
    Nov 17, 2014 at 11:50
  • The purpose of usage deferred.then() since in my example 2 requests are called: 1) get ListItem associated with Folder resource using /_api/web/GetFolderByServerRelativeUrl(serverrelativeurl)/ and 2)update ListItem resource. But it could be performed using a single REST request as demonstrated in the updated answer. Nov 17, 2014 at 12:13
  • Okay.. do you know how to clear the cache for the deferred object. For two requests I am getting the same response.
    – Mihir
    Nov 17, 2014 at 12:19

Your Answer

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

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