Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.File.CheckOut();
    oListItem["Name"] = "xyz";           
    oListItem.Update();
    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";
           file.Update();
     }                                  
}

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
add comment

2 Answers 2

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

Works for me in the example code on this page.

share|improve this answer
1  
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
add comment
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;
    file.Item.Update();                                
}
return;
}
share|improve this answer
    
Thanks Charles, for reformatting the code. –  Anonymous May 7 '10 at 19:32
add comment

Your Answer

 
discard

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