如何解决C#SharePoint CSOM上传后更改文件属性
我已经搜索并找到了几个执行此操作的示例,但是我无法使它们起作用-很好的一部分是行不通的。 我可以执行文件上传,但是以下更改属性的尝试失败。
我正在尝试从base64有效负载上载文件-这部分起作用-但是当我随后尝试编辑与该文件关联的属性(自定义列)时,代码将失败。
以下是代码(为便于阅读而简化): (请注意, props 是具有名称和value属性的自定义对象(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都是相同的?
我希望有人能帮助我-谢谢:)
解决方法
好的,我找到了解决问题的办法。我不知道为什么这样做会更好,但确实如此。 就我所知,我正在以另一种方式做着完全相同的事情-也许其他比我更了解SharePoint的人(不多)可以解释为什么这有效,而我发布的第一个示例却不
在显示的代码之前,我确保
(?<=...)
,
在这种情况下,请确保文件服务器相对网址有效。
例如,如果完整的网址是:
https://zheguo.sharepoint.com/sites/test/Shared%20Documents/test.jpg
然后相对网址应为
/sites/test/Shared%20Documents/test.jpg
您还可以使用GetFileByUrl方法,传递完整的文件url,如下所示:
clientContext.Credentials = new SharePointOnlineCredentials(userName,securePassword);
Web web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();
File file = web.GetFileByUrl("https://zheguo.sharepoint.com/sites/test/Shared%20Documents/test.jpg");
clientContext.Load(file);
clientContext.ExecuteQuery();
file.CheckOut();
ListItem item = file.ListItemAllFields;
item["Title"] = "Test";
item.Update();
file.CheckIn(string.Empty,CheckinType.OverwriteCheckIn);
clientContext.ExecuteQuery();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。