2

这似乎是一个愚蠢的问题,但我似乎无法在谷歌上找到任何答案。

我编写了一个方法来查询 SharePoint 并根据我指定的文档名称参数重命名文档。我使用了类似的方法来重命名文件夹,并且效果很好,但是当我尝试重命名文件时,我得到了 ArgumentOutOfRangeException

这是我的代码:

public bool RenameFileInDocumentLibrary(string documentName, string newDocumentName, ClientContext clientContext)
        {
            {
                bool isDocumentRenamed = false;

                string url = "MySharePointSite";

                List list = clientContext.Web.Lists.GetByTitle("MyDocLib");

                CamlQuery query = new CamlQuery();
                query.ViewXml = "<View Scope=\"RecursiveAll\"> " +
                            "<Query>" +
                                "<Where>" +
                                    "<And>" +
                                        "<Eq>" +
                                            "<FieldRef Name=\"FSObjType\" />" +
                                            "<Value Type=\"Integer\">2</Value>" +
                                         "</Eq>" +
                                          "<Eq>" +
                                            "<FieldRef Name=\"Title\"/>" +
                                            "<Value Type=\"Text\">" + documentName + "</Value>" +
                                          "</Eq>" +
                                    "</And>" +
                                 "</Where>" +
                            "</Query>" +
                            "</View>";

                var files = list.GetItems(query);

                clientContext.Load(list);
                clientContext.Load(list.Fields);
                clientContext.Load(files, fs => fs.Include(fi => fi["Title"],
                    fi => fi["DisplayName"],
                    fi => fi["FileLeafRef"]));
                clientContext.ExecuteQuery();

                if (files.Count == 0)
                {
                    files[0]["Title"] = newDocumentName;
                    files[0]["FileLeafRef"] = newDocumentName;
                    files[0].Update();
                    clientContext.ExecuteQuery();
                    isDocumentRenamed = true;
                }

                return isDocumentRenamed;
            }
        }
    }  

对此的任何帮助将不胜感激。谢谢!

4

1 回答 1

2

您需要使用 ListItems File 成员:

string newPath = files[0]["FileDirRef"] + "/" + "MyNewFileName" + ".extension";
files[0].File.MoveTo(newPath, MoveOperations.Overwrite);
files[0].Update();
clientContext.ExecuteQuery();
于 2013-09-05T15:17:56.833 回答