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


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);



return collection;


public static XmlNodeList XPathQuery(XmlNode xmltoquery, string xpathquery)
XmlDocument doc = new XmlDocument();
XmlNamespaceManager mg = new XmlNamespaceManager(document1.NameTable);
mg.AddNamespace(“sp”, “;);
mg.AddNamespace(“z”, “#RowsetSchema”);
mg.AddNamespace(“rs”, “urn:schemas-microsoft-com:rowset”);
mg.AddNamespace(“y”, “;);
mg.AddNamespace(“w”, “;);
mg.AddNamespace(“d”, “;);

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”]


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.


Leave a Reply

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

You are commenting using your 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