CRMBuzz.net

Abe Saldana on Microsoft Dynamics CRM Development
posts - 23, comments - 0, trackbacks - 0

Friday, March 20, 2009

Converting FetchXML ResultSet to a Business Entity Strong Type

Recurrently I have the same issue on how to get strong type CRM objects from a FetchXML resultset, so I was looking on the SDK documentation and found that there are new code samples on how to accomplish this behavior indirectly from a conversion process, the classes that will help you converting the Fetch to a QueryExpression are FetchXmlToQueryExpressionRequest and FetchXmlToQueryExpressionResponse the SDK give you a small taste of the functionallity, only giving you the return QueryExpression, this is the code from the SDK documentation

 

   1:  [C#]
   2:  // Set up the CRM service.
   3:  CrmAuthenticationToken token = new CrmAuthenticationToken();
   4:  // You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
   5:  token.AuthenticationType = 0; 
   6:  token.OrganizationName = "AdventureWorksCycle";
   7:   
   8:  CrmService service = new CrmService();
   9:  service.Url = "http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
  10:  service.CrmAuthenticationTokenValue = token;
  11:  service.Credentials = System.Net.CredentialCache.DefaultCredentials;
  12:   
  13:  // Create the request object.
  14:  FetchXmlToQueryExpressionRequest fetch = new FetchXmlToQueryExpressionRequest();
  15:   
  16:  // Set the Fetch Xml to be converted.
  17:  fetch.FetchXml = "<fetch mapping='logical'><entity name='account'><all-attributes/></entity></fetch>";
  18:   
  19:  // Execute the request.
  20:  FetchXmlToQueryExpressionResponse qe = (FetchXmlToQueryExpressionResponse) service.Execute(fetch);
 
 

So I created a helper class to enable the conversion and also to run the QueryExpression and return the BusinessEntity{} array.

 

   1:          public BusinessEntity[] Executefetch(string query)
   2:          {
   3:   
   4:              FetchXmlToQueryExpressionRequest request = new FetchXmlToQueryExpressionRequest();
   5:              request.FetchXml = query;
   6:   
   7:              FetchXmlToQueryExpressionResponse response = (FetchXmlToQueryExpressionResponse)service.Execute(request);
   8:   
   9:              BusinessEntityCollection items = service.RetrieveMultiple(response.Query);
  10:              return items.BusinessEntities;
  11:   
  12:          }

 

The implementation is so simple and easy to understand, you send the FetchXML and is converted to a QueryExpression and then the query runs and gets the related CRM strong type object back.


This is a sample code on how to use the ExecuteFetch method, the code will get all related records for a user  with only the open and not started task records.

 

   1:          public void SampleCode2(CrmService service)
   2:          {
   3:              Guid userId = new Guid("{6997B343-7AA0-DD11-9FDF-001F29E24CD0}");
   4:   
   5:              string fetch1 = string.Format(@"<fetch mapping='logical'>
   6:    <entity name='task'>
   7:      <all-attributes />
   8:      <order attribute='scheduledend' descending='true' />
   9:      <filter>
  10:        <condition attribute='ownerid' operator='eq' value='{0}' />
  11:        <condition attribute='statecode' operator='eq' value='Open' />
  12:        <condition attribute='statuscode' operator='eq' value='2' />
  13:      </filter>
  14:    </entity>
  15:  </fetch>", userId.ToString());
  16:   
  17:              FetchHelper fHelper = new FetchHelper(service);
  18:              BusinessEntity[] Response = fHelper.Executefetch(fetch1);
  19:   
  20:              foreach (task myTask in Response)
  21:              {
  22:                  string subject = myTask.subject;
  23:                  string desc = myTask.description;
  24:                  string ownerName = myTask.ownerid.name;
  25:                  Guid activityId = myTask.activityid.Value;
  26:                  string creationDate = myTask.createdon.date;
  27:                  if(myTask.regardingobjectid != null)
  28:                  {
  29:                      string regardingId = myTask.regardingobjectid.Value.ToString();
  30:                      string regardingIdType = myTask.regardingobjectid.type;
  31:                  }
  32:              }
  33:   
  34:   
  35:          }

 

 

After all this the conversion code was so simple to put together and easy to read, I hope the following code snippet's will work for you, keep in mind that I just put the core of the code, CRM connection objects and class definition are not included you need to create those and then test the code

 

Abe Saldaña

CrmBuzz.net

Everything here, though, is my personal opinion and is not read nor approved before being posted. No warranties or other guarantees will be offered as to the quality of the opinions or anything else offered here.

posted @ Friday, March 20, 2009 7:02 PM | Feedback (0) | Filed Under [ Level 300 MS Dynamics CRM 4.0 SDK code Unsupported ]

Powered by: