locked
Rename/Move folder including files and subfoders using Client API RRS feed

  • Question

  • Hi, 

    I have a document library with folders tree and this is synchronized with the local computer. If I rename local folder, I'd need to rename also the Sharepoint folder. I haven't been able to find any function for this and only what I was able to do was something like this:

    1) create new folder

    2) move all files one by one calling File.MoveTo()

    3) delete original folder

    in the code bellow there is missing the processing of all subfolders and theis subfolders etc. 

    If I rename the root folder, it need a huge amout of calls and it's very very complicated and "expensive" 

    Isn't there some better - simple way how to rename the folder on SHarepoint using Client API? It will be always only in one library, no movement outside of it.

    Thanks a lot

    Filip

    ublic Folder MoveFolder(string fromFolderPath, string toFolderPath)
            {
                try
                {
                    
                    var origFolder = GetFolderByUrl(fromFolderPath);
                    var newFolder = Library.RootFolder.Folders.Add(toFolderPath);
     
                    var origFolderFiles = origFolder.Files;
                    _ctx.Load(origFolderFiles, fs => fs.Include(f => f.Name));
                    _ctx.Load(newFolder, f => f.Name, f => f.ServerRelativeUrl);
     
                    _ctx.ExecuteQuery();
     
                    string serverUrl = "http://" + new Uri(_webUrl).Host;
     
                    foreach (var f in origFolderFiles )
                    {
                        string newfilePath = serverUrl + toFolderPath + "/" + f.Name ;
                        f.MoveTo(newfilePath, MoveOperations.Overwrite);
                        _ctx.ExecuteQuery();
                    }
     
                    origFolder.DeleteObject();
                    _ctx.ExecuteQuery();
     
                    return newFolder;
     
                }
                catch (Exception ex)
                {
                    
                    throw;
                }
            }

    Monday, September 24, 2012 10:48 AM

Answers

  • The following CSOM code works by renaming the BaseName field, but it throws an error stating the BaseName field does not exist but it actually does.

    public static void UpdateFolderName()
    {
    
                ClientContext clientContext = new ClientContext("http://basesmc2008");
    
    
                List list = clientContext.Web.Lists.GetByTitle("tester2");
                FieldCollection fields = list.Fields;
    
                CamlQuery camlQueryForItem = new CamlQuery();
                camlQueryForItem.ViewXml = @"<View>
                                        <Query>
                                            <Where>
                                                <Eq>
                                                    <FieldRef Name='ID'/>
                                                    <Value Type='Counter'>22</Value>
                                                </Eq>
                                            </Where>
                                        </Query>
                                    </View>";
    
    
                ListItemCollection listItems = list.GetItems(camlQueryForItem);
    
    
    
                clientContext.Load(listItems, items => items.Include
                                                (item => item["BaseName"],
                                                 item => item["Name"],
                                                 item => item["Title"]));
    
                clientContext.ExecuteQuery();
    
                ListItem itemToUpdate = listItems[0];
             
                itemToUpdate["BaseName"] = "newfoldername3";
    
                itemToUpdate.Update();
                clientContext.Load(itemToUpdate);
                clientContext.ExecuteQuery();
    
    
    }


    Blog | SharePoint Field Notes Dev Tool | ClassMaster

    • Marked as answer by Lhan Han Thursday, October 4, 2012 3:14 AM
    Monday, September 24, 2012 9:33 PM
  • It is up to you if you want to use the code. The code successfully renames the folder but it throws an exception. You can trap the exception.

    Blog | SharePoint Field Notes Dev Tool | ClassMaster

    • Marked as answer by Lhan Han Thursday, October 4, 2012 3:14 AM
    Tuesday, September 25, 2012 3:48 PM

All replies

  • The following CSOM code works by renaming the BaseName field, but it throws an error stating the BaseName field does not exist but it actually does.

    public static void UpdateFolderName()
    {
    
                ClientContext clientContext = new ClientContext("http://basesmc2008");
    
    
                List list = clientContext.Web.Lists.GetByTitle("tester2");
                FieldCollection fields = list.Fields;
    
                CamlQuery camlQueryForItem = new CamlQuery();
                camlQueryForItem.ViewXml = @"<View>
                                        <Query>
                                            <Where>
                                                <Eq>
                                                    <FieldRef Name='ID'/>
                                                    <Value Type='Counter'>22</Value>
                                                </Eq>
                                            </Where>
                                        </Query>
                                    </View>";
    
    
                ListItemCollection listItems = list.GetItems(camlQueryForItem);
    
    
    
                clientContext.Load(listItems, items => items.Include
                                                (item => item["BaseName"],
                                                 item => item["Name"],
                                                 item => item["Title"]));
    
                clientContext.ExecuteQuery();
    
                ListItem itemToUpdate = listItems[0];
             
                itemToUpdate["BaseName"] = "newfoldername3";
    
                itemToUpdate.Update();
                clientContext.Load(itemToUpdate);
                clientContext.ExecuteQuery();
    
    
    }


    Blog | SharePoint Field Notes Dev Tool | ClassMaster

    • Marked as answer by Lhan Han Thursday, October 4, 2012 3:14 AM
    Monday, September 24, 2012 9:33 PM
  • Hi Steve,

    I'm little bit confused, why do you answer me by the code which throws an exception? Or ...?

    Filip

    Tuesday, September 25, 2012 3:41 PM
  • It is up to you if you want to use the code. The code successfully renames the folder but it throws an exception. You can trap the exception.

    Blog | SharePoint Field Notes Dev Tool | ClassMaster

    • Marked as answer by Lhan Han Thursday, October 4, 2012 3:14 AM
    Tuesday, September 25, 2012 3:48 PM