3

I have a C# workflow that I'm trying to create an IOrganzationService in that has the proxy types enabled on it so I can use my early bound data types...

This is how I'm creating the IOrganizationSerivce

IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

But since I don't have an OrganizationServiceProxy object, I can't call EnableProxyTypes() on the service, and any create calls on the service using an early bound entity fails.

I know I can revert to setting the server url url, sdk server url, and organization in the app.config and use that to create an OrganizationServiceProxy but it seems like I should be able to just set the EnableProxyTypes on the IOrganizationService that I already have...

Update

I believe this was a bug in 2011 and maybe 2013 that has long since been resolved. See Jim's answer with a link as to why you should not be using the accepted answer for 2015 or newer instances.

5 Answers 5

4

This is an old post, addressing an issue that was fixed long ago. But some of the workarounds provided here are not supported and will cause workflow activities that have used them to break.

Please see this: Remove unsupported code that uses reflection in custom workflow activities

3

To be able to use the early bound entities in the context of an workflow try the following snippet:

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

var type = Type.GetType("Microsoft.Crm.Workflow.SynchronousRuntime.WorkflowContext, Microsoft.Crm.Workflow, Version=5.0.0.0");
type.GetProperty("ProxyTypesAssembly").SetValue(serviceFactory, typeof(YourServiceContext).Assembly, null); //YourServiceContext - the name of crm context
IOrganizationService service = serviceFactory.CreateOrganizationService(context.InitiatingUserId); 

As you can see there is a property named ProxyTypesAssembly that specifies the assembly where you keep the early bound entities. The only way i could set this property was using reflection because the WorkflowContextBase (the base class of WorkflowContext) was not accessible.

4
  • That's exactly what I needed! I'm surprised this isn't a little more documented...
    – Daryl
    Feb 12, 2012 at 15:50
  • 1
    This appears to be broken in CRM 2013, type is null. Have you come across a work around?
    – Ant Swift
    May 19, 2014 at 17:45
  • @AntSwift see my new answer, below, for the CRM 2013+ fix
    – Jon Grant
    Oct 7, 2015 at 10:28
  • 4
    It is essential that anyone who used this workaround remove it. It is no longer necessary and will cause the workflow activity to break. More information: Remove unsupported code that uses reflection in custom workflow activities Aug 14, 2019 at 23:16
2

The fix for CRM 2013 (and beyond, I hope), is the much simpler:

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

// this is the important change
var property = serviceFactory.GetType().GetProperty("ProxyTypesAssembly");

property.SetValue(serviceFactory, typeof(YourServiceContext).Assembly, null);
IOrganizationService service = serviceFactory.CreateOrganizationService(context.InitiatingUserId); 
1
  • You're able to use reflection in a sandbox environment, as long as you don't access something you wouldn't normally have access to. So you can reflectively call a public method, but not a private one
    – Daryl
    Aug 29, 2017 at 22:45
2

The answers using reflection are not ideal, you will not be able to register your assembly in sandbox isolation mode.

You can add the following to the end of your AssemblyInfo.cs file in the Plugin or Workflow project.

[assembly: Microsoft.Xrm.Sdk.Client.ProxyTypesAssembly()]
0

I found this same issue couple months back. The issue you are experiencing is due to the inconsistencies in the api.

The OrganizationServiceProxy implements IOrganizationService which only has a few members create, update etc. It also inherits from ServiceProxy with members ClientCredentials, IsAuthenticated, DeviceCredentials etc.

Now what you don't find on the interface or the abstract base class is the EnableProxyTypes member.

So basically your outta luck. I ended using concrete OrganizationServiceProxy.

have a look here and do the comparison for yourself.

http://technet.microsoft.com/en-us/library/microsoft.xrm.sdk.iorganizationservice_members.aspx, http://technet.microsoft.com/en-us/library/microsoft.xrm.sdk.client.organizationserviceproxy.aspx, http://technet.microsoft.com/en-us/library/gg306039.aspx and http://technet.microsoft.com/en-us/library/microsoft.xrm.sdk.client.organizationserviceproxy_methods.aspx

Guess the ms crm dev team creating the api should have used tdd and a mocking framework to pick it up early in development.

Cheers Rustin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.