2010-10-01 33 views
3

我試圖將項目添加到Sharepoint中的列表中。目前我試圖通過CAML添加項目將ListItem添加到Sharepoint 2007中的列表中

我可以讀取列表並查詢列表,但我無法將其添加到列表中。我所看到的所有例子都更新了列表,我預計它應該是相當類似的添加項目的過程。

這就是我現在正在測試它的方式。 SPLists是一個Web引用到http:對SO,但沒有回答

///_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編輯
這是我得到

錯誤<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>

當我設置Web引用指向它在正確的網站,甚至看着列表中的s確保它在那裏。

+1

(1)在'UpdateListItems'周圍添加'try ... catch ...'並檢查是否有拋出的異常。 (2)以下是一個*添加項目*示例:http://msdn.microsoft.com/en-us/library/ms440289.aspx。 – 2010-10-01 09:25:21

+0

是的,我已經做到了。但錯誤不是很有幫助。 – 2010-10-03 22:10:09

+0

原來比我想象的更有幫助。一旦發現問題,錯誤就有意義。 – 2010-10-04 01:17:47

回答

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個字段(標題)的子網,只是爲了讓它工作。

2

看起來你可能需要一個小的除了你strBatch(use this article as a reference):<Field Name='ID'>New</Field>

這意味着你必須是這樣的:

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

另外,如果您有任何必填字段您列表中,您可能還必須指定這些。

相關問題