CRM 2013 – How to get guid of initiating record in CustomWorkflow

I have written a customer workflow for a while and I was wondering how I got the guid of the calling record.

I was sure it passed in the entity reference but searching for this was a suprisingly difficult task

I’m not sure if I wasn’t using the correct syntax or what.

If you want to learn about Custom Workflows then I you should start here


Custom workflow activities (workflow assemblies)

here is the Microsoft example

Sample: Create a custom workflow activity


Here is a Hosk example being called from a Dialog, blog and video



So to find out the information I thought about what was being passed into the my Custom Workflow. Here is the template code the CRM Developer toolkit provides you with


         /// <summary>

        /// Executes the workflow activity.

        /// </summary>

        /// <param name="executionContext"> The execution context.</param>

        protected override void Execute( CodeActivityContext executionContext)


            // Create the tracing service

            ITracingService tracingService = executionContext.GetExtension <ITracingService >();

            if (tracingService == null)


                throw new InvalidPluginExecutionException("Failed to retrieve tracing service.");


            tracingService. Trace( "Entered MaintenancePlanPublish.Execute(), Activity Instance Id: {0}, Workflow Instance Id: {1}",

                executionContext. ActivityInstanceId,

                executionContext. WorkflowInstanceId);

            // Create the context

            IWorkflowContext context = executionContext.GetExtension <IWorkflowContext >();

            if (context == null)


                throw new InvalidPluginExecutionException("Failed to retrieve workflow context.");


            tracingService. Trace( "MaintenancePlanPublish.Execute(), Correlation Id: {0}, Initiating User: {1}",

                context. CorrelationId,

                context. InitiatingUserId);

            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory >();

            IOrganizationService service = serviceFactory.CreateOrganizationService (context.UserId );




We can see we have CodeActivityContext and this gets you access to IOrganisationService, tracing and all the other goodies you need to write CRM code.

Then you have this code to get the Workflow context

  // Create the context
            IWorkflowContext context = executionContext.GetExtension <IWorkflowContext >();

Now I clicked on the interface and could see what was available, then I tried looking online but I could only find CRM 4

I went into the chm in the CRM 2013 sdk and it looks very simlar

In here I can see how to get the id

PrimaryEntityId Gets the ID of the primary entity.
PrimaryEntityName Gets the name of the primary entity for the workflow.


When the going gets tough, the tough search the internet

I have no idea why this information was so hard to find.  There doesn’t seem to be much information on CRM 2011 and custom workflows.  I find it puzzling the CRM 2013 SDK is so hard to find.

The process above shows you if the internet doesn’t have the answer then you can puzzle out the answers yourself by looking at the interfaces and SDK to see what is available.

I always recommend people look in the SDK because I always find something interesting.


Magic code

One downside of finding solutions and examples on the internet is you get what I call magic code.  You get the example, change it a bit and it works, but because you didn’t work out how to do it, you don’t always understand how it works.

A lot of the time this is ok because you can work out what is going on and you can work backwards by having a working example, you can see the variables are being assigned and what values they have.

Still often the most valuable part of learning some new development is the journey, making the mistakes and finally understanding how it works using your own steam.

Don’t always grab that magic code or if you do make sure you take some time to understand how it works and lookup the interfaces being used and have a look in the SDK