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.



            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["Name"] = "xyz";           


            oListItem.File.CheckIn("file name has been changed");


Comments (3)

  1. friesNgravy says:

    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!

  2. Mariano says:

    How can one rename a file using SharePoint Client OM?

  3. Jim says:

    TO rename a file using the Client Object Model:


    item.File.MoveTo(fname2, MoveOperations.Overwrite);                        

    string comment = "Testing file rename";

    item.File.CheckIn(comment, CheckinType.MinorCheckIn);

Skip to main content