How can I download files from document library using Lists.asmx web service ? How do I instantiate an object of Lists web service in my code ? Does any one have complete code for this ?
1 Answer
And if you really want complete code here it is (copied, pasted, formatted):
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using SiteDataWebService;
namespace SiteDataWebService
{
class Program
{
public static void DownLoadAttachment(string strURL,string strFileName)
{
HttpWebRequest request;
HttpWebResponse response = null;
try
{
request = (HttpWebRequest)WebRequest.Create(strURL);
request.Credentials = System.Net.CredentialCache.DefaultCredentials;
request.Timeout = 10000;
request.AllowWriteStreamBuffering = false;
response = (HttpWebResponse)request.GetResponse();
Stream s = response.GetResponseStream();
//Write to disk
FileStream fs = new FileStream(@"C:\\DownLoads\\" +strFileName, FileMode.Create);
byte[] read = new byte[256];
int count = s.Read(read, 0, read.Length);
while (count > 0)
{
fs.Write(read, 0, count);
count = s.Read(read, 0, read.Length);
}
//Close everything
fs.Close();
s.Close();
response.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);}
}
static void Main(string[] args)
{
XmlDocument resdoc = new System.Xml.XmlDocument();
XmlNode resnode = null;
string strURL = "";
string strFileName = "";
try
{
ListsService.Lists objLists = new SiteDataWebService.ListsService.Lists();
objLists.Credentials = System.Net.CredentialCache.DefaultCredentials;
objLists.Url = "http://[SITENAME]:34028/sites/TestSite/_vti_bin/lists.asmx"; // change the URL to your sharepoint site
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 ="<IncludeAttachmentUrls>TRUE</IncludeAttachmentUrls>";
ndViewFields.InnerXml = "";
ndQuery.InnerXml = "";
try
{
XmlNode ndListItems = objLists.GetListItems("Shared Documents", null, ndQuery, ndViewFields, null, ndQueryOptions, null); // you can change the document library name to your custom document library name
XmlNodeList oNodes = ndListItems.ChildNodes;
foreach (XmlNode node in oNodes)
{
XmlNodeReader objReader = new XmlNodeReader(node);
while(objReader.Read())
{
if (objReader["ows_EncodedAbsUrl"] != null && objReader["ows_LinkFilename"]!=null)
{
strURL = objReader["ows_EncodedAbsUrl"].ToString();
strFileName = objReader["ows_LinkFilename"].ToString();
DownLoadAttachment(strURL,strFileName);
}
}
}
Console.ReadLine();
}
catch (System.Web.Services.Protocols.SoapException ex)
{
Console.WriteLine("Message:\n" + ex.Message + "\nDetail:\n" + ex.Detail.InnerText + "\nStackTrace:\n" + ex.StackTrace);
Console.ReadLine();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
-
1how can I use this code snippet? I paste the code in VS2005 C#, but I can't compile. I obtain error concerning ListsService: impossible find the type or the name of the namespace 'ListsService'; probably is missing a directive using or a reference to an assemnbly" I also tried to create a web reference to the _vti_bin/Lists.asmx of my WSS site and naming it "ListsService", as suggested in another forum; now I have only this two errors, instead of much more previously obtained– user5854Nov 30, 2011 at 15:11
-
Franceso: You must create a .cs file with wsdl.exe tool and add it to your project. If you want to add a DLL then you need to use csc.exe to convert your .cs file into a DLL. Both wsdl and csc tools are command-line tools.– Zakir HCDec 15, 2011 at 9:47