This is how you can rename a SPFolder. There is no need to use MoveTo() as suggested by others.
The user has to have rights to do it, or enclose the whole thing inside a
var siteId = "Guid of my site";
var webUrl= "Server relative path to my web";
SPSecurity.RunWithElevatedPrivileges(delegate(){
using (SPSite elevatedSite = new SPSite(siteId))
{
using (SPWeb elevatedWeb = elevatedSite.OpenWeb(webUrl))
{
string requestFolderUrl = webUrl+ "/MYLIST/OldFolderName";
SPFolder requestFolder = elevatedWeb.GetFolder(requestFolderUrl);
if (requestFolder.Exists)
{
requestFolderItem[SPBuiltInFieldId.Title] = "NewName"; //not really needed for renaming
//requestFolderItem[SPBuiltInFieldId.BaseName] = "NewName"; //this Fails.don't know why
requestFolderItem["BaseName"] = "NewName"; //this Works!
//don't want to update the Modified time nor the modified by Info
//If you want to modify this info use Update, and avoid being in a RunWithElevatedPrivileges
///because else you would have the System User registered
requestFolderItem.SystemUpdate(false);
}
}
}
}
Thanks for the useful perspective. I think you are missing one elevatedWeb.AllowUnsafeUpdates = true, elevatedWeb.AllowUnsafeUpdates = false before and after requestFolderItem.SystemUpdate(false);
ReplyDelete