Friday, November 16, 2007

Searching with the WSSListProvider for Sharepoint

The WSSListProvider is a provider build on the Asp.NET provider framework that eases accessibility and simplifies the programming required to access list data in SharePoint. This is the basis for the SharePoint membership, profile, sitemap and role providers based on SharePoint list data. If you interested in any of these providers, please contact me.

This post explain in brief how to search using the WSSListProvider Sharepoint list provider.
It takes a hashtable of string pairs of search terms. Right now it only does exact "Text" based searching. Adding multiple items to the hashtable results in an "and" statement, all search items must be found.
The result is an xml root node containing no, one, or multiple rows of data. In each row, the data is stored in the attributes.


//This will search for "chris" in the "Username" column of the "+Abstract" list.
Hashtable search = new Hashtable();
search.Add("Username", "chris");
XmlNode result = WSSListManager.SearchListItems("+Abstract", search);
//Next, we can check to see how many items are in the list
//by using the Attribute "ItemCount" of the root node.
int count = int.Parse(result.Attributes["ItemCount"].Value);
//Now, based on how many rows there are, we can do various things.
if (count == 1)
{
//If there's only one row, we will select the first (and only) row
XmlNode node = result["z:row"];
//We now have access to the various Attributes of the row
//You can either access them individually...
string title = node.Attributes["Title"].Value;
//Or loop through all the attributes and use a switch statement
string temp;
bool approved;
foreach (XmlAttribute attrib in node.Attributes)
{
switch (attrib.Name)
{
case "Title":
temp = attrib.Value;
break;
case "Approved":
approved = bool.Parse(attrib.Value);
break;
default:
break;
}
}
}
else if (count > 1)
{
//Alternatively, if there is more than one row, we can
//access the data in all the rows by looping through them
foreach (XmlNode node in result)
{
//We need to check to make sure it's the right kind of node
//It injects "Whitespace" nodes at the beginning and ending of the rows returned
//Alternatively, it may be necessary to use (node.NodeType == XmlNodeType.Element)
if (node.NodeType != XmlNodeType.Whitespace)
{
//Using the same loop-through attributes code as above...
string temp;
bool approved;
foreach (XmlAttribute attrib in node.Attributes)
{
switch (attrib.Name)
{
case "Title":
temp = attrib.Value;
break;
case "Approved":
approved = bool.Parse(attrib.Value);
break;
default:
break;
}
}
}
}
}

0 comments: