Monday, February 26, 2007

How to use the GetListItems web service?

Small tip for using the web service easily.
I posted this on the msdn forums a lot of times for people who had problems getting the web service to work.
The following are two functions that you can use after adding referances to Lists.asmx and SiteData.asmx (why sitedata? because I use it to get the WebID that is needed by lists.asmx)

private System.Xml.XmlNode GetItems(string webPath)

{

ListsOnLocalhost.Lists listsWS = new Sharepoint_Unit_Tests.ListsOnLocalhost.Lists();

listsWS.Url = webPath + "/_vti_bin/lists.asmx";

listsWS.UseDefaultCredentials = true;

System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

doc.LoadXml("<Document><Query /><ViewFields /><QueryOptions /></Document>");

System.Xml.XmlNode listQuery = doc.SelectSingleNode("//Query");

System.Xml.XmlNode listViewFields = doc.SelectSingleNode("//ViewFields");

System.Xml.XmlNode listQueryOptions = doc.SelectSingleNode("//QueryOptions");

Guid g = GetWebID(webPath);

System.Xml.XmlNode items = listsWS.GetListItems(listName.Text, string.Empty, listQuery, listViewFields, string.Empty, listQueryOptions, g.ToString());

return items;

}

 

 

private Guid GetWebID(string webPath)

{

SiteDataWebService.SiteData siteDataWS = new SiteDataWebService.SiteData();

siteDataWS.UseDefaultCredentials = true;

SiteDataWebService._sWebMetadata webMetaData;

SiteDataWebService._sWebWithTime[] arrWebWithTime;

SiteDataWebService._sListWithTime[] arrListWithTime;

SiteDataWebService._sFPUrl[] arrUrls;

string roles; string[] roleUsers; string[] roleGroups; 

siteDataWS.Url = webPath + "/_vti_bin/sitedata.asmx";

uint i = siteDataWS.GetWeb(out webMetaData, out arrWebWithTime, out arrListWithTime, out arrUrls, out roles, out roleUsers, out roleGroups);

Guid g = new Guid(webMetaData.WebID);

return g;

}

10 comments:

Christian Henry said...

Thank you. You are the first I have encountered to have a working example.

Anonymous said...

Thanks for the great tip - but - now I have the items, i can't seem to traverse the xmlnodes .
I'm using

foreach(XmlNode item in items.ChildNodes)
{
MessageBox.Show(item.Attributes.GetNamedItem["ows_LinkFilename"].Value.ToString());
}

Any thoughts

Anonymous said...

You have to traverse the childnodes (z:row) of the childnotes.

Otherwise excellent sample,

peter

Drew said...

Ishai,
Great post - very helpful. One question - once you have the XMLNode fro the web service, how can you load the items returned into a DataSet?

Thanks

Amandeep said...

I am new to the SharePoint world and try learning tricks to excel in this field. Right now i am developing a form(Web enabled Infopath forms) for sharepoint site, in which I want to do cascading through drop down lists e.g. as soon as user selects the country from Country_DropDownList, form should populate the next drop down list with cities (datasource are sharepoint lists) and so on. Since it is web enabled i can't use built-in filter functions, so i am trying using it through web services, but webserices giving me hard time to achieve my task and came up with few questions and problems, and hope you will be able to help me.
Questions:
1. Is there is any other way to achieve this task (other than web services)?
2. How to deploy web service on sharepoint? (I am getting hard time deploying webservices on sharepoint site, I tried so many solutions, but didn't work for me.)
3. How should I access my data source, which is sharepoint lists, from a web service?
4.Since web services doesn't support collections (some XML serialzation), how should i get (from sharepoint list) and pass (pass back to form) result list or collections?

Please if you can help me in any way, I would really really appriciate that. Thanks in advance

Ishai Sagi [SharePoint MVP] said...

Amandeep:
I have a post about making cascading dropdowns work in infopath - which does not require the web service.

I also have a post about creating a web service for sharepoint

GerardV said...

Thanks, finally making some progress and finally an example that actually works.

Much appreciated!

Reza said...

Thank you,
It was simple and good code.

Fred Simons said...

This is a great Post on extracting the list items, but I had the same problem with what to do with the inner xml once we got it.

The following is niplet after extraction like the author showed us:

XmlDocument _wdoc = new XmlDocument();

DataRow dr = _dt.NewRow();
string tstx = String.Empty;
_wdoc.LoadXml(item.InnerXml);
int attrcnt = _wdoc.DocumentElement.Attributes.Count;
XmlAttributeCollection coll = _wdoc.DocumentElement.Attributes;

foreach (XmlAttribute attr in coll)
{
tstx = attr.Name;
switch (tstx)
{
case "ows_Title":
dr["Title"] = attr.Value;
process = true;
break;

case "ows_HDT":
dr["Helpdesk Ticket"] =
Convert.ToInt32(attr.Value);
process = true;
break;

case "ows_System":
dr["System"] = attr.Value;
process = true;
break;

case "ows_DueDate":
DateTime dt;
DateTime.TryParse
(attr.Value, out dt);
dr["Date Due"] = dt;
process = true;
break;

case "ows_Progress":
dr["Progress"] =
Convert.ToInt32
(attr.Value);
process = true;
break;

default:
break;
}
}
if (process)
{
_dt.Rows.Add(dr);
_dt.AcceptChanges();
process = false;
}

The key is to create a sub XML document by the XMLLoad. This method accepts a string. This will allow you to traverse the XML Document. In this example, I am adding from a custom list into a Data-table that is being shown in a gridview on the Web Page.

cornasdf said...

Thank you, that was very useful. I used the post to work up a powershell version and posted about it at http://cornasdf.blogspot.com/2009/06/powershell-and-sharepoint-lists.html