Adding a ListItem to a list in Sharepoint 2007
I am trying to add an item to a list in Sharepoint. At the moment I am trying to add the item via CAML
I can read the list, and query the list but I have not been able to add to the list. All the examples that I have seen update the list, I would expect that it should be reasonably similar process to add an item.
this is how I am testing it at the moment. SPLists is a web reference to开发者_如何学JAVA 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 on SO but not answered
Edit
This is the error that I get<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>
When I setup the web reference pointed it at the correct site and even looked at the list in sharepoint to make sure that it is there.
Looks like you probably need a small addition to your strBatch (use this article as a reference): <Field Name='ID'>New</Field>
Which means you'll have something like:
string strBatch ="<Method ID='1' Cmd='New'><Field Name='ID'>New</Field><Field Name='Title'>Test</Field></Method>";
Also, if you have any required fields on your list, you'll probably have to specify those as well.
This is what I found that solved my problem.
When I set up the Web Reference in visual studio I pointed it to the http://sharepointSite/subweb1/subweb2/_vit_bin/lists.asmx as the reference.
However when I went back and checked today it was pointing to http://sharepointSite/_vit_bin/lists.asmx. manually changing it back to http://sharepointSite/subweb1/subweb2/_vit_bin/lists.asmx in the app.config file made all the difference.
@Kit +1 I also added in you suggestion as well. With your suggetion and what I discovered about the web reference, it worked first time.
I ended up just creating a sub web with only 1 field (Title) just to get it working.
精彩评论