2010-05-13 73 views
2

我正嘗試使用Web服務將新項目從c#中的winform應用程序添加到SharePoint項目列表中。只有結果,我得到無用的異常「類型'Microsoft.SharePoint.SoapServer.SoapServerException'的異常被拋出。」如何使用C sharp中的Web服務在SharePoint列表中添加新項目

我有一個名爲WebSrvRef的Web引用http://server/site/subsite/_vti_bin/Lists.asmx

而這種代碼:

 XmlDocument xmlDoc; 
     XmlElement elBatch; 
     XmlNode ndReturn; 
     string[] sValues; 
     string sListGUID; 
     string sViewGUID; 

     if (lstResults.Items.Count < 1) 
     { 
      MessageBox.Show("Unable to Add To SharePoint\n" + 
       "No test file processed. The list is blank.", 
       "Add To SharePoint", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
      return; 
     } 

     WebSrvRef.Lists listService = new WebSrvRef.Lists(); 
     sViewGUID = "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"; // Test List View GUID 
     sListGUID = "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"; // Test List GUID 

     listService.Credentials= System.Net.CredentialCache.DefaultCredentials; 

     frmAddToSharePoint dlgAddSharePoint = new frmAddToSharePoint(); 
     if (dlgAddSharePoint.ShowDialog() == DialogResult.Cancel) 
     { 
      dlgAddSharePoint.Dispose(); 
      listService.Dispose(); 
      return; 
     } 

     sValues = dlgAddSharePoint.Tag.ToString().Split('~'); 
     dlgAddSharePoint.Dispose(); 

     string strBatch = "<Method ID='1' Cmd='New'>" + 
      "<Field Name='Client#'>" + sValues[0] + "</Field>" + 
      "<Field Name='Company'>" + sValues[1] + "</Field>" + 
      "<Field Name='Contact Name'>" + sValues[2] + "</Field>" + 
      "<Field Name='Phone Number'>" + sValues[3] + "</Field>" + 
      "<Field Name='Brand'>" + sValues[4] + "</Field>" + 
      "<Field Name='Model'>" + sValues[5] + "</Field>" + 
      "<Field Name='DPI'>" + sValues[6] + "</Field>" + 
      "<Field Name='Color'>" + sValues[7] + "</Field>" + 
      "<Field Name='Compression'>" + sValues[8] + "</Field>" + 
      "<Field Name='Value % 1'>" + (((float)lstResults.Groups["Value 1"].Tag)*100).ToString("##0.00") + "</Field>" + 
      "<Field Name='Value % 2'>" + (((float)lstResults.Groups["Value 2"].Tag)*100).ToString("##0.00") + "</Field>" + 
      "<Field Name='Value % 3'>" + (((float)lstResults.Groups["Value 3"].Tag)*100).ToString("##0.00") + "</Field>" + 
      "<Field Name='Value % 4'>" + (((float)lstResults.Groups["Value 4"].Tag)*100).ToString("##0.00") + "</Field>" + 
      "<Field Name='Value % 5'>" + (((float)lstResults.Groups["Value 5"].Tag)*100).ToString("##0.00") + "</Field>" + 
      "<Field Name='Comments'></Field>" + 
      "<Field Name='Overall'>" + (fTotalScore*100).ToString("##0.00") + "</Field>" + 
      "<Field Name='Average'>" + (fTotalAvg * 100).ToString("##0.00") + "</Field>" + 
      "<Field Name='Transfered'>" + sValues[9] + "</Field>" + 
      "<Field Name='Notes'>" + sValues[10] + "</Field>" + 
      "<Field Name='Resolved'>" + sValues[11] + "</Field>" + 
      "</Method>"; 

     try 
     { 
      xmlDoc = new System.Xml.XmlDocument(); 
      elBatch = xmlDoc.CreateElement("Batch"); 

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

      strBatch = strBatch.Replace("&", "&amp;"); 
      elBatch.InnerXml = strBatch; 

      ndReturn = listService.UpdateListItems(sListGUID, elBatch); 

      MessageBox.Show(ndReturn.OuterXml); 
      listService.Dispose();     
     } 
     catch(Exception Ex) 
     { 
      MessageBox.Show(Ex.Message + 
       "\n\nSource\n" + Ex.Source + 
       "\n\nTargetSite\n" + Ex.TargetSite + 
       "\n\nStackTrace\n" + Ex.StackTrace, 
       "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      listService.Dispose(); 
     } 

我在做什麼錯?我錯過了什麼? 請幫忙!!

弗蘭克

回答

1

好吧,它現在有效。以下是我錯了:

我沒有使用FieldInternalName - >感謝羅伯特·威廉姆斯

我還缺少這行,因爲即使我在我的Web引用指向正確的位置,我listService。列表仍然指向服務器根站點。

listService.Url = @"http://server/site/subsite/_vti_bin/Lists.asmx"; 
0

肥皂異常將有一個詳細節點與更有幫助的錯誤說明。在調試器中抓住它並鑽入它。

1

這很可能是由於該行:

listService.Credentials= System.Net.CredentialCache.DefaultCredentials; 

修改成一組用戶名/密碼,就像這樣:

listService.Credentials = new NetworkCredential("UserID", "Password"); 

確保用戶ID /密碼訪問創建列表,等

+0

我改變了線這一: listService.Credentials =新的NetworkCredential( 「用戶ID」, 「密碼」, 「域」); 仍然無法正常工作。如果我轉到SharePoint站點的列表,我可以創建新項目。 我還沒有嘗試用調試器緩存錯誤。 – Frank 2010-05-13 18:00:22

1

如果NeworkCredential不是答案,那麼您應該確定您使用的是正確的字段名稱。

「字段名稱」應該是SharePoint列表字段的「FieldInternalName」。要獲取字段的FieldInternalName,請在SharePoint中從列表中選擇「新建」,然後從該頁面的「查看源代碼」中右鍵單擊「新建」窗體頁面中的任何位置。您將在源文件結尾附近看到這些字段及其內部名稱。

相關問題