2012-09-14 139 views
0

我已經創建了一個使用list.asmx(getlistitems方法)從另一臺服務器上的文檔庫中下載文檔的Windows應用程序。我可以訪問文檔名稱,URL等。當我使用以下代碼下載文件時,它將返回登錄頁面的html作爲我嘗試下載的每個文件的內容。GetListitems不返回預期的結果

有什麼想法?

  Dim spAuthentication As New Authentication() 
      spAuthentication.Url = authenticationWSAddress 
      spAuthentication.CookieContainer = New CookieContainer() 
      Dim spLists As New Lists() 
      spLists.Url = listWSAddress 
      'Try to login to SharePoint site with Form based authentication 
      Dim loginResult As LoginResult = spAuthentication.Login(userName, password) 
      Dim cookie As New Cookie() 
      'If login is successfull 
      If loginResult.ErrorCode = LoginErrorCode.NoError Then 
       'Get the cookie collection from the authenticatin web service 
       Dim cookies As CookieCollection = spAuthentication.CookieContainer.GetCookies(New Uri(spAuthentication.Url)) 
       'Get the specific cookie which contains the security token 

       cookie = cookies(loginResult.CookieName) 
       'Initialize the cookie container of the list web service 
       spLists.CookieContainer = New CookieContainer() 
       'set the cookie of list web service to the authenticatio cookie 
       spLists.CookieContainer.Add(cookie) 
       'Dim responseNode As XmlNode = spLists.GetListCollection() 
       'response = responseNode.InnerXml 

       Dim query As String = "<mylistitems><Query><Where><Eq><FieldRef Name='FileDirRef' /><Value Type='Url'>DocLib/Property Documents/BELASERA AT FULTON (lax10027)/Master Meter Invoices</Value></Eq></Where></Query><QueryOptions><ViewAttributes Scope='RecursiveAll' IncludeRootFolder='False' /><IncludeAttachmentUrls>TRUE</IncludeAttachmentUrls><ViewFields><FieldRef Name='EncodedAbsUrl'/></ViewFields></QueryOptions></mylistitems>" 
       Dim doc As New XmlDocument() 
       doc.LoadXml(query) 
       Dim dt As DataTable = Nothing 
       Dim queryNode As XmlNode = doc.SelectSingleNode("//Query") 
       Dim viewNode As XmlNode = doc.SelectSingleNode("//ViewFields") 
       Dim optionNode As XmlNode = doc.SelectSingleNode("//QueryOptions") 

       Dim retNode As XmlNode = spLists.GetListItems("DocLib", String.Empty, queryNode, viewNode, String.Empty, optionNode, Nothing) 

       Dim ds As New DataSet() 
       Using sr As New StringReader(retNode.OuterXml) 
        ds.ReadXml(sr) 
       End Using 

       If ds.Tables("Row") IsNot Nothing AndAlso ds.Tables("Row").Rows.Count > 0 Then 
        dt = ds.Tables("Row").Copy() 
        For Each myrow As DataRow In dt.Rows 
         ' myrow.Item(0) contains url of the document 
         If myrow.Item(0) IsNot Nothing AndAlso myrow.Item(0) <> "" Then 
          DownLoadAttachmentold(myrow.Item("ows_EncodedAbsUrl"), RemoveLookupID(myrow.Item("ows_FileLeafRef"))) 
         End If 
        Next 
       End If 




    Public Shared Sub DownLoadAttachment(ByVal strURL As String, ByVal strFileName As String) 
     Dim myWebClient As New WebClient() 
     Dim DestinationFolder As String = "C:\\DownLoads\\" 
     Form2.RTBStatus.AppendText("Downloading File " + strFileName + " from " + strURL + " .......") 
     ' The DownloadFile() method downloads the Web resource and saves it into the current file-system folder. 
     myWebClient.DownloadFile(strURL, DestinationFolder + strFileName) 
     'Form2.RTBStatus.AppendText("Successfully Downloaded file ""{0}"" from ""{1}""", "C:\\DownLoads\\" + strFileName, strURL) 
     Form2.RTBStatus.AppendText((ControlChars.Cr + "Downloaded file saved in the following file system folder:" + ControlChars.Cr + ControlChars.Tab + DestinationFolder)) 

    End Sub 
+0

SharePoint 2007的??? –

回答

0

DownloadAttachment還需要進行(基於表單的)經過身份驗證的HTTP請求。

在C#下面的例子:

request = (HttpWebRequest)WebRequest.Create(strURL); 
request.Credentials = System.Net.CredentialCache.DefaultCredentials;//adapt for your FBA 
request.AllowWriteStreamBuffering = false; 
response = (HttpWebResponse)request.GetResponse(); 
Stream s = response.GetResponseStream(); 
FileStream fs = new FileStream(@"C:\DownLoads\"+strFileName, FileMode.Create);