7 August 2011

List web service and Copy web service to down load files from a Folder in SharePoint

/*It is a very common requirement that to down load the files from a folder from a sharepoint library using web service.The following code uses two inbuilt web services of sharepoint two down load files using webservice. This code can be used to create a console application that can be scheduled using a windows service to have scheduled transfer from Sharepoint*/


using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.IO;using System.Xml;using System.Configuration;using System.Data;namespace
{
FromSharePointclass Program{

{
static void Main(string[] args)try{
FileGET.
objLists.Credentials = System.Net.


objLists.Url = sitePath +


ndQueryOptions.InnerXml =
Lists objLists = new FileGET.Lists();CredentialCache.DefaultCredentials;string sitePath = ConfigurationManager.AppSettings["SitePath"].ToString();string destPath = ConfigurationManager.AppSettings["DestinationPath"].ToString();"_vti_bin/Lists.asmx";XmlDocument xmlDoc = new System.Xml.XmlDocument();XmlNode ndQueryOptions = xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");string.Format("<Folder>{0}</Folder>", ConfigurationManager.AppSettings["SourcePath"].ToString());XmlNode ndListItems = objLists.GetListItems("Library", null, null, null, null, ndQueryOptions, null); // you can change the document library name to your custom document library name

ds.ReadXml(reader);

{

{

CopyLIST.
myCopyService.Url = sitePath +
myCopyService.Credentials = System.Net.


CopyLIST.
CopyLIST.
XmlTextReader reader = new XmlTextReader(ndListItems.OuterXml, XmlNodeType.Element, null);DataSet ds = new DataSet();if (ds.Tables.Count > 1)foreach (DataRow dr in ds.Tables[1].Rows)string fpath = dr["ows_FileRef"].ToString().Substring(dr["ows_FileRef"].ToString().IndexOf("/"));Copy myCopyService = new CopyLIST.Copy();"_vti_bin/copy.asmx";CredentialCache.DefaultCredentials;string webURL = sitePath.Substring(0, sitePath.IndexOf("sites/") + 5);string copySource = webURL + fpath;FieldInformation myFieldInfo = new CopyLIST.FieldInformation();FieldInformation[] myFieldInfoArray = { myFieldInfo };byte[] myByteArray;// Call the web service
uint myGetUint = myCopyService.GetItem(copySource, out myFieldInfoArray, out myByteArray);// Convert into Base64 String
base64String =
string base64String;Convert.ToBase64String(myByteArray, 0, myByteArray.Length);// Convert to binary array
byte[] binaryData = Convert.FromBase64String(base64String);// Create a temporary file to write the text of the form to
string tempFileName = destPath + @"\"+ dr["ows_LinkFileName"].ToString();// Write the file to temp folder
fs.Write(binaryData, 0, binaryData.Length);
fs.Close();
}
}
}

{


}
}
}
 
}
 
FileStream fs = new FileStream(tempFileName, FileMode.Create, FileAccess.ReadWrite);catch (Exception ex)Console.WriteLine(ex.ToString());Console.Read();

No comments:

Post a Comment