1

I have tried this:

SPFolder folder = ...;
folder.Item["Name"] = newName;
folder.Item.Update();

And it behaves mysteriously. If I run it, it throws an exception:

 SPException: Cannot complete this action.

HOWEVER, if I stop it in the debugger after the new Name assignment and before the Update(), and look at the properties of folder.Item, then continue, it works every time. It's not a timing thing, I tried stopping it in the debugger without looking at it in the Locals window, but it threw an exception that time.

This question indicates a similar solution but using SystemUpdate(), does that matter? Programmatically changing name of SPFolder

Community
  • 1
  • 1
Scott Stafford
  • 43,764
  • 28
  • 129
  • 177

6 Answers6

8

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();
pbaris
  • 4,525
  • 5
  • 37
  • 61
1

Try to add before the action "folder.ParentWeb.AllowUnsafeUpdates = true" and after it bring back the AllowUnsafeUpdates to it's previous value.

Netsh
  • 11
  • 1
1

You do not need to change the name, but te title. So:

folder.Item[SPBuiltInFieldId.Title] = newName;
folder.Item.Update(); // or SystemUpdate(false)

The difference between Update and SystemUpdate is that Update will change modified / modified by information and if versioning is enabled it will increase the version number. SystemUpdate does not update these.

Also note that I use SPBuiltInFieldId.Title. This is better than using "Title", because "Title" may cause issues in sites that are not in English.

Tom Vervoort
  • 5,028
  • 2
  • 22
  • 19
  • Sorry, I didn't see that it was for SharePoint 2003. I don't have any experience in 2003, only 2007 and 2010. It may work like this, but I'm not sure. – Tom Vervoort Sep 10 '10 at 19:49
1
 SPFolder attachfolder = documentLibrary.RootFolder.SubFolders[guid];
            //rename guid to new item id
            attachfolder.Properties[SPBuiltInFieldId.Title.tostring()] = itemID;
            attachfolder.Update();
Nishant
  • 11
  • 1
0
folder.Item["Name"] = newName;
folder.ParentWeb.AllowUnsafeUpdates = true;
folder.Item.Update();
folder.ParentWeb.AllowUnsafeUpdates = false;

this fork for me

hemen.rohani
  • 31
  • 1
  • 5
0

You can try using the MoveTo method

Vedran
  • 757
  • 6
  • 21