通过客户端对象模型在SharePoint中重命名文件时,如果两者之间有一个点,则会修剪文件名

Pan*_*ora 5 c# sharepoint sharepoint-clientobject

我使用SharePoint客户端对象模型编写了一个小应用程序,该模型重命名SharePoint 2010文档库中的所有文件.一切都运行良好,除非文件名中间应包含一个点,它将被修剪,从点开始.

例如,当新文件名应为" my fi.le name "时,它最终会在SharePoint中显示" my fi ".顺便说一句,文件的扩展名(在我的例子中是.pdf)保持正确.

这是我正在做的(一般):

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

当我通过浏览器(编辑属性)手动重命名SharePoint中的文件时,一切都按预期工作:点保持不变,文件名根本不会被修剪.

"FileLeafRef"错误的财产?任何想法是什么原因在这里?

Vad*_*hev 12

使用FileLeafRef属性可以更新文件名但没有扩展名.

如何使用SharePoint CSOM重命名文件

使用File.MoveTo方法重命名文件:

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();
}
Run Code Online (Sandbox Code Playgroud)

用法

using (var ctx = new ClientContext(webUrl))
{
    RenameFile(ctx, "/Shared Documents/User Guide.docx", "User Guide 2013.docx");
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!这实际上有效:-)可以通过使用**file.ListItemAllFields ["File_x0020_Type"]**添加扩展名,如:`file.MoveTo(file.ListItemAllFields ["FileDirRef"] +"/"+ newName + file .ListItemAllFields ["File_x0020_Type"],MoveOperations.Overwrite);` (2认同)