0

In an event receiver, in itemcreated and not creating, i need to rename the spfolder created. I've tried with meta FileLeafRef, it works bu url are not updated.

SPFolder.item["FileLeafRef"] = "new name";  
SPFolder.item.Update();

Ho to achieve this

SPFolder.Url....
1
  • How are you getting the SPFolder instance from the SPlistitem properties? Can you paste the code you are using for that? Commented Mar 25, 2015 at 19:46

1 Answer 1

0

How to rename Folder using SSOM

public static class SPFolderExtensions
{
    public static void RenameFolder(this SPFolder folder, string name)
    {
        folder.Item[SPBuiltInFieldId.FileLeafRef] = name;
        folder.Item.Update();
    }
}

Usage:

using (var site = new SPSite(siteUrl))
{
     using (var web = site.OpenWeb())
     {
         var folder = web.GetFolder("/en/Documents/Archive");
         folder.RenameFolder("Archive 2012");
     }
} 
3
  • thanks for your solution but with this solution, folder.Url will change and how can have new folder url? Commented Mar 26, 2015 at 13:29
  • Hm.. in the example i have provided the Folder Url should be changed to "Archive 2012" (server relative url "/en/Documents/Archive 2012") Commented Mar 26, 2015 at 13:36
  • i've tried and url stayies old one but as it's a document set, i need to update url too because i'm creating file inside. thanks Commented Mar 26, 2015 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.