【问题标题】:Adding a ListItem to a list in Sharepoint 2007将 ListItem 添加到 Sharepoint 2007 中的列表
【发布时间】:2011-04-19 17:10:11
【问题描述】:

我正在尝试将项目添加到 Sharepoint 中的列表。目前我正在尝试通过 CAML 添加项目

我可以阅读列表并查询列表,但我无法添加到列表中。我看到的所有示例都更新了列表,我希望添加项目的过程应该是相当相似的。

这就是我目前正在测试它的方式。 SPLists 是对 http:///_vti_bin/lists.asmx 的网络引用

    void Test(){
        var listService = new SPLists.Lists();

        string strBatch ="<Method ID='1' Cmd='New'><Field Name='Title'>Test</Field></Method>";

        XmlDocument xmlDoc = new System.Xml.XmlDocument();

        System.Xml.XmlElement elBatch = xmlDoc.CreateElement("Batch");

        elBatch.SetAttribute("OnError", "Continue");
        elBatch.SetAttribute("ListVersion", "1");

        elBatch.InnerXml = strBatch;
        XmlNode ndReturn = listService.UpdateListItems("TestList",elBatch);

        Console.Write(ndReturn.OuterXml); 
        Console.WriteLine("");

}

someone already asked a similar/same question here 在 SO 但没有回答

编辑
这是我得到的错误

<Results xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<Result ID="1,New">
<ErrorCode>0x81020026</ErrorCode>
<ErrorText>The list that is referenced here no longer exists.</ErrorText>
</Result>
</Results>

当我设置网络参考时,它指向了正确的站点,甚至查看了 sharepoint 中的列表以确保它在那里。

【问题讨论】:

  • (1) 在UpdateListItems 周围添加try ... catch ... 并检查是否有异常抛出。 (2) 这是一个添加项目的例子:msdn.microsoft.com/en-us/library/ms440289.aspx.
  • 是的,我已经做到了。但错误不是很有帮助。
  • 结果比我想象的更有帮助。一旦我发现问题,错误就有意义了。

标签: sharepoint sharepoint-2007 caml


【解决方案1】:

看起来您可能需要在 strBatch 中添加一些小内容 (use this article as a reference):&lt;Field Name='ID'&gt;New&lt;/Field&gt;

这意味着你会有类似的东西:

string strBatch ="<Method ID='1' Cmd='New'><Field Name='ID'>New</Field><Field Name='Title'>Test</Field></Method>";

此外,如果您的列表中有任何必填字段,您可能还必须指定这些字段。

【讨论】:

    【解决方案2】:

    这就是我发现的解决了我的问题的方法。

    当我在 Visual Studio 中设置 Web Reference 时,我将其指向 http://sharepointSite/subweb1/subweb2/_vit_bin/lists.asmx 作为参考。

    但是,当我今天回去查看时,它指向的是http://sharepointSite/_vit_bin/lists.asmx。在 app.config 文件中手动将其更改回 http://sharepointSite/subweb1/subweb2/_vit_bin/lists.asmx 会有所不同。

    @Kit +1 我也添加了你的建议。根据您的建议以及我对网络参考的发现,它第一次奏效了。

    我最终只是创建了一个只有 1 个字段(标题)的子网站,只是为了让它工作。

    【讨论】:

      猜你喜欢
      • 2015-01-15
      • 1970-01-01
      • 2011-04-18
      • 2012-12-24
      • 2013-06-11
      • 2010-10-22
      • 1970-01-01
      • 1970-01-01
      • 2011-02-27
      相关资源
      最近更新 更多