Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When renaming a file in SharePoint via client object model, the filename gets trimmed if there's a dot in between

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?

like image 676
Pandora Avatar asked Oct 30 '14 09:10

Pandora


People also ask

Does renaming a file in SharePoint break the link?

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.

How do you rename a file in SharePoint?

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.

What happens if you rename SharePoint folder?

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!

How do I mass rename a document in SharePoint?

Click on “Home”, “Select All” to select all found files. Click on “Home”, “File Operations”, “Bulk Rename”


1 Answers

Using FileLeafRef property it is possible to update file name but without extension.

How to rename file using SharePoint CSOM

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");
}
like image 124
Vadim Gremyachev Avatar answered Sep 19 '22 17:09

Vadim Gremyachev