这似乎是一个愚蠢的问题,但我似乎在谷歌上找不到任何答案。
我编写了一个方法来查询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;
}
}
}
对此有任何帮助将不胜感激。谢谢!
您需要使用listtitems File成员:
string newPath = files[0]["FileDirRef"] + "/" + "MyNewFileName" + ".extension";
files[0].File.MoveTo(newPath, MoveOperations.Overwrite);
files[0].Update();
clientContext.ExecuteQuery();