[英]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: 我已经确认以下内容:
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问题的问题,因此为后代在此包括在内:
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"> ;< ;Field Name="MigratedChemist"> ;< ;/Field> ;< ;/Method> ; <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));
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.