6

我在使用 SharePoint 2013 提供的 REST api 重命名文档库中的文件夹时遇到问题。这是我在下面使用的代码。

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();
    }
}

在提琴手这里是请求:

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"}

然后是回应:

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

在我返回 SharePoint 之前,一切看起来都很好,并且 Test 文件夹的名称仍然相同。我正在遵循此处的指南,并且我还看到了其他非常相似的示例。我可以通过界面重命名它,没有任何问题。

提前感谢您的帮助!

4

2 回答 2

1

以下示例演示如何通过 SharePoint 2013 REST 服务重命名文件夹

场景:将Archive文件夹重命名为2015位于Documents 库中

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

在哪里

    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);
    }

笔记:

  • SPHttpClient 类- 继承自HttpClient并提供一些额外的 SharePoint 特定功能,例如获取请求摘要
  • SPHttpClientHandler 类- 隐藏与 SharePoint Online 身份验证相关的所有复杂问题
于 2015-10-01T22:06:16.060 回答
0

尝试在清单文件中添加列表。这似乎是一个权限问题,当您必须“信任”一个应用程序时,请选择您要操作的列表。

于 2013-04-12T10:34:53.793 回答