I wrote a small application using the SharePoint client object model, which renames all files inside a SharePoint 2010 document library. Everything works well, except if the filename should contain a dot somewhere in between, it get's trimmed, starting with the dot.
For example, when the new filename should be "my fi.le name" it ends up with "my fi" in SharePoint. The extension of the file (.pdf in my case) stays correct by the way.
Here's what I'm doing (in general):
ClientContext clientContext = new ClientContext("http://sp.example.com/thesite);
List list = clientContext.Web.Lists.GetByTitle("mydoclibrary");
ListItemCollection col = list.GetItems(CamlQuery.CreateAllItemsQuery());
clientContext.Load(col);
clientContext.ExecuteQuery();
foreach (var doc in col)
{
if (doc.FileSystemObjectType == FileSystemObjectType.File)
{
doc["FileLeafRef"] = "my fi.le name";
doc.Update();
clientContext.ExecuteQuery();
}
}
When I'm renamig the file in SharePoint manually via browser (edit properties), everything works as it should: The dot stays and the filename won't be trimmed at all.
Is "FileLeafRef"
the wrong property? Any ideas what's the cause here?
With SharePoint Online, if you send someone a sharing link, those people will still be able to access the same file using that link, even after you rename it. But if you previously sent them a direct link, they will not be able to access the file anymore.
Right-click on your desired File and Choose the “Rename” option. (Or Select the File >> Click on the “Rename” button in the Toolbar) You can also rename a document in SharePoint by selecting the file >> Click on three little dots in the toolbar and then click on the “Rename” button.
The folder will be renamed immediately. Please note, Renaming the SharePoint Online folder will impact the URL of the folder! Now, let's see SharePoint Online PowerShell to rename a folder. You can also rename a folder by using the Explorer View!
Click on “Home”, “Select All” to select all found files. Click on “Home”, “File Operations”, “Bulk Rename”
Using FileLeafRef
property it is possible to update file name but without extension.
Use File.MoveTo method to rename a file:
public static void RenameFile(ClientContext ctx,string fileUrl,string newName)
{
var file = ctx.Web.GetFileByServerRelativeUrl(fileUrl);
ctx.Load(file.ListItemAllFields);
ctx.ExecuteQuery();
file.MoveTo(file.ListItemAllFields["FileDirRef"] + "/" + newName, MoveOperations.Overwrite);
ctx.ExecuteQuery();
}
Usage
using (var ctx = new ClientContext(webUrl))
{
RenameFile(ctx, "/Shared Documents/User Guide.docx", "User Guide 2013.docx");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With