Programmatically rename a file inside a SharePoint document library
We can’t rename a file using file property SPFile.Name, it will say "Property or indexer 'Microsoft.SharePoint.SPFile.Name' cannot be assigned to -- it is read only.", then how we can rename a file ?
Yes, the “name” and “display name” are read-only fields, so you will get an error at the time of compilation. But you can rename a file in a document library by the following way.
1: SPSite oSite = new SPSite("https://<sitename>/");
2: SPWeb oWeb = oSite.OpenWeb();
3: SPList oList = oWeb.Lists["Shared Documents"];
4: SPListItem oListItem = oList.Items[0];
5: oListItem.File.CheckOut();
6: oListItem["Name"] = "xyz";
7: oListItem.Update();
8: oListItem.File.CheckIn("file name has been changed");
9: oWeb.Dispose();