Create, Get, Delete Custom Entity via API
Friday, October 5, 2007 at 11:16PM One of the new features of CRM 4 (Titan) is the ability to not only read from the platform Metadata but to be able to modify it though the API as well.
If you are familiar with CRM3, you were only able to do these type of changes though the web client, or by importing existing customization files.
There's lots of things you can do with this capability, but for this post, I'm just going to use the API to create a custom entity, read it back and finally delete it to cleanup.
First an up front disclaimer, the SDK docs are still a little thin here so take the following example with a grain of salt, and use it to understand the concept not as a reference for all the features.
To Create the Custom Entity, we need to do a CreateEntityRequest
Before you call the Execute there's one more required step, you must setup the Primary Attribute - in this case I'm going to use Name as the primary attribute.
Note: In CTP3 if you do not set the MaxLength property you will get an Unexpected Exception from the API
And finally, we actually call the Execute method on the Metadata service to perform the request
Now at this point we could jump over to the web client and check the list of custom entities and we should see the new entity on the list. The other thing we could do is use the RetreiveEntityRequest and do the same check using the API
Finally, using the API, I will just delete the new custom entity I just created
One thing you might notice above is the fact that when I set the DisplayName and Description Properties on both the Entity and the Attribute I did it by calling a helper method called CreateSingleCRMLabel. Those properties are of type CrmLabel which is designed to support the multi language aspects of CRM 4 and allow you to set one or more language strings for each of the properties. In a future post, I will elaborate more on how you can use the CrmLabel but for now here's a simple code snippet for the helper method
So with the ability to do Metadata modifications using the API you can now can do things like automate the creation of your custom schema as part of your install program. It also makes it so in the uninstall routine you can remove attributes or entities if you want to completely remove your solution from a schema. In a future blog post I will look at how you can surgically add custom attributes one at a time because that is real useful when your adding your solution to an existing entity.

Reader Comments