Rename file in a document library folder
-
16-10-2019 - |
سؤال
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.
المحلول 2
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;
}
نصائح أخرى
Try using item.Name
rather than item["Name"]
.
Works for me in the example code on this page.
لا تنتمي إلى sharepoint.stackexchange