简体   繁体   English

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

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

I have a console application which tries to upload a document into a share point document library list.我有一个控制台应用程序,它尝试将文档上传到共享点文档库列表中。

I am successfully able to do it and also I am able to fill one of the custom Column(Column name is: "Category") value while uploading the file using C#.我成功地做到了,并且在使用 C# 上传文件时,我能够填充自定义列之一(列名是:“类别”)值。

I have tried to fill another custom column(Column name is: "Related Assets") value using the same procedure but i get the error stating the provided column name does not exist but when i see in actual share point portal it does exist.我尝试使用相同的过程填充另一个自定义列(列名是:“相关资产”)值,但我收到错误消息,指出提供的列名不存在,但当我在实际共享点门户中看到它确实存在时。

So not able to solve this issue.所以无法解决这个问题。 Even i tried couple of methods as given below and i get same error message in terms of the column does not exist or it has been deleted or not able to recognize it.即使我尝试了下面给出的几种方法,但在列不存在或已被删除或无法识别时,我得到相同的错误消息。

Please find the screenshot of SharePoint showing the list of columns:请找到显示列列表的 SharePoint 的屏幕截图:

在此处输入图像描述

Please find the code i have till now which upload the document into SharePoint portal.请找到我到目前为止将文档上传到 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";
            }
        }

Please find the error message i get while trying to add value to another custom column present in SharePoint: Microsoft.SharePoint.Client.ServerException: 'Column 'Related Assets' does not exist.请找到我在尝试向 SharePoint 中存在的另一个自定义列添加值时收到的错误消息: Microsoft.SharePoint.Client.ServerException:'列'相关资产'不存在。 It may have been deleted by another user.它可能已被其他用户删除。

Even if i use the ReleaseNoteFiledata.SetFileProperties() and pass the values as a dictionary key value pair containing the column name and its value then also i get the same error for the second custom column.即使我使用ReleaseNoteFiledata.SetFileProperties()并将值作为包含列名及其值的字典键值对传递,那么我也会为第二个自定义列得到相同的错误。 If i keep only the category custom column then it works perfectly without any issue as you can see in the screenshot above.如果我只保留类别自定义列,那么它可以完美运行,没有任何问题,如您在上面的屏幕截图中所见。

If i select the record and see the details or properties in the SharePoint the Related assets column symbol is some like in below screenshot:如果我 select 记录并查看 SharePoint 中的详细信息或属性,则相关资产列符号类似于以下屏幕截图:

在此处输入图像描述

Please let me know if the supporting documents are fine or still if my issue is not understandable so that i can re frame it or provide more screenshots.如果我的问题无法理解,请让我知道支持文件是否正常,以便我可以重新构建它或提供更多屏幕截图。

Please help me in solving the above issue or how to make this column recognizable or readable or identifiable in the code.请帮助我解决上述问题或如何使该列在代码中可识别、可读或可识别。

Thanks in Advance提前致谢

Regards问候

ChaitanyaNG柴坦阳

You need to use the internal name of the column 'Related Assets' in your code.您需要在代码中使用“相关资产”列的内部名称。 It should be Related_x0020_Assets .它应该是Related_x0020_Assets

You could check the internal name of the column by go to list settings-> click the column, you would see the internal name in the url.您可以通过 go 检查列的内部名称以列出设置-> 单击列,您将在 url 中看到内部名称。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM