How to get sharepoint folder by name using CamlQuery

el shorty picture el shorty · May 26, 2015 · Viewed 8.9k times · Source

So I need to rename a sharepoint folder in a plugin from crm. The name of the folder is a fullname from a contact but in case of a typo in the name there needs to be a plugin to change the folder name in sharepoint aswell. I found a way to do this but to do this I need to get the folder and I'm trying to do this with CamlQuery. (it is a sharepoint 2010)

Here is what I do to get the folder:

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

But I keep getting the error message: "Value does not fall within the expected range".

Here is the log:

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>

Answer

Vadim Gremyachev picture Vadim Gremyachev · May 26, 2015

Since you are using SharePoint CSOM API, i would recommend to utilize Web.GetFolderByServerRelativeUrl Method for getting folder object located at the specified server-relative Url

Example

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

How to rename a folder using SharePoint 2010 CSOM API

The following example demonstrates how to rename a folder:

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

}

Usage

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