如何在使用 C# 将文档作为项目上传到 SharePoint 文档库时向自定义列添加值?

How to add value to a custom column while uploading document into a SharePoint document library as an item using C#?

我有一个控制台应用程序试图将文档上传到共享点文档库列表中。

我成功地做到了,而且我能够在使用 C# 上传文件时填充自定义列之一(列名称为:“类别”)值。

我尝试使用相同的过程填充另一个自定义列(列名是:“相关资产”)值,但我收到错误消息,指出提供的列名不存在,但当我在实际共享点门户中看到时它确实存在。

所以无法解决这个问题。即使我尝试了下面给出的几种方法,我也收到相同的错误消息,内容是该列不存在、已被删除或无法识别。

请查找显示列列表的 SharePoint 屏幕截图:

请找到我目前拥有的将文档上传到 SharePoint 门户的代码。

public static async Task<string> UploadReleaseNoteDocumentintoSpPortal(string releasenotefilepath, string releasenotefilename, string clientid, string clientsecret)
        {
            string status = string.Empty;
            try
            {
                Console.WriteLine("Trying to Upload Release note file into  Share Point Portal...");
                
                string siteUrl = "<<Sp site URL>>";

                Console.WriteLine("Connecting to Share Point Portal...");
                var ClientContext = new OfficeDevPnP.Core.AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, clientid, clientsecret);
                ClientContext.Load(ClientContext.Web, p => p.Title);
                await ClientContext.ExecuteQueryAsync();
                Console.WriteLine(ClientContext.Web.Title);
                var web = ClientContext.Web;
                Console.WriteLine("Connected successfully to Share Point Portal...");

                List DocumentsList = web.Lists.GetByTitle("Accelerators Documents");
                ClientContext.Load(DocumentsList.RootFolder);
                await ClientContext.ExecuteQueryAsync();
                Console.WriteLine("Reading and loading the list named : Accelerators Documents from SP");

                Console.WriteLine("Converting the release note document into byte array");
                byte[] bytes = System.IO.File.ReadAllBytes(releasenotefilepath);
              
                MemoryStream stream = new MemoryStream(bytes);

                Console.WriteLine("Storing the release note Data into File Create information object of SharePoint");
                FileCreationInformation FileCreateInfo = new FileCreationInformation();
                FileCreateInfo.Content = bytes;
                FileCreateInfo.ContentStream = stream;
                FileCreateInfo.Overwrite = true;
                FileCreateInfo.Url = DocumentsList.RootFolder.ServerRelativeUrl + @"\" + releasenotefilename;

                Console.WriteLine("Adding file to  SharePoint");
                var ReleaseNoteFiledata = DocumentsList.RootFolder.Files.Add(FileCreateInfo);
                ReleaseNoteFiledata.Update();
                ReleaseNoteFiledata.ListItemAllFields["Category"] = "Release Notes";
        //ReleaseNoteFiledata.ListItemAllFields["Related Assets"] = "<<Desired Value>>"; 
                //IN Above commented line i get the error stating Microsoft.SharePoint.Client.ServerException: 
        //'Column 'Related Assets' does not exist. It may have been deleted by another user.  
        //But in actual site if we see it exists as you can see in above screenshot
                ReleaseNoteFiledata.ListItemAllFields.Update();
                ClientContext.Load(ReleaseNoteFiledata);
                await ClientContext.ExecuteQueryAsync();

                Console.WriteLine("Adding file to SharePoint Completed Successfully...");
                return status = "Successful";
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception occured while trying to upload Release note file into CoP Portal :" + ex.Message);
                return status = "Error/Exception";
            }
        }

请查找我在尝试向 SharePoint 中存在的另一个自定义列添加值时收到的错误消息: Microsoft.SharePoint.Client.ServerException: '列 'Related Assets' 不存在。它可能已被其他用户删除。

即使我使用 ReleaseNoteFiledata.SetFileProperties() 并将值作为包含列名称及其值的字典键值对传递,我也会在第二个自定义列中遇到相同的错误。如果我只保留类别自定义列,那么它可以完美运行,没有任何问题,如您在上面的屏幕截图中所见。

如果我 select 在 SharePoint 中记录并查看详细信息或属性,则相关资产列符号类似于下面的屏幕截图:

请让我知道支持文件是否完好,或者如果我的问题仍然无法理解,以便我可以重新构建它或提供更多屏幕截图。

请帮助我解决上述问题或如何使此列在代码中可识别、可读或可识别。

提前致谢

此致

ChaitanyaNG

您需要在代码中使用列的内部名称 'Related Assets'。应该是 Related_x0020_Assets.

您可以通过转到列表设置-> 单击该列来检查列的内部名称,您将在 url 中看到内部名称。