• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2216
  • Last Modified:

Rename Document Library Document using Client Object Model

Does anyone have and example of renaming a document in a SharePoint 2010 document library and folder using the SharePoint 2010 Client Object Model (Microsoft.SharePoint.Client.File)?

I've tried setting the fields Title, FileRef and FileDirRef to the new name. I've even tried a FIle.MoveTo (which I really don't want to do I just want to rename the file/title).

Thanks,
TK
0
tkendall57
Asked:
tkendall57
1 Solution
 
tkendall57Author Commented:
I was hoping for an example using the Microsoft.SharePoint.Client or Microsoft.SharePoint.Silverlight library and not the lower-level Microsoft.SharePoint (which can't be used in Silverlight).

0
 
lpd123Commented:
hi i'm using this for renaming, hope it can help you or give you some ideas

cheers
Lars

------Code--
      public Boolean RenameFolder(string siteurl, string groupname, string libname, string RelRoot, string foldername, string newname)
        {

            using (ClientContext clientContext = new ClientContext(siteurl))
            {
                try
                {
                    clientContext.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication;
                    clientContext.FormsAuthenticationLoginInfo = _formsLoginInfo;
                    List oList = clientContext.Web.Lists.GetByTitle(libname);
                    ListItemCollection collListItem;
                    string prefix = ConfigurationManager.AppSettings["FolderPrefix"].ToString();



                    CamlQuery camlQuery = new CamlQuery();
                    // .ViewXml = "<View><RowLimit>100</RowLimit></View>";
                    if (RelRoot != "") RelRoot = "/" + RelRoot;
                    Boolean libdelete = (foldername == libname);

                    if (libdelete)
                    {
                        oList.Title = newname;
                        oList.Update();
                        clientContext.ExecuteQuery();
                        return true;
                    }
                    else
                    {
                        camlQuery.FolderServerRelativeUrl = prefix + groupname + "/" + libname + RelRoot;
                    }
                    collListItem = oList.GetItems(camlQuery);
                    clientContext.Load(collListItem, items => items.IncludeWithDefaultProperties(item => item.DisplayName, item => item.File.ServerRelativeUrl));
                    clientContext.ExecuteQuery();

                    foreach (ListItem _item in collListItem)
                    {
                        if ((_item.FileSystemObjectType == FileSystemObjectType.Folder) && (string.Compare(_item.DisplayName, foldername, true) == 0))
                        {

                            _item["FileLeafRef"] = newname;
                            _item["Title"] = newname;
                            _item.Update();
                            clientContext.ExecuteQuery();
                            TCDebug.DebugFile.PostDebugEvent("Rename folder", "From " + foldername + " to " + newname, DebugFormName, _username);
                            return true;
                        }
                    }

                    return false;
                }
                catch (Exception e)
                {
                    TCDebug.DebugFile.PostDebugEventException("Renamefolder", e.Message, DebugFormName, _username);
                    return false;
                }
            }
        }
0
 
HairbrushCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Tackle projects and never again get stuck behind a technical roadblock.
Join Now