如何使用CamlQuery按名称获取sharepoint文件夹



所以我需要重命名一个sharepoint文件夹从crm插件。文件夹的名称是联系人的全名,但如果名称中出现错别字,则需要一个插件来更改sharepoint中的文件夹名称。我找到了一种方法来做到这一点,但要做到这一点,我需要得到文件夹,我正试图用CamlQuery做到这一点。(这是sharepoint 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();

但是我一直得到错误信息:"值不落在预期范围内"。

日志如下:

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方法获取位于指定服务器相对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();

相关内容

  • 没有找到相关文章

最新更新