Search code examples
c#dynamics-crm

What is the recommended approach to consume a ATOM feed from the CRM 2011 REST Service API


This code receives a ATOM feed from the CRM 2011 REST Service API. It works but it seem's very verbose but I can't find a more efficient way to consume the response. I would appreciate any suggestions or if my question needs to be more specific I would ask is this the recommended approach? I have found nothing on this in the CRM SDK or MSDN so far.

XmlDocument xml = new XmlDocument();
xml = xrm.GetXMLData(query);
XDocument xdoc = XDocument.Parse(xml.OuterXml);
XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";
XNamespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
XNamespace atom = "http://www.w3.org/2005/Atom";
foreach (var entity in xdoc.Descendants(atom + "entry"))
{
    var psi = entity.Element(atom + "content")
                     .Element(m + "properties")
                      .Element(d + "new_name");

    var status = entity.Element(atom + "content")
                        .Element(m + "properties")
                         .Element(d + "new_ExternalStatus");

    UpdateExternalDBStatus(psi.Value, status.Value);
}

Solution

  • From memory the API is OData based. Due to the design of OData (and the metadata it provides) you can actually use web-service style "Add Servicde reference" or the command line tools and Visual Studio will automatically construct the classes required for you to query the service. OData also provides an integrated query mechanism, so the classes also provide a LINQ style interface if required.

    This blog post explains consuming OData in a little more detail: http://crdevelopment.net/2011/06/11/ebayodat/