簡體   English   中英

如何在使用 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:'列'相關資產'不存在。 它可能已被其他用戶刪除。

即使我使用ReleaseNoteFiledata.SetFileProperties()並將值作為包含列名及其值的字典鍵值對傳遞,那么我也會為第二個自定義列得到相同的錯誤。 如果我只保留類別自定義列,那么它可以完美運行,沒有任何問題,如您在上面的屏幕截圖中所見。

如果我 select 記錄並查看 SharePoint 中的詳細信息或屬性,則相關資產列符號類似於以下屏幕截圖:

在此處輸入圖像描述

如果我的問題無法理解,請讓我知道支持文件是否正常,以便我可以重新構建它或提供更多屏幕截圖。

請幫助我解決上述問題或如何使該列在代碼中可識別、可讀或可識別。

提前致謝

問候

柴坦陽

您需要在代碼中使用“相關資產”列的內部名稱。 它應該是Related_x0020_Assets

您可以通過 go 檢查列的內部名稱以列出設置-> 單擊列,您將在 url 中看到內部名稱。

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM