Using SP Webservice after using SP OM with minimal impact to code

Standard

So earlier in my post, I made not of some issues my team encountered when using the sharepoint object model in relation to authentication. Well, we still haven’t be able to figure out why using SP OM raises server authentication and using the Sharepoint webservice does not. In order for use to meet our deadline, we had to change all our data retrieval and updating code statements from using the OM to using a .net proxy webservice of the sharepoint site. That was a pain because all our existing code utilized the fact that all the rows and columns where in a form of collection (SPListCollection and SPListItems). So the question was how do we change our code with minimal impact since the webservice returns XML data. In response to this, I created a simple xmlToCollection class that parses the xml data from the sharepoint list data retrieval methods (Getlistitems) into a collection that can be used the same way as used using SP OM. Here is the code, feel free to use it

public class SPCollection
{

private List> collection;
public SPCollection()
{
collection = new List>();
}

public <!–[CDATA[ List<dictionary> ]]–> List”<“Dictionary””>” CreateCollection(XmlNode data)
{
XmlNodeList rows = XPathQuery(data, “//z:row”);
string key = null;
string value = null;
foreach (XmlNode row in rows)
{
Dictionary item = new Dictionary();

for (int i =0; i
{
key = row.Attributes[i].Name;
key = key.Remove(0, 4);

value = row.Attributes[i].Value;
item.Add(XmlConvert.DecodeName(key), value);

}
collection.Add(item);

}

return collection;

}

public static XmlNodeList XPathQuery(XmlNode xmltoquery, string xpathquery)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmltoquery.OuterXml);
XmlNamespaceManager mg = new XmlNamespaceManager(document1.NameTable);
mg.AddNamespace(“sp”, “http://schemas.microsoft.com/sharepoint/soap/&#8221;);
mg.AddNamespace(“z”, “#RowsetSchema”);
mg.AddNamespace(“rs”, “urn:schemas-microsoft-com:rowset”);
mg.AddNamespace(“y”, “http://schemas.microsoft.com/sharepoint/soap/ois/&#8221;);
mg.AddNamespace(“w”, “http://schemas.microsoft.com/WebPart/v2&#8221;);
mg.AddNamespace(“d”, “http://schemas.microsoft.com/sharepoint/soap/directory/&#8221;);

return doc.SelectNodes(xpathquery, manager1);
}

}

Once you have this going you can replace codes like

SPListItemCollection coll = Mylist.GetItems(query);
string Fname = coll[0][“First Name”]

with

XmlNode result = sharepontwebservie.GetListitems(“Mylist”,null,query,null,null,null,null);
SPCollection foo = new SPCollection();
<!–[CDATA[ List<dictionary> ]]–> List”<“Dictionary””>” coll = foo.CreateCollection(result)
string Fname = coll[0][“FirstName”]

So as you can see, the only only section where you have to make you changes is the part high lighted in green, everything else syntactically remains the same from there on ( in relation to accessing and setting data in the collection)

FYI: Take out the ” in List”<“Dictionary””>” statements before use.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s