Question

I have a question about the Dynamics CRM 4.0 Webservice. I've been using it to get records from CRM into ASP.NET. After the request and the casting, the values of the columns (for instance for a contact) can be accessed through;

BusinessEntity be = getBusinessEntity(service, crmGuid, type, colnames);
contact tmp = (contact)be;

Response.Write("firstname: " + tmp.firstname + "<BR>");
Response.Write("lastname: " + tmp.lastname+ "<BR>");

I have an array of strings which identify which columns should be retrieved from CRM (colnames), for instance in this case {"firstname", "lastname"}.

But colnames can become quite big (and may not be hardcoded), so I don't want to go through them one by one. Is there a way to use something like

for(int i = 0; i < colnames.length; i++)
{
    Response.write(colnames[i] + ": " + tmp.colnames[i] + "<BR>");
}

If I do this now I get an error that colnames is not a field of tmp. Any ideas?

Was it helpful?

Solution

Not using BusinessEntity (unless you use reflection). DynamicEntity is enumerable by types deriving from Property. You'll have to do something like (I did this from memory, so might not compile)...

for(int i = 0; i < colnames.length; i++)
{
  string colName = colnames[i];
  foreach(Property prop in tmp)
  {  
    if (prop.name != colName)
      continue;
    if (prop is StringProperty)
    {
       var strProp = prop as StringProperty;
       Response.Write(String.Format("{0}: {1}<BR />", colName, strProp.Value));
    } 
    else if (prop is LookupProperty)
    {
      ...
     }
    ... for each type deriving from Property

  }
}

Reply to Note 1 (length):

Could you give me an example of what you're using. If you are only looking at the base types (Property) then you won't be able to see the value property - you'll need to convert to the appropriate type (StringProperty, etc).

In my example tmp is a DynamicEntity (it defines GetEnumerator which returns an array of Property). The other way to access the properties of a DynamicEntity is using the string indexer. For tmp:

string firstname = (string)tmp["firstname"];

Note that if you use this method, you get the Values (string, CrmNumber, Lookup) and not the whole property (StringProperty, CrmNumberProperty, etc).

Does that answer your question? Also, I recommend using the SDK assemblies and not the web references. They're much easier to use. The SDK download has a list of helper classes if you choose to use the web references, however. Search "Helper" in the SDK.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top