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

2 comments
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.

2 comments :

  1. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.
    aws training in chennai
    AWS TRAINING IN VELACHERY
    AWS TRAINING IN TAMBARAM
    aws training in chennai
    aws training in annanagar
    aws training in chennai

    ReplyDelete
  2. I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog. 
    python training Course in chennai
    python training in Bangalore
    Python training institute in bangalore

    ReplyDelete