Solved

Rename Document Library Document using Client Object Model

Posted on 2011-03-01
5
1,912 Views
Last Modified: 2012-05-11
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
Comment
Question by:tkendall57
5 Comments
 
LVL 6

Expert Comment

by:sabby447
ID: 35020433
0
 
LVL 3

Author Comment

by:tkendall57
ID: 35022821
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
 
LVL 1

Accepted Solution

by:
lpd123 earned 500 total points
ID: 35772384
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
 
LVL 12

Expert Comment

by:Hairbrush
ID: 37693391
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Work Over Net is a new and very powerful collaboration product. With its new easy interface it is becoming very competitive to other similar products like webex and office interactive. WON 2010 have the standard business tools needed for multi-offic…
I used to be SharePoint evangelist in our company, so my Outlook always full of questions about how to do this, or where I can find that. One day I found such an email with the following question: "how to attach 3-State workflow (one of the workflow…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now