2014-03-03 92 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>"); 
    }