C#/Sharepoint: get lists and list contents from webservice

1 comment
I had access to part of a sharepoint site at
http://sharepointserver/areas/myarea
(in norwegian http://sharepointserver/omrader/myarea, I think it's called area in english installation)
Within the area there was a list I wanted to extract some data from.

Started up .Net and created a new winforms-project.
Add service reference - Advanced - Add web reference
In url I added the string /_vti_bin/Lists.asmx resulting in something like
http://sharepointserver/areas/myarea/_vti_bin/Lists.asmx
there are other services available, but Lists was the part I was interested in.
I gave it the name sharepoint_lists
Now I could get a list of all lists in this part of sharepoint:


using (var sharepoint = new sharepoint_lists.Lists())

{
sharepoint.UseDefaultCredentials = true;
var lister = sharepoint.GetListCollection();
VisXML(lister, txtXML);
}
...

private void VisXML(XmlNode node, Control txt)
{
    txt.Text = node.OuterXml;
}

This gives me an XML containg all the lists in this part of sharepoint.
By glancing through it I could see my lists ID and Title, I could now use either to get the contents of my list:
using (var sharepoint = new sharepoint_lists.Lists())
{
sharepoint.UseDefaultCredentials = true;
var query = SorterQuery("Title");
var felter=LagViewFields(new string[]{"ID", "Title", "Beskrivelse"});
//var node = sharepoint.GetListItems("4dab35f1-d2c3-4270-9c7b-555555b55555", "",query ,felter, "", null, null);
var node = sharepoint.GetListItems("My list", "", query, felter, "", null, null);
VisXML(node, txtXML);
}

SorterQuery and LagViewFields are just some quick helpers I created to help with sorting and returning fields  
private XmlNode SorterQuery(string sortBy)
{
XmlDocument xmlDoc = new System.Xml.XmlDocument();
XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", ""); ndQuery.InnerXml = "<OrderBy>" + "<FieldRef Name=\"" + sortBy + "\" />" + "</OrderBy>";
return ndQuery;
}
private XmlNode LagViewFields(string[] felter)
{
var xmlDoc = new System.Xml.XmlDocument();
var node = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
foreach (var s in felter)
{
node.InnerXml += "<FieldRef Name='" + s + "' />";
}
return node;
}


With this I now have access to all the elements in my list,
and further processing is just a matter of parsing xml etc.

1 comment :

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru Dot Net Training in Chennai. Nowadays Dot Net has tons of job opportunities on various vertical industry.
    or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete