6

I'm having trouble renaming a folder inside a document library using the REST api provided by SharePoint 2013. Here is the code I'm using below.

string digest = String.Empty;
using (var response = await connector.HttpClient.PostAsync("_api/contextinfo", null, token)) 
{
    response.EnsureSuccessStatusCode();
    var obj = await response.ReadObject("d");
    digest = obj["GetContextWebInformation"].Value<string>("FormDigestValue");
}

using (var request = new HttpRequestMessage(HttpMethod.Post, String.Format("/_api/Web/GetFolderByServerRelativeUrl('{0}')", operation.Path.FullName))) 
{
    request.Headers.Add("X-HTTP-Method", "MERGE");
    request.Headers.Add("IF-MATCH", "*");
    request.Headers.Add("X-RequestDigest", digest);

    //{ '__metadata': { 'type': 'SP.Folder' }, 'Name': 'New name' }
    dynamic obj = new JObject();
    obj.__metadata = new JObject();
    obj.__metadata.type = "SP.Folder";
    obj.Name = operation.DesiredName;

    request.Content = new ODataJObjectContent(obj);

    using (var response = await connector.HttpClient.SendAsync(request, token)) 
    {
        response.EnsureSuccessStatusCode();
        await response.ReadText();
    }
}

In Fiddler here is the request:

POST http://2013.blah.com/_api/Web/GetFolderByServerRelativeUrl('/Shared%20Documents/Test') HTTP/1.1
X-HTTP-Method: MERGE
IF-MATCH: *
X-RequestDigest: 0xA7C057B3AECE805B7313909570F64B8EACD7A677014B8EBE7F75CC5A7C081F87973D94E7CC22346964ECAB1FE3C6B326DA3B67DF7A646FE6F47E9B1E686C3985,11 Apr 2013 15:13:05 -0000
Accept: application/json; odata=verbose
Content-Type: application/json; odata=verbose
Host: 2013.skysync.com
Content-Length: 50
Expect: 100-continue

{"__metadata":{"type":"SP.Folder"},"Name":"Test2"}

And then the response:

HTTP/1.1 204 No Content
Cache-Control: private, max-age=0
Expires: Wed, 27 Mar 2013 15:13:15 GMT
Last-Modified: Thu, 11 Apr 2013 15:13:15 GMT
Server: Microsoft-IIS/8.0
X-SharePointHealthScore: 0
SPClientServiceRequestDuration: 15
X-AspNet-Version: 4.0.30319
SPRequestGuid: 53bd109c-43bb-2064-4a1b-82298b670ece
request-id: 53bd109c-43bb-2064-4a1b-82298b670ece
X-RequestDigest: 0x9CDB4F31CC5F3877C4383657C12BEC6CFF10FC28AB6A0BB2D9D38B4279187CBD1450359BDFF07F0E63FF550BFF96C46E0476FB895CDA104348AC066D86246BC6,11 Apr 2013 15:13:15 -0000
X-FRAME-OPTIONS: SAMEORIGIN
X-Powered-By: ASP.NET
MicrosoftSharePointTeamServices: 15.0.0.4420
X-Content-Type-Options: nosniff
X-MS-InvokeApp: 1; RequireReadOnly
Date: Thu, 11 Apr 2013 15:13:15 GMT

Everything looks good until I go back to SharePoint and the Test folder is still the same name. I'm following the guidelines from here and I've seen other very similar examples. I can rename it through the interface without any problem.

Thanks in advance for any help!

Mo Patel
  • 2,321
  • 4
  • 22
  • 37
TroyC
  • 482
  • 1
  • 6
  • 8
  • Going to be a permissions problem. – Jordan Apr 11 '13 at 15:25
  • 1
    @Jordan I able to delete folders though, I know it's possible but I'd think if it was a permissions issue I wouldn't be able to delete either. – TroyC Apr 11 '13 at 15:56
  • Adding one more comment..I can rename using the old SharePoint asmx services, which is what I'm doing now. Funny enough renaming a List works using the above method. – TroyC Apr 29 '13 at 04:40
  • Did you ever solve this? i am seeing exactly the same behaviour and then everything on the net says i need an etag from that http 204 response, but there is no ETag! If i use etag from /ListItemAllFields then it's of no use to me.... – zaitsman Jun 10 '13 at 09:03
  • @zaitsman No, I ended up dropping back to the ASMX services to get this to work. Very frustrating. – TroyC Jun 10 '13 at 13:20
  • 2
    @TroyC, I think MS announced somewhere that they will be deprecating asmx services altogether in the next release. Hope they get REST working properly before that... – zaitsman Jun 11 '13 at 03:03
  • @TroyC Hello! Did you got any solution for this? I am also facing same issue. – Mihir Jul 01 '14 at 11:27
  • @Mihir Unfortunately, no. I've had to band-aid their entire REST API will fallbacks to either SCOM or ASMX calls. It's pretty awful, really. – TroyC Jul 02 '14 at 18:30

2 Answers2

1

The following example demonstrates how to rename Folder via SharePoint 2013 REST service

Scenario: rename Archive folder to 2015 located in Documents library

using (var client = new SPHttpClient(webUri, userName, password))
{
    RenameFolder(client, webUri.ToString(),"Documents/Archive","2015");
}

where

    private static void RenameFolder(SPHttpClient client, string webUrl,string folderUrl,string folderName)
    {
        var folderItemUrl = webUrl + "/_api/web/GetFolderByServerRelativeUrl('" + folderUrl + "')/ListItemAllFields";
        var data = client.ExecuteJson(folderItemUrl);

        var itemPayload = new {
            __metadata = new { type = data["d"]["__metadata"]["type"] },
            Title = folderName,
            FileLeafRef = folderName,

        };
        var  itemUrl = data["d"]["__metadata"]["uri"];
        var headers = new Dictionary<string, string>();
        headers["IF-MATCH"] = "*";
        headers["X-HTTP-Method"] = "MERGE";
        client.ExecuteJson((string)itemUrl, HttpMethod.Post, headers, itemPayload);
    }

Note:

  • SPHttpClient class - inherits from HttpClient and provides some additional SharePoint specific functionaly such as getting request digest
  • SPHttpClientHandler class - hides all the intricacies related to SharePoint Online authentication
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
0

Try to add list in your manifest file. It seems like a permissions problem, and when you have to "trust" an application then choose the list which you want to operate with.

Jens Erat
  • 37,523
  • 16
  • 80
  • 96
Sebastian 506563
  • 6,980
  • 3
  • 31
  • 56
  • 1
    Very wrong - this may be invoked on any side, including client side (such is my case). The language may be anything and you may not even have a 'manifest'. As far as permissions are concerned, the author confirmed he can delete the folders and same here - i just can't rename using this API. – zaitsman Jun 10 '13 at 09:04