Rename a File in a SharePoint document library through object model

Rename a File in a SharePoint document library through object model

  • Comments 3

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 ("http://<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
  • 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!

  • How can one rename a file using SharePoint Client OM?

  • 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);

Page 1 of 1 (3 items)
Leave a Comment
  • Please add 4 and 5 and type the answer here:
  • Post