SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is all good except my file is located in a folder called "Shared Folder" under shared documents library.

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

=== added more ===

SPFolder gFolder = currentWeb.Folders[doclibname].SubFolders["Shared Folder"];
SPFileCollection filesColl = gFolder.Files;
foreach (SPFile file in filesColl)
     if (file.Title.Equals("abcdefg"))
           //file.Title = newFileName;
           file.Properties["Title"] = "xyz";

spfile does not let me do it this way.

share|improve this question
Anders's What exactly did you edit? – Anonymous May 5 '10 at 19:00

Try using item.Name rather than item["Name"].

Works for me in the example code on this page.

share|improve this answer
I get this error as soon it hits the following line. Value does not fall within the expected range. SPFolder gFolder = currentWeb.Folders[doclibname].SubFolders["Shared Folder"]; – Anonymous May 5 '10 at 18:12
up vote 0 down vote accepted

Finally this works. Some of the variables are declared on the project that are not shown here. like document library.

if (properties.BeforeUrl != properties.AfterUrl)
string[] onp = properties.BeforeUrl.Split('/');
string ofn= onp[onp.Length - 1]; // get the old file name
string ofnwoext = Path.GetFileNameWithoutExtension(ofn);
string[] nnp = properties.AfterUrl.Split('/');
string nfn = nnp[nnp.Length - 1]; //get the new file name

string furl = doculibrUrl + "/Shared Folder/" + ofnwoext + ".tiff";
SPFile file = web.GetFile(fURL);
if (file.Exists)
    file.Item["Name"] = nfn;
    file.Properties["Title"] = nfn;
share|improve this answer
Thanks Charles, for reformatting the code. – Anonymous May 7 '10 at 19:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.