2014-03-03 49 views
1

我想通過Web服務獲取Sharepoint 2007列表的內容。我使用此代碼,這是我大多來自the MSDN page for GetListItems複製:GetListItems()導致401錯誤,但Checkout()工作

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Xml; 

namespace testGetListItems 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      sharepoint.Lists listService = new sharepoint.Lists(); 
      listService.Credentials = System.Net.CredentialCache.DefaultCredentials; 

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

      XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", ""); 
      XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", ""); 
      XmlNode ndQueryOptions = xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", ""); 

      ndQueryOptions.InnerXml = 
       "<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>" + 
       "<DateInUtc>TRUE</DateInUtc>"; 
      ndViewFields.InnerXml = "<FieldRef Name='Field1' /> <FieldRef Name='Field2'/>"; 
      ndQuery.InnerXml = "<Where><And><Gt><FieldRef Name='Field1'/>" + 
       "<Value Type='Number'>5000</Value></Gt><Gt><FieldRef Name='Field2'/>" + 
       "<Value Type=  'DateTime'>2003-07-03T00:00:00</Value></Gt></And></Where>"; 
      try 
      { 
       bool checkoutResult=listService.CheckOutFile("http://sharepoint/sites/mysite/myFile.xlsx", "false", null); 
       XmlNode ndListItems = 
        listService.GetListItems("Test List", null, ndQuery, 
        ndViewFields, null, ndQueryOptions, null); 
      } 

      catch (System.Web.Services.Protocols.SoapException ex) 
      { 
       Console.WriteLine("Message:\n" + ex.Message + "\nDetail:\n" + 
        ex.Detail.InnerText + 
        "\nStackTrace:\n" + ex.StackTrace); 
      } 
     } 
    } 
} 

CheckOutFile()的調用正常工作。但GetListItems()電話給了我這個錯誤:

An unhandled exception of type 'System.Net.WebException' occurred in System.Web.Services.dll 
Additional information: The request failed with HTTP status 401: Unauthorized. 

我不明白爲什麼CheckOutFile()成功,但GetListItems()失敗,尤其是因爲我檢查出來的文件是多數民衆贊成由GetListItems()訪問列表。

+0

有時前,我試圖建立一個工具導出SharePoint 2007列表,並不時得到相同的錯誤。我在客戶端使用WCF。 –

+0

嘗試使用列表GUID而不是顯示名稱。 –

+0

@NitinChhajer,我試着用列表GUID得到了相同的結果。 – sigil

回答

3

更新:這工作在測試控制檯應用程序,但不是在我的主要應用程序。現在離開這個答案,但我不會接受它,直到我得到修正。


原來問題出在Web服務的URL上。儘管我已經增加一條,作爲:

http://sharepoint/sites/mySite/_vti_bin/Lists.asmx 

的app.config文件有它列爲:

http://sharepoint/_vti_bin/Lists.asmx 

這仍然是即使我刪除了參考和重新加入它的問題;我必須手動更改app.config內容。一旦我完成了,GetListItems()調用成功。

0

您必須設置一個用戶名和密碼誰可以得到在庫/列表中的項目這種方式(對我來說它的工作原理,你可以試試):

public string GetIDFromList(string parameter) 
    { 
     string retorno = ""; 

     try 
     { 
      string path = "http://www.test.com/web/"; 

      // Reference to the SharePoint Lists web service: 
      WSSharePointCSCLists.Lists listsWS = new WSSharePointCSCLists.Lists(); 

      listsWS.Url = path + "_vti_bin/lists.asmx"; 

      listsWS.Credentials = GetUserCredential(); 

      string listName = "MyList"; 
      string viewName = ""; 
      //string webID; 
      string rowLimit = "500"; 

      // Web ID: 
      webID = "098304-9098asdf-qwelkfj-eoqiula";      


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

      // Query em CAML (SharePoint): 
      XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", ""); 

      XmlNode ndViewFields = 
         xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", ""); 
      XmlNode ndQueryOptions = 
         xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", ""); 

      ndQueryOptions.InnerXml = 
         "<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>" + 
         "<DateInUtc>TRUE</DateInUtc>" + 
         "<ViewAttributes Scope=\"RecursiveAll\" />"; 

      ndViewFields.InnerXml = @"<FieldRef Name='Title' />       
             <FieldRef Name='ID' />"; 


      string caml = 
       String.Format(
          @"<Where> 
           <Contains> 
            <FieldRef Name='MyColumn' /> 
            <Value Type='Text'>{0}</Value>   
           </Contains>          
          </Where>", 
        parameter); 

      ndQuery.InnerXml = caml; 

      XmlNode retornoWS = listsWS.GetListItems(listName, null, ndQuery, 
                 ndViewFields, rowLimit, 
                 ndQueryOptions, webID); 

      retorno = retornoWS.InnerXml; 


     } 
     catch (Exception ex) 
     { 
      Debug.WriteLine("Exception: " + ex.Message); 
      throw; 
     } 

     return retorno; 
    } 


    public NetworkCredential GetUserCredential() 
    { 
     return new System.Net.NetworkCredential("<username>", 
               "<password>", 
               "<domain>"); 
    }