如何使用 CamlQuery 按名称获取共享点文件夹

How to get sharepoint folder by name using CamlQuery

所以我需要重命名 crm 插件中的共享点文件夹。该文件夹的名称是来自联系人的全名,但如果名称中有拼写错误,则还需要一个插件来更改共享点中的文件夹名称。我找到了一种方法来执行此操作,但要执行此操作,我需要获取该文件夹,并且我正在尝试使用 CamlQuery 来执行此操作。 (这是一个共享点 2010)

这是我获取文件夹的方法:

            ClientContext clientContext = new ClientContext(siteUrl);
            clientContext.Credentials = new NetworkCredential(login, password);
            Web web = clientContext.Web;
            List list = web.Lists.GetByTitle(listName);

            string FolderFullPath = siteUrl + "contact/" + folderName;

            CamlQuery query = new CamlQuery();
            query.ViewXml = "<View Scope=\"RecursiveAll\"> " +
                            "<Query>" +
                                "<Where>" +
                                    "<And>" +
                                        "<Eq>" +
                                            "<FieldRef Name=\"FSObjType\" />" +
                                            "<Value Type=\"Integer\">1</Value>" +
                                         "</Eq>" +
                                          "<Eq>" +
                                            "<FieldRef Name=\"Title\"/>" +
                                            "<Value Type=\"Text\">" + folderName + "</Value>" +
                                          "</Eq>" +
                                    "</And>" +
                                 "</Where>" +
                            "</Query>" +
                            "</View>";

            if (relativePath.Equals(string.Empty))
            {
                query.FolderServerRelativeUrl = "/lists/" + listName;
            }
            else
            {
                query.FolderServerRelativeUrl = "/lists/" + listName + "/" + relativePath;
            }
            var folders = list.GetItems(query);

            clientContext.Load(list);
            clientContext.Load(list.Fields);
            clientContext.Load(folders, fs => fs.Include(fi => fi["Title"],
                fi => fi["DisplayName"],
                fi => fi["FileLeafRef"]));
            clientContext.ExecuteQuery();

但我不断收到错误消息:"Value does not fall within the expected range"。

这是日志:

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Value does not fall within the expected range.Detail: 
<OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
  <ErrorCode>-2147220891</ErrorCode>
  <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
    <KeyValuePairOfstringanyType>
      <d2p1:key>OperationStatus</d2p1:key>
      <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">0</d2p1:value>
    </KeyValuePairOfstringanyType>
  </ErrorDetails>
  <Message>Value does not fall within the expected range.</Message>
  <Timestamp>2015-05-26T07:16:43.90779Z</Timestamp>
  <InnerFault i:nil="true" />

</OrganizationServiceFault>

由于您使用的是 SharePoint CSOM API,我建议使用 Web.GetFolderByServerRelativeUrl Method 获取位于指定位置的文件夹对象服务器相关 Url

例子

var folderUrl = "Lists/Discussions/2013"; //folder named 2013 located in Discussions list
using (var ctx = new ClientContext(webUri))
{
   var folder = ctx.Web.GetFolderByServerRelativeUrl(folderUrl);
   ctx.Load(folder);
   ctx.ExecuteQuery();
}

如何使用 SharePoint 2010 CSOM 重命名文件夹API

以下示例演示了如何重命名文件夹:

public static class FolderExtensions
{
    public static void RenameFolder(this Folder folder,string name)
    {
        var folderItem = folder.ListItemAllFields;
        folderItem["Title"] = name;
        folderItem["FileLeafRef"] = name;
        folderItem.Update();
    }

}

用法

var folder = ctx.Web.GetFolderByServerRelativeUrl(folderUrl);
folder.RenameFolder("Archive");  //<-set new folder name here
ctx.ExecuteQuery();