The following code sample display how to read from a Sharepoint 2007 list which includes also attachments. I haven't used this code for MOSS 2010!
private ICredentials GetCredentials()
{
return new NetworkCredential("username", "password", "domain");
}
private DataRowCollection LoadSharepointData(string viewFields, string listName)
{
WebServiceRef.Lists sp = new WebServiceRef.Lists();
sp.Url = "http://your.sharepoint.com/_vti_bin/Lists.asmx";
sp.Credentials = GetCredentials();
// this part is needed to receive the link to document attachments and all specified fields
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 = viewFields;
ndQuery.InnerXml = ""; // <Query />
XmlNode allLists = sp.GetListCollection();
XmlNode lists = sp.GetListItems(listName, "", ndQuery, ndViewFields, "10000", ndQueryOptions, "");
XmlNodeReader r = new XmlNodeReader(lists);
DataSet ds = new DataSet();
ds.ReadXml(r, XmlReadMode.Auto);
return ds.Tables[1].Rows;
}
WebServiceRef is a normal Web Service reference to Sharepoints List Webservice:
- http://your.sharepoint.com/_vti_bin/lists.asmx
The following code blocks contain various helper classes to extract data row by row, download attachments and saving data to a local folder.
Here we have the class to download files from Sharepoint:
public class AttchmentHandler
{
private static CookieContainer _cookieContainer;
public static CookieContainer CookieContainer
{
get
{
if (_cookieContainer == null)
{
_cookieContainer = new CookieContainer();
}
return _cookieContainer;
}
}
public class CookieAwareWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = AttchmentHandler.CookieContainer;
(request as HttpWebRequest).KeepAlive = false;
}
return request;
}
}
public string DownloadSharepointAttachment(string url, string filename, ICredentials credentials)
{
string localFolder = @"D:\\LocalFolder\\";
// ensure we do not overwrite files with the same name.
string uniqueGuid = Guid.NewGuid().ToString();
//now the code that will download the file
try
{
using (WebClient client = new CookieAwareWebClient())
{
client.Credentials = credentials;
// client.DownloadFile("http://address.com/somefile.pdf", @"c:\\temp\savedfile.pdf");
client.DownloadFile(url, localFolder + uniqueGuid + "_-_" + filename);
client.Dispose();
}
return localFolder + uniqueGuid + "_-_" + filename;
}
catch (Exception ex)
{
Console.WriteLine("DownloadSharepointAttachment: " + ex.Message);
return string.Empty;
}
}
}
To extract data from the DataRowCollection we have to Parse each row. For that i had to write 2 methods, one which retrieves the row and another one which extract the data from the column.
public void ParseAndExtractRow(System.Data.DataRow data, ICredentials credentials)
{
this.ContentTypeId = this.GetRowValue(data, "ows_ContentTypeId");
this.Title = this.GetRowValue(data, "ows_Title");
this.Date = this.GetRowValue(data, "ows_Date");
if (string.IsNullOrEmpty(this.Date))
this.Date = DateTime.Now.ToString();
DateTime.TryParse(this.Date, out this.DateTimeSorting);
this.Attribute_01 = this.GetRowValue(data, "Attribute_01");
// add here all other attributes
// ....
// ...
// ...
}
private string GetRowValue(System.Data.DataRow data, string columnName)
{
try
{
if (data[columnName] != null)
return data[columnName].ToString();
else
return string.Empty;
}
catch (Exception ex)
{
// Console.WriteLine(ex.Message);
return string.Empty;
}
}
Finally a method which uses all this code:
private List<Data> ExtractList()
{
string listname = "SharepointListName";
string viewFields = "";
DataRowCollection rows = LoadSharepointData(viewFields, listname);
List<Data> data = new List<Data>();
int i = 0;
foreach (DataRow row in rows)
{
Data b = new Data();
try
{
b.ParseAndExtractRow("Name", row, GetCredentials());
data.Add(b);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
b = null;
}
}
return data;
}
enjoy
No comments:
Post a Comment