Let’s assume that we have an HTTPClient object as described in Part 1. That is we have

created the HttpClient objected,

set the base URL to our Sharepoint site,

set the Authentication Header; and

set the Accept Header for “application/atom+xml”.

Now we want to query the REST API for the metadata of a Sharepoint List. Sharepoint List Titles are unique by design we can use the List Title (rather than the GUID) to locate the item and return its metadata. The format of this call as a uri fragment is:

web/lists/GetByTitle(‘listname’)

Now our code looks like:

string uri=”web/lists/GetByTitle(‘Master Document Library’)”;

//Note that spaces are allowed in most calls but there are other situations where spaces are escaped!

This will put a string representation of the XML formatted metadata about the list into the HttpResponseMessage. Please note that the call to extract the XML from the response body:

string respString = resp.Content.ReadAsStringAsync().Result;

is only appropriate for string results (XML or JSON as specified in our Accept Header) and is not correct if the return is binary data. I will copy binary data in a subsequent blog when I discuss file upload and download.

Few things are less documented than the exact contents of the metadata feed returned by calls like this from the REST API. On a high level it is an Atom feed which allows for a combination of collections of Property nodes (key/value pairs) and collections of Link nodes. The Property Nodes are the meta data fields related to the list and the Link Nodes are uri segments to guide additional REST API calls concerning what ever item is currently being queried. Neither the properties nor the links are a fixed are vary from Sharepoint object to object and even vary between objects of the same type if those object depending on the values of the property fields (for example a list item on a list with versioning will contain a Link node for access to the versions. If the list items are not versioned then the link item of versions will not be emitted.

Rather than list XML directly I will use the visualization tool XMLSPY to display the XML in a “grid view”. On a high level the entry for a list would look like:

The Links are on the root of the entry node and the properties are nested as entry/content/properties. Note that the XML makes heavy use of XML namespaces and any creative manipulation of the entry requires some knowledge of XML, namespaces, XPath or LINQ for XML. I use LINQ for XML at my desk so I will use that idiom rather than XPATH to manipulate these objects. If we expand the properties node it will look something like this:

There is a lot of data here about the list most of it only lightly documented. We can see however that the property key: d:title contains the name of our list which we queried on and d:iD contains the GUID for the list. The later never changes but the former can be renamed.

If we expand the Links collection it would look something like this:

Note item 10, the Items link. The href attribute contains the uri for the REST API to retrieve all the items in this list, while the Fields link (item 7) is an uri for the Fields currently defined for this list. If we know the d:Id of a particular item (item IDs are not GUIDS for simple integers), say 6, we can retrieve a single item with the uri of the Items uri and post pending in the form of:

Web/list(guid’850fae4-0cce-8c30-c2a85001e215’)/Items(6)

What about the link with a blank title? For historical reasons this is blank but it represents the EDIT link. To make my life simpler I translate the XML property and link collections into C# Dictionary objects and place them in a C# class with two supporting methods:

public class CEntry

{

public Dictionary<string, string> Links;

public Dictionary<string, string> Properties;

public string GetLink(string key){

string value = string.Empty;

Links.TryGetValue(key, out value);

return value;

}

public string GetProperty(string key)

{

string value = string.Empty;

Properties.TryGetValue(key, out value);

return value;

}

}

At this time I am not using any of the root nodes so I just discard them. I get to the Dictionary objects from the XML using LINQ for XML. I learned what little I know about LINQ for XML from this book. To brush up on your XML try this book. For a XML tree containing entry node(s) my LINQ looks like this: