Solved

Rename Document Library Document using Client Object Model

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

Suggested Solutions

Last week I faced a strange issue recently, i have deployed SharePoint 2003 servers for one project and one of the requirements was to open SharePoint site from same server. when i was trying to open site from the same server i was getting authentic…
The vision: A MegaMenu for a SharePoint portal home page The mission: Make it easy to maintain. Allow rich content and sub headers as well as standard links. Factor in frequent changes without involving developers or a lengthy Dev/Test/Prod rel…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

26 Experts available now in Live!

Get 1:1 Help Now