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

时间:2020-07-07 06:32:09

标签: c# sharepoint sharepoint-online

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

我能够成功完成此操作,同时还能在使用C#上传文件时填充自定义Column(列名称为“类别”)值之一。

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

因此无法解决此问题。甚至我尝试了如下所示的几种方法,但在该列不存在或它已被删除或无法识别的情况下,我得到了相同的错误消息。

请找到显示列列表的SharePoint屏幕快照:

enter image description here

请找到我到目前为止所拥有的将文档上载到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:'列'相关资产'不存在。它可能已被其他用户删除。

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

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

enter image description here

请让我知道支持文档是否还可以,或者我的问题还是无法理解的,以便我可以重新构图或提供更多屏幕截图。

请帮助我解决以上问题,或如何使该列在代码中可识别,可读或可识别。

预先感谢

致谢

ChaitanyaNG

1 个答案:

答案 0 :(得分:1)

您需要在代码中使用“相关资产”列的内部名称。应该是Related_x0020_Assets

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

enter image description here

相关问题