Rename a File in a SharePoint document library through object model
Following code will do that functionality. Like how we are renaming a file through UI, we have to first check-out the file and after renaming the file we have to check-in it.
<code>
SPSite oSite = new SPSite ("https://<sitename>/");
SPWeb oWeb = oSite.OpenWeb();
SPList oList = oWeb.Lists["Shared Documents"];
SPListItem oListItem = oList.Items[0]; //taking the first list item
oListItem.File.CheckOut();
oListItem["Name"] = "xyz";
oListItem.Update();
oListItem.File.CheckIn("file name has been changed");
</code>
Comments
Anonymous
November 17, 2008
I don't know if this is possible.. but i tried this with lists. im about to rename an attachment filename. but i get an error 'Object reference not set to an instance of an object.' can you help me with this? i'm a newbie in sharepoint and i already tried everything found on the net that would help me rename attachment filenames but still fail. thanks in advance!Anonymous
December 06, 2012
How can one rename a file using SharePoint Client OM?Anonymous
May 29, 2013
TO rename a file using the Client Object Model: item.File.CheckOut(); item.File.MoveTo(fname2, MoveOperations.Overwrite); string comment = "Testing file rename"; item.File.CheckIn(comment, CheckinType.MinorCheckIn);