Geeks With Blogs
Fringe SharePoint Continued

I have been working with infopath 2007 to build an application for SharePoint. I put together a dashboard that pushes and pulls data from lists within SharePoint. With Visual Studio Tools for Applications you can write code that will accompany the form for extra functionality. Part of the code is that you can reference the "Microsoft.SharePoint.dll" to have access to the SharePoint object model and ultimately the data in SharePoint. Part of the architecture decisions I made quite early was to implement Web Services to give the infopath forms the flexibility to be used outside of the SharePoint environment.

I came across, what I think is pretty common scenario in SharePoint development with Web Services, cross List CAML Queries! I had one list of results and based on those results I needed to get a second set of results. It would be nice to be able to submit one query that would span across multiple lists instead of querying a list, processing the results and then querying again. Unfortunately you can't!

What you are left with at the end is multiple quires and manipulating data. I spent some time trying to figure out how to actually manipulate the results from a SharePoint query. The results have this format:

 <GetListItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">

<GetListItemsResult>

<listitems xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
     xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
     xmlns:rs='urn:schemas-microsoft-com:rowset'
     xmlns:z='#RowsetSchema'>
<rs:data ItemCount="3">
   <z:row ows_firstColumn="hi" ows_secondColumn="hello">
   <z:row ows_firstColumn="hi" ows_secondColumn="hello">
   <z:row ows_firstColumn="hi" ows_secondColumn="hello">
</rs:data>
</listitems>
</GetListItemsResult>
</GetListItemsResponse>


In order to get at the rows you can use the following code:

            XPathExpression exp = webserviceresult.Compile("/x:GetListItemsResponse/x:GetListItemsResult/x:listitems/rs:data/z:row");
            XPathNodeIterator iterator = GetXPathFromQuery(webserviceresult, exp);
            iterator.MoveNext();
            do {   } while (iterator.MoveNext());


private XPathNodeIterator GetXPathFromQuery(XPathNavigator nav, XPathExpression exp)
        {
            XmlNamespaceManager nsman = new XmlNamespaceManager(nav.NameTable);
            nsman.AddNamespace("x", "http://schemas.microsoft.com/sharepoint/soap/");
            nsman.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
            nsman.AddNamespace("z", "#RowsetSchema");
            exp.SetContext(nsman);
            XPathNodeIterator it = nav.Select(exp);
            return it;
        }


The above is pretty standard and you can cut paste and use that to get at the rows that are returned. The variable "webserviceresult" is an XPathNavigator, the result set that is returned. The Do-While Loop will traverse through all the rows.

What does this have to do with cross List queries? well what I did was actually create two queries and I had to XPathNavigators  and I had to create an XPathExpression that would query the second result set. It looks like the following.

                exp = secondResults.Compile(string.Format("/x:GetListItemsResponse/x:GetListItemsResult/x:listitems/rs:data/z:row[substring-after(@ows_CustomCreatedBy,\"#\")=\"{0}\"]", "Juan Larios");
                iterator = GetXPathFromQuery(secondResults, exp);
                iterator.MoveNext();


What you are seeing is a a query of a row attribute. Since the result is returned as attributes of an xml element, all attributes are prefixed with "ows". You are able to edit the attribute value and then compare agains the string, "Juan Larios". The reason I am doing that is that the value in "ows_CustomCreatedBy" is actually, "13;#Juan Larios". So part of the editing is to get the substring after ,'#'. If there was a way that I could execute a "contains" query , then I would do that instead.

Anyway, I hope it helps when manipulating through the Web Service Response and also querying the result set. I would also add that CAML queries are the way to go. The querying of a complete result set is not really the way you should or want to go but I had to make a decision. Either perform a one time query to bring in all the data I want to query against and perform the query above or, actually execute 100's or 1000's of individual queries.  

 

Posted on Monday, November 22, 2010 8:33 PM SharePoint , Development | Back to top


Comments on this post: Query SharePoint Web Service Result

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © juanlarios | Powered by: GeeksWithBlogs.net