views:

371

answers:

1

Hi all,

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?

+1  A: 

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.

benjynito
Okay, that's an okay option. I've been trying to use a DynamicEntity, but it took me too long to figure out how to get the actual value of the properties using DynamicEntities. Do you have any suggestions? I could get the name and the type, but not the value...
Lex
My comment was too long, see the body.
benjynito
I'll look into it tomorrow, thanks in advance!
Lex
I will mainly be using String properties, but also some Lookups. I did try using the SDK, but I ran into problems using custom fields when I did that. IE in the SDK the code wouldn't compile if I did something like tmp.new_customfield1. (where tmp is a contact). Probably also this will be solved by using DynamicEntities. I'm gonna check out your method using DynamicEntities.
Lex
Yeah, this works! Great, thanks!
Lex
Great! Yea, with custom fields and the SDK you will have to use the indexer. The WSDL has the benefit of the strongly typed properties, but even with that I find the SDK far easier to use. You can actually find strong type generators out there for MSCRM too, so you can use the SDK AND use tmp.new_customfield1.
benjynito