【发布时间】:2020-09-17 13:58:10
【问题描述】:
我已经搜索并找到了几个如何执行此操作的示例,但我无法让它们起作用 - 有一部分不起作用。 我可以执行文件上传,但以下更改属性的尝试失败。
我正在尝试从 base64 有效负载上传文件 - 这部分有效 - 但是当我随后尝试编辑与文件关联的属性(自定义列)时,代码失败。
以下是代码(为便于阅读而进行了简化): (请注意,props 是具有名称和值属性的自定义对象(FileProperty)的集合)。
using (ClientContext context = new ClientContext("<sharepoint_server_url>"))
{
context.Credentials = new SharePointOnlineCredentials(<usr>,<secure_pwd>);
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(Convert.FromBase64String(<base64_content>)))
{
File.SaveBinaryDirect(context, <relative_path>, ms, true);
}
// file is uploaded - so far so good!
// attempt to edit properties of the file.
if (props != null)
{
if (props.Count > 0)
{
File newFile = context.Web.GetFileByServerRelativeUrl(<relative_path>);
context.Load(newFile);
context.ExecuteQuery();
newFile.CheckOut();
ListItem item = newFile.ListItemAllFields;
foreach (FileProperty fp in props)
{
item[fp.name] = fp.value;
}
item.Update();
newFile.CheckIn(string.Empty, CheckinType.OverwriteCheckIn);
}
}
}
此代码在我尝试更新属性的部分引发异常。 消息:找不到文件。
谁能告诉我这个例子有什么问题或提供另一个例子来说明如何做到这一点?
还有一个问题 - 有没有办法通过唯一 ID 来寻址文件,无论文件位于或移动到 SharePoint 服务器中的哪个位置,该 ID 都是相同的?
我希望有人可以帮助我 - 谢谢:)
【问题讨论】:
-
我也尝试过添加 context.ExecuteQuery();最后,但没有区别。
-
另外,澄清一下 - 文件已上传并在浏览该位置时在浏览器中可见。由于找不到文件,只有更改属性的尝试才会失败。
标签: sharepoint sharepoint-online csom file-properties