2

I was wondering whether it is possible to programmatically change the name of an SPFolder after it has been created?

e.g.

foreach (SPFolder folder in list.RootFolder.SubFolders)
{
    if (folder.Name.Equals("blah"))
    {
        // set the name of the folder to something else
        folder.Name = "blah 2.0";
    }
}

Googling so far suggested that MoveTo is the only way of doing so. There are a lot of items inside the folder so I'm reluctant to moving it unless there is absolutely no other ways.

Thanks.

BeraCim
  • 2,317
  • 8
  • 49
  • 78

3 Answers3

2

I ended up using MoveTo as there was no other ways of doing this.

BeraCim
  • 2,317
  • 8
  • 49
  • 78
1

In a Document Library the field Name of an item (folder) has StaticName = FileLeafRef. So what really worked for me is

folder.Item[SPBuiltInFieldId.FileLeafRef] = "The new name";
folder.Item.Update();
Maurizio Pozzobon
  • 3,044
  • 7
  • 34
  • 44
0

when you have an SPFolder object, you can do it like this:

folder.item["Title"] = "blah 2.0";
folder.item.SystemUpdate();'
naivists
  • 32,681
  • 5
  • 61
  • 85
  • How does ["Title"] differ from ["Name"] or ["BaseName"]? And how would SystemUpdate() differ from Update()? – Scott Stafford Sep 10 '10 at 16:33
  • `Title` is the most popular field in SharePoint, since every content type has it. BaseName and Name are fields specific to folders or files, I believe. – naivists Sep 11 '10 at 07:15
  • SystemUpdate differs from the regular Update as it does not change the "Last modified" and "Modified by" values. Hence, this is the best way to change it from the code, if the change does not have a significant business value. – naivists Sep 11 '10 at 07:16
  • You can find a good reference of built-in field names here: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spbuiltinfieldid_members.aspx – naivists Sep 11 '10 at 07:32