C# SharePoint CSOM 在上传后更改文件属性

C# SharePoint CSOM change file properties after upload

我已经搜索并找到了几个如何执行此操作的示例,但我无法使它们起作用 - 其中一部分不起作用。 我可以执行文件上传,但以下尝试更改属性失败。

我正在尝试从 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 都相同?

我希望有人能帮助我 - 谢谢:)

确保文件服务器相对 url 在这种情况下有效。

例如,如果完整的 url 是:

https://zheguo.sharepoint.com/sites/test/Shared%20Documents/test.jpg

那么相对url应该是

/sites/test/Shared%20个文件/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();
            }

好的,我找到了解决问题的方法。我不知道为什么这样效果更好,就是这样。 据我所知,我正在做完全相同的事情,只是以另一种方式 - 也许比我更了解 SharePoint 的其他人(并不多)可以解释为什么这有效,而我发布的第一个示例却没有.

在显示的代码之前,我确保 不以“/”结尾并且 不以“/”开头或结尾并且 不以“/”开头或结尾。 使用下面的代码,我可以上传一个文件并更新属性,在我的例子中,我更改了“Title”和一个自定义列“CustCulomnA”并且它有效。

using (ClientContext context = new ClientContext(<site_url>))
                {
                    context.Credentials = new SharePointOnlineCredentials(<usr>, <secure_pwd>);

                    FileCreationInformation fci = new FileCreationInformation()
                    {
                        Url = <file_name>,
                        Content = Convert.FromBase64String(<base64_content>),
                        Overwrite = true
                    };

                    Web web = context.Web;
                    List lib = web.Lists.GetByTitle(<library_name>);
                    lib.RootFolder.Files.Add(fci);
                    context.ExecuteQuery();

                    response.message = "uploaded";

                    if (props != null)
                    {
                        if (props.Count > 0)
                        {
                            File newFile = context.Web.GetFileByUrl(<site_url> +"/"+ <library_name> + "/" + <file_name>);
                            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);
                            context.ExecuteQuery();