Working With SharePoint Web Services
One of the most confounding things about working with the SharePoint web services is that the return values are all in XML strings (wrapped in an XmlNode).
To make working with the services even more puzzling, suppose you get a result like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<font face="Courier New"><font color="#3040fe"><</font><font color="#ff0000">ContentType </font> <font color="#ff0000">ID</font><font color="#3040fe">=</font><font color="#ff00ff">"0x010100347433A509750F4D88880291599D314D04" </font> <font color="#ff0000">Name</font><font color="#3040fe">=</font><font color="#ff00ff">"My Content Type" </font> <font color="#ff0000">Group</font><font color="#3040fe">=</font><font color="#ff00ff">"My Custom Content Types" </font> <font color="#ff0000">Version</font><font color="#3040fe">=</font><font color="#ff00ff">"7" </font><font color="#00e100">xmlns</font><font color="#3040fe">=</font><font color="#ff00ff">"http://schemas.microsoft.com/sharepoint/soap/"</font><font color="#3040fe">></font> <font color="#000000"> </font><font color="#3040fe"><</font><font color="#ff0000">Folder TargetName</font><font color="#3040fe">=</font><font color="#ff00ff">"_cts/My Content Type" </font><font color="#3040fe">/></font> <font color="#000000"> </font><font color="#3040fe"><</font><font color="#ff0000">Fields</font><font color="#3040fe">></font> <font color="#000000"> </font><font color="#3040fe"><</font><font color="#ff0000">Field ... </font><font color="#3040fe">/></font> <font color="#000000"> </font><font color="#3040fe"><</font><font color="#ff0000">Field ... </font><font color="#3040fe">/></font> <font color="#000000"> </font><font color="#3040fe"><</font><font color="#ff0000">Field ... </font><font color="#3040fe">/></font> <font color="#000000"> </font><font color="#3040fe"><</font><font color="#ff0000">Field ... </font><font color="#3040fe">/></font> <font color="#000000"> </font><font color="#3040fe"></</font><font color="#ff0000">Fields</font><font color="#3040fe">></font> <font color="#000000"> </font><font color="#3040fe"><</font><font color="#ff0000">XmlDocuments</font><font color="#3040fe">></font> <font color="#000000"> </font><font color="#3040fe"><</font><font color="#ff0000">XmlDocument </font> <font color="#ff0000"><font color="#003300"> </font>NamespaceURI</font><font color="#3040fe">=</font><font color="#ff00ff">"http://schemas.microsoft.com/sharepoint/v3/contenttype/forms"</font><font color="#3040fe">></font> <font color="#000000"> </font><font color="#3040fe"><</font><font color="#ff0000">FormTemplates </font> <font color="#00e100">xmlns</font><font color="#3040fe">=</font><font color="#ff00ff">"http://schemas.microsoft.com/sharepoint/v3/contenttype/forms"</font><font color="#3040fe">></font> <font color="#000000"> </font><font color="#3040fe"><</font><font color="#ff0000">Display</font><font color="#3040fe">></font><font color="#000000">DocumentLibraryForm</font><font color="#3040fe"></</font><font color="#ff0000">Display</font><font color="#3040fe">></font> <font color="#000000"> </font><font color="#3040fe"><</font><font color="#ff0000">Edit</font><font color="#3040fe">></font><font color="#000000">DocumentLibraryForm</font><font color="#3040fe"></</font><font color="#ff0000">Edit</font><font color="#3040fe">></font> <font color="#000000"> </font><font color="#3040fe"><</font><font color="#ff0000">New</font><font color="#3040fe">></font><font color="#000000">DocumentLibraryForm</font><font color="#3040fe"></</font><font color="#ff0000">New</font><font color="#3040fe">></font> <font color="#000000"> </font><font color="#3040fe"></</font><font color="#ff0000">FormTemplates</font><font color="#3040fe">></font> <font color="#000000"> </font><font color="#3040fe"></</font><font color="#ff0000">XmlDocument</font><font color="#3040fe">></font> <font color="#000000"> </font><font color="#3040fe"></</font><font color="#ff0000">XmlDocuments</font><font color="#3040fe">></font> <font color="#3040fe"></</font><font color="#ff0000">ContentType</font><font color="#3040fe">></font> </font> |
You would expect to be able to retrieve all of the <Field /> elements by executing the following code:
1 |
<font face="Courier New"><font color="#000000">XmlNodeList nodes = xmlResponse.SelectNodes(</font><font color="#ff00ff">"//Field"</font><font color="#000000">);</font></font> |
But it’s not quite so straight forward. You actually have to instantiate an XmlNamespaceManager with a bogus prefix:
NameTable table = new NameTable();
XmlNamespaceManager manager = new XmlNamespaceManager(table);
manager.AddNamespace(“sp”, “http://schemas.microsoft.com/sharepoint/soap/”);
XmlNodeList nodes = xmlResponse.SelectNodes(“//sp:Field”, manager);
In this case, I used “sp” as my prefix. Notice that it’s utilized in the XPath query as well.