簡體   English   中英

使用UpdateList將項目添加到SharePoint列表

[英]Adding items to a SharePoint list using UpdateList

我遇到了一個SharePoint問題,希望有人可以提供幫助。 基本上,當我嘗試將名為“ MigratedChemist”的列添加到名為“ WorkCards”的列表(方法參數中的pListName)時。 無論我如何嘗試,在調用UpdateList時都會出現FaultException錯誤。 我正在使用SharePoint Web服務進行連接。 我已經確認以下內容:

  1. 該列尚不存在,並且我具有在SharePoint中創建該列的權限
  2. 與SharePoint的連接與/_vti_bin/lists.asmx核心建立
  3. 列表名稱是正確的,因為我還有另一種方法可以從列表中返回項目,並且效果很好。
  4. xVersion和xId值在程序運行時正確設置並作為參數傳遞-就我而言,我應該只能傳遞列表名稱,而不是GUID,但是這兩種方法均無效。

我的代碼如下:

public static bool AddColumnToList(string pUri, string pListName, string pViewName, string pMaxRecords)
    {

        string version = string.Empty;
        XAttribute xId = null;
        XAttribute xVersion = null;

        try
        {
            XElement listDetails = client.GetList(pListName);
            xVersion = listDetails.Attribute("Version");
            xId = listDetails.Attribute("ID");
        }
        catch { throw; }

        XElement ndNewFields = new XElement ("Fields", "");
        string newXml = "<Method ID='1' Cmd='New'><Field Name='MigratedChemist' Type='Text' DisplayName='MigratedChemist' /></Method></Fields>";

        ndNewFields.Add(newXml);

        XElement result;

        try
        {
            result = client.UpdateList(xId.Value, null, ndNewFields, null, null, xVersion.Value);
        }
        catch (FaultException fe)
        {
        }


        return true;
    }

除此之外,還有人知道如何從FaultRequest中獲取任何體面的信息嗎? 目前,我收到以下錯誤消息,這是沒有用的,並且似乎沒有多余的細節。 我已經嘗試過,因為有人建議刪除錯誤處理並暫停程序,但這也沒有給我任何額外的信息。

{“類型'Microsoft.SharePoint.SoapServer.SoapServerException'的異常被拋出。”}

供以后參考,我已經解決了SharePoint更新失敗和FaultException問題的問題,因此為后代在此包括在內:

問題1-UpdateList問題

造成此問題的原因是我的XML格式錯誤。 當我調用ndNewFields.Add(newXml)時,返回的XML正在用控制字符替換<和>,如下所示(我添加了額外的空間,因為此編輯器會自動轉換它們。

<Method ID =“ 1” Cmd =“ New”&gt;&lt;字段名稱=“ MigratedChemist”&gt;&lt; / Field&gt;&lt; / Method&gt;

現在,我確實很早就注意到了這一點,但不確定是否會引起問題。 但是,使用XElement.Parse命令,我能夠刪除這些字符,從而解決了該問題:

ndNewFields.Add(XElement.Parse (newXml));

問題2-SharePoint錯誤返回為FaultException的問題

很久以來這一直困擾着我,所以很高興我終於解決了它。 我不能相信它,就像我從另一頁上摘下來的一樣,但是下面的代碼是我獲得詳細信息的方式。

catch (FaultException fe)
        {
            MessageFault msgFault = fe.CreateMessageFault();
            XmlElement elm = msgFault.GetDetail<XmlElement>();
}

希望這會在將來節省一些人的挫敗感!

安德魯

暫無
暫無

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

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