Solved

Rename Document Library Document using Client Object Model

Posted on 2011-03-01
5
1,970 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
The Scenario: Let’s say you have a quote worksheet in Excel that you use to work up sales figures and such for your clients. You utilize SharePoint to manage and keep track of these documents. You would like values from your worksheet to populate Sh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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