Thursday, April 19, 2012

CRM2011: Export entities from one organization and import to another while changing the ownership of the entity.

In this post I will demonstrate, with code examples, how to export your structure from one org to another using the SDK.
Before I really got knee deep in it, my first inclination was to modify the customizations.xml file and import into the new org. The idea of modding the xml is a great way to  However considering the idea of creating relationships and lookup fields, this practice always fails.
The solution lies within the SDK of CRM 2011.

The basic concept is this:
1. Retrieve the entity structure from one organization.
1b. Modify structure as needed. In my case I need to change the ownership to user/team owned but you can also add fields, views, etc.
2. Loop through the custom entities and add to new organization.

In reality, at a low level viewing, it works like this:
1. Retrieve the entity structure from one organization.
2. Loop through the custom entities and add to new organization.
2a. Find entities where prefix matches yours to filter out system entities.
2b. Modify structure as needed. In my case I need to change the ownership to user/team owned but you can also add fields, views, etc.
3. Create new entity, minus any attributes.
4. Create attributes that aren't of type Lookup.
5. Do Steps 2 and 2a.
6. Create one to many relationships for each entity. This will create the many to one to the corresponding entity and also the lookup attribute you filtered out earlier.
7. Create many to many relationships.
8. Complete.

SDK objects used:
enums for componenttype and systemformtype. located at helpercode\optionsets.cs
Microsoft.Xrm.Sdk.Messages
Microsoft.Xrm.Sdk.Metadata
AttributeTypeCode
RetrieveAllEntitiesRequest
RetrieveAllEntitiesResponse
EntityMetadata
AttributeMetadata
CreateEntityRequest
CreateAttributeRequest
ManyToManyRelationshipMetadata
CreateManyToManyRequest
CreateOneToManyRequest
LookupAttributeMetadata

Members to create:

  • member using RetrieveAllEntitiesRequest and RetrieveAllEntitiesResponse to get entity structure.
  • member using CreateEntityRequest to create entity.
  • member using CreateAttributeRequest to create entity. This member will need to filter using AttributeTypeCode.Lookup.
  • member using CreateOneToManyRequest and LookupAttributeMetadata to explicit bind a new relationship. The lookup needs to be created in the request.
Considerations:
  • When using RetrieveAllEntitiesRequest set the EntityFilters property to All. This will take longer but will grab all attributes and relationships if you want to do this in one routine.
  • When looping through, do a conditional to only run process on entities that are custom.
  • In the CreateEntityRequest Notes and Activities won't acknowledge you structure setting so set these booleans.
  • In the CreateEntityRequest Primary Attribute has to be created. Set the RequiredLevel to None.
  • When using CreateAttributeRequest, loop through the EntityMetadata where IsCustonAttribute is true and attributeof is null. AttributeOf seems to correlate to a lookup.
  • When using the CreateOneToManyRequest, set the ReferenceEntity and the ReferencingEntity.
  • When using the CreateOneToManyRequest lookup property, get the displayname by using the localizedlabels[0].Label property.
Good Luck and comment with any questions.

No comments:

Post a Comment