IronPython CRM 2011 Script for Copying Forms

So I’ve got an entity which has two very similar and very long forms. I made changes to one form which I needed to replicate on the other. I didn’t want to do that manually because that seemed like a very tedious and longwinded piece of work. So instead I used some code.

I did this using an IronPython script and Determination my IronPython/CRM 2011 tool.

EDIT: In hindsight this may not have been such a great idea, this duplicated lots of IDs which caused problems when importing to another environment, :/

import clr

clr.AddReference('Microsoft.Xrm.Sdk')
clr.AddReference('Microsoft.Xrm.Client')

import Microsoft.Xrm.Sdk
import Microsoft.Xrm.Client.Services

connection = Microsoft.Xrm.Client.CrmConnection.Parse(r'[connection string goes here]')
service = Microsoft.Xrm.Client.Services.OrganizationService(connection)

# This assumes the froms have unique names within the system
source = 'Form A'
target = 'Form B'

query = Microsoft.Xrm.Sdk.Query.QueryExpression('systemform')
query.ColumnSet = Microsoft.Xrm.Sdk.Query.ColumnSet('formxml')
query.Criteria = Microsoft.Xrm.Sdk.Query.FilterExpression()
query.Criteria.AddCondition('name', Microsoft.Xrm.Sdk.Query.ConditionOperator.Equal, source)

sourceFromXml = service.RetrieveMultiple(query).Entities[0]['formxml']

print 'got form xml'

query = Microsoft.Xrm.Sdk.Query.QueryExpression('systemform')
query.ColumnSet = Microsoft.Xrm.Sdk.Query.ColumnSet('formxml')
query.Criteria = Microsoft.Xrm.Sdk.Query.FilterExpression()
query.Criteria.AddCondition('name', Microsoft.Xrm.Sdk.Query.ConditionOperator.Equal, target)

targetId = service.RetrieveMultiple(query).Entities[0].Id

print 'got target id'

e = Microsoft.Xrm.Sdk.Entity('systemform')
e.Id = targetId
e['formxml'] = sourceFromXml

service.Update(e)

print 'done'
Advertisements

One thought on “IronPython CRM 2011 Script for Copying Forms

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s