In our CRM environment we have a custom entity type in settings for custom parameters in which entities contain a name, type, and value. I need to get one of these "parameters" in a web application using asp and C# to do some math. How might I retrieve the value and type fields of this entity in my aspx.cs file using the entity's ID so that i might make a c# variable that corresponds to the custom parameter entity.
1 Answers
First Way
You can connect to the CRM Web Service, which is an .asmx and retrieve your entity from there. Just add it as a web reference. From my experience adding it as a service reference is troublesome but adding as a web reference is not (since the "Add a service reference" thing is for WCF and "Add a web reference" has been there from the start for .asmx).
As an aside, if you are used to using the CRM SDK through writing plugins and custom workflow activities you may notice:
- Under the hood, the CRM SDK assemblies are actually calling the CRM Web Service anyway...they just wrap it in a nice-ish way to faciltate the development of your plugins etc
- Once you add the web service you'll have a whole bunch of methods to choose from and that your entities are now classes in your ASP.NET application. Because VS will import the WSDL of the service it will recognise your custom entities as first class citizens and generate the appropriate classes (i.e. you don't need to deal a lot with DynamicEntity, unlike in your plugin & custom workflow assembly development)
It is also a good practice to update/refresh your web reference if you make changes to the entities in CRM (e.g. add a field, create a new entity etc) so that your application is in sync. If you only work with a narrow set of entities (sounds like it in this case) and they don't change this isn't essential but still a good practice.
Second Way
You could get it directly out of your _MSCRM database.
If you wanted to go to the raw tables, your entity will have two tables: Base and ExtensionBase. The data you want, if it is an attribute you have defined against the entity, will be in the ExtensionBase table. There will also be an view that joins the Base and ExtensionBase table if you wanted to use that instead.
You could also query the filtered view but if you do this, you need to ensure that the account which your ASP.NET application runs as (so when deployed to IIS this will be the app pool identity) has access to CRM as going against the filtered view runs the CRM security checks against the calling user.
Also, note that going straight to the database table or the view is technically unsupported. Going against the filtered view should be OK.
I would suggest going the first way but I have seen both approaches done in the past and both should work.

- 5,551
- 2
- 16
- 12
-
I am connected to the web service, previous work was done by another programmer however he interacted with the database directly through some crazy sql queries id rather not deal with. I've updated the web service but im just not familiar with how to retrieve a field of a custom entity using the web reference. I'm kinda looking for an example to retrieve fields values of a specific entity (in this case Service Contract Average Rate) which is a custom parameters parameters entity I've created. – Aaron Mar 22 '13 at 16:51
-
my boss would rather i not deal directly with the database as apparently its frowned upon by Microsoft as a bad practice. – Aaron Mar 22 '13 at 16:52
-
I see that i can do crm. and then i start to type retrieve and intellisense shows so many methods its ridiculous. – Aaron Mar 22 '13 at 17:53
-
Sorry for the delayed response - you could retrieve an individual field using a [FetchXML](http://msdn.microsoft.com/en-us/library/bb928434.aspx) to get an entity and limit what comes back in the results. Other than that, you'll need to retrieve the entire entity by it's ID (if you've got it) and then pick the field you need out of the returned DynamicEntity – nkvu Mar 22 '13 at 19:31
-
If you want to use FetchXML the [Stunnware](http://www.stunnware.com/products/tools4/overview.htm) tools, though they are old now and I think Stunnware no longer operates as a full business, may help via the FetchXML wizard, which is part of the free Community Edition: [download](http://www.stunnware.com/products/tools4/download.htm) – nkvu Mar 22 '13 at 19:38