3

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

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

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

    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("");

}

有人已经在这里问过类似/相同的问题,但没有回答

编辑
这是我得到的错误

<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 中的列表以确保它在那里。

4

2 回答 2

2

看起来您可能需要对您的 strBatch 做一点补充(使用这篇文章作为参考):<Field Name='ID'>New</Field>

这意味着您将拥有以下内容:

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

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

于 2010-10-01T18:55:22.810 回答
0

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

当我在 Visual Studio 中设置 Web 参考时,我将它指向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 个字段(标题)的子网站,只是为了让它工作。

于 2010-10-03T23:27:24.880 回答