Welcome to MSDN Blogs Sign in | Join | Help

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 ("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>

Published Wednesday, April 02, 2008 2:22 AM by sowmyancs

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Rename a File in a SharePoint document library through object model

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!

Monday, November 17, 2008 8:57 AM by friesNgravy

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker