4

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>
el shorty
  • 237
  • 2
  • 5
  • 14

1 Answers1

5

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();
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
  • Yes, I thought about that to but the name attribute here has no setter. So I won't be able to change the folder name. – el shorty May 26 '15 at 13:07
  • So, if your goal is to rename a folder, then you could check the updated answer ;) – Vadim Gremyachev May 26 '15 at 13:33
  • I would but I seem to be missing a reference in order to user your code. I get the following error: microsoft.sharepoint.client.folder does not contain a definition for ListItemAllFields and no extension method 'ListItemAllFields' accepting a first argument could be found. I have added the Microsoft.sharepoint.client & Microsoft.sharepoint.client.runtime dll's to my project. – el shorty May 27 '15 at 05:28
  • @elshorty, yes, the reference is not missing, that code is for sharepoint 2013 and above, which does not work for sharepoint 2010 as tagged in op. – oshirowanen May 18 '17 at 13:19
  • This page is the top result on google for "sharepoint caml query get folder by name" and does not actually answer the question. Can we get an answer that addresses the question as asked or maybe change the original question to match the answer? – hamboy Mar 21 '22 at 18:44