2

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.

  • Anders's What exactly did you edit? – Anonymous May 5 '10 at 19:00
0

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

Works for me in the example code on this page.

  • 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
0

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy