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的人(不多)可以解释为什么这有效,而我发布的第一个示例却不

在显示的代码之前,我确保不以“ /”结尾,并且确保不以“ /”开头或结尾,并且确保不以“ /”开头或结尾。 “ /”。 使用下面的代码,我可以更新文件并更新属性,在我的情况下,我更改了“标题”和自定义列“ CustCulomnA”,并且可以正常工作。

(?<=...)
,

在这种情况下,请确保文件服务器相对网址有效。

例如,如果完整的网址是:

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();
            }