Update Filename or List Item name programmatically in SharePoint/WSS
March 9th, 2010
As simple as it sounds, it ain’t simple.
you would of thought the following would work:
SPFile file = web.GetFile(strUrl);
file.name = "mynameFilename.txt";
file.Update();
does not compute..
There are 2 ways I’ve found of accomplishing this:
1. using the SPFile.MoveTo method:
string strUrl = "http://mysharepointsite/myolddocumentlibrary/Filename.txt";
string newFileUrl = "http://mysharepointsite/mynewdocumentlibrary/mynewFilename.txt";
SPSite site = new SPSite("http://mysharepointsite");
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates = true; // this will stop it from erroring out due to security errors
SPFile file = web.GetFile(strUrl);
file.MoveTo(newFileUrl);
2. using File.Item["Name"] method..
string strUrl = "http://mysharepointsite/myolddocumentlibrary/Filename.txt";
SPSite site = new SPSite("http://mysharepointsite");
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates = true; // this will stop it from erroring out due to security errors
SPFile file = web.GetFile(strUrl);
file.Item["Name"] = "NewFileName.txt";
However i have found method 1 more reliable. hope this helps!
Recent Comments