【问题标题】:How to add value to a custom column while uploading document into a SharePoint document library as an item using C#?如何在使用 C# 将文档作为项目上传到 SharePoint 文档库时为自定义列添加值?
【发布时间】:2020-07-07 06:32:09
【问题描述】:

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

我能够成功地做到这一点,而且我能够在使用 C# 上传文件时填充自定义 Column(Column name is : "Category") 值之一。

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

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

请查看显示列列表的 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:“列“相关资产”不存在。它可能已被其他用户删除。

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

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

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

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

提前致谢

问候

柴坦尼亚

【问题讨论】:

    标签: c# sharepoint sharepoint-online


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的意见,现在我明白了问题出在哪里以及在哪里寻找这些类型的列。这行得通。再次感谢您的宝贵时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多