Parsing SharePoint List Items from Lists.asmx
I am almost certain there might be a better way of doing this, but this works for me and has been proven and tested in my own environment on multiple occassions.I have a SharePoint list containing employee data that needs to be used in a Reporting Services report. The goal of this report is to display the number of employee turnovers for each satellite office location. On one specific occassion, i had a need to parse out the ugly XmlNode that was returned to me after querying my SharePoint list for employee ID’s.
First, i connected to my SharePoint list containing the employee id’s, then created my XmlDocument and XmlNode(s) to query my list. The XmlNode, “ndViewFields”, contains the fields i am interested in returning. You can leave this empty if you’d like to return every column in that list, but in my case, i’m only interested in the “ID” and “EMPLID” columns.
The “ndQuery” XmlNode contains any CAML query you may need to assist you in retrieving your own specific list items.
02.
ArrayList employeeIDs =
new
ArrayList();
03.
mySharePointSite.Lists wList =
new
mySharePointSite.Lists();
04.
wList.Url = ListServiceURL;
05.
wList.Credentials = System.Net.CredentialCache.DefaultCredentials;
06.
XmlDocument xmlDoc =
new
System.Xml.XmlDocument();
07.
XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element,
"Query"
,
""
);
08.
XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element,
"ViewFields"
,
""
);
09.
XmlNode ndQueryOptions = xmlDoc.CreateNode(XmlNodeType.Element,
"QueryOptions"
,
""
);
10.
ndQueryOptions.InnerXml = "<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>
11.
<DateInUtc>FALSE</DateInUtc>
12.
<ExpandUserField>FALSE</ExpandUserField>";
13.
//you don't need to specifically request the 'ID' column since it will be returned regardless
14.
ndViewFields.InnerXml =
"<FieldRef Name='ID' /><FieldRef Name='EMPLID' />"
;
15.
ndQuery.InnerXml =
"<OrderBy><FieldRef Name='EMPLID'/></OrderBy>"
;
16.
try
17.
{
18.
XmlNode ndListItems = wList.GetListItems(yourSharePointListGUID, yourSharePointListViewGUID, ndQuery, ndViewFields,
null
, ndQueryOptions,
null
);
19.
}
20.
catch
(System.Web.Services.Protocols.SoapException ex)
21.
{
22.
Console.WriteLine(
"Message:n"
+ ex.Message +
"nDetail:n"
+ ex.Detail.InnerText +
"nStackTrace:n"
+ ex.StackTrace);
23.
Console.Read();
24.
}
The first line, “Attribute (ows_EMPLID) = ###” is the item i am interested in for my final array of employee ID’s in my SharePoint list, but this is not really useful in this format, so I now need to parse out only the EMPLID column. To do that, we’ll create a foreach loop that will parse out only the ows_EMPLID column and get the value for to create an array of employee id’s.
02.
{
03.
if
(node.Name ==
"rs:data"
)
04.
{
05.
for
(
int
f = 0; f < node.ChildNodes.Count; f++)
06.
{
07.
if
(node.ChildNodes[f].Name ==
"z:row"
)
08.
{
09.
//Add the employee ID to my 'employeeIDs' ArrayList
10.
employeeIDs.Add(node.ChildNodes[f].Attributes[
"ows_EMPLID"
].Value);
11.
}
12.
}
13.
}
14.
}
To put all this together, this is the code for my method:
No comments:
Post a Comment