简体   繁体   English

使用UpdateList将项目添加到SharePoint列表

[英]Adding items to a SharePoint list using UpdateList

I've come across a SharePoint problem that I hope someone can help with. 我遇到了一个SharePoint问题,希望有人可以提供帮助。 Basically, when I am trying to add a column called "MigratedChemist" to a list called "WorkCards" (pListName in the method parameters). 基本上,当我尝试将名为“ MigratedChemist”的列添加到名为“ WorkCards”的列表(方法参数中的pListName)时。 No matter what I try I am getting a FaultException error raised when calling UpdateList. 无论我如何尝试,在调用UpdateList时都会出现FaultException错误。 I am connecting using the SharePoint web service. 我正在使用SharePoint Web服务进行连接。 I have confirmed the following: 我已经确认以下内容:

  1. The column doesn't already exist and I do have permissions to create it in SharePoint 该列尚不存在,并且我具有在SharePoint中创建该列的权限
  2. The connection to SharePoint is established corectly to /_vti_bin/lists.asmx 与SharePoint的连接与/_vti_bin/lists.asmx核心建立
  3. The list name is correct as I have another method which returns items from the list and that works perfectly. 列表名称是正确的,因为我还有另一种方法可以从列表中返回项目,并且效果很好。
  4. The xVersion and xId values are set correctly when the program runs and passed as parameters - as far as I am concerned I should just be able to pass the list name, as opposed to the GUID, but neither method works. xVersion和xId值在程序运行时正确设置并作为参数传递-就我而言,我应该只能传递列表名称,而不是GUID,但是这两种方法均无效。

My code is as follows: 我的代码如下:

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;
    }

In addition to this does anyone know how to get any decent information from FaultRequest? 除此之外,还有人知道如何从FaultRequest中获取任何体面的信息吗? At the moment I get the following error message, which is of no use and there appears to be no extra detail. 目前,我收到以下错误消息,这是没有用的,并且似乎没有多余的细节。 I have tried, as some have suggested removing the error handling and letting the program halt, but that doesn't give me any extra information either. 我已经尝试过,因为有人建议删除错误处理并暂停程序,但这也没有给我任何额外的信息。

{"Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown."} {“类型'Microsoft.SharePoint.SoapServer.SoapServerException'的异常被抛出。”}

For future reference I have solved both the problem of the SharePoint update failing AND the FaultException problem, so am including it here for posterity: 供以后参考,我已经解决了SharePoint更新失败和FaultException问题的问题,因此为后代在此包括在内:

Problem 1 - Problem with UpdateList 问题1-UpdateList问题

This problem was caused because my XML was malformed. 造成此问题的原因是我的XML格式错误。 When I called ndNewFields.Add(newXml) then XML that was being returned was replacing < and > than with control characters, as shown below (I have added an extra space because this editor converts them automatically. 当我调用ndNewFields.Add(newXml)时,返回的XML正在用控制字符替换<和>,如下所示(我添加了额外的空间,因为此编辑器会自动转换它们。

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

Now, I did notice this pretty early on but wasn't sure whether it would cause a problem or not. 现在,我确实很早就注意到了这一点,但不确定是否会引起问题。 However using the XElement.Parse command I was able to remove these characters and this resolved the problem: 但是,使用XElement.Parse命令,我能够删除这些字符,从而解决了该问题:

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

Problem 2 - Problem with the SharePoint error coming back as FaultException 问题2-SharePoint错误返回为FaultException的问题

This has been bugging for me ages so I am glad I have finally solved it. 很久以来这一直困扰着我,所以很高兴我终于解决了它。 I can't take credit for it, as I took it from another page, but the following code was how I got the details. 我不能相信它,就像我从另一页上摘下来的一样,但是下面的代码是我获得详细信息的方式。

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

Hopefully this will save someone some frustration in the future! 希望这会在将来节省一些人的挫败感!

Andrew 安德鲁

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

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