Retrieving QueryString paramaters in a CRM Plug-In


Retrieving QueryString parameters inside of a CRM plug-in was easier than I thought, but with just one caveat.  After you register your parameter with your entity's form view, you can then retrieve those parameters using the HttpContext class. Just a reference to System.Web to your plug-in assembly and then call it just like you would in any other web applicatioon:

using System.Web;

public void Execute(IServiceProvider serviceProvider)
{
     var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext
));
     var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory
));
     IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

     if (HttpContext.Current.Request.QueryString["MyParameter"] != null)
     {
          string id = HttpContext.Current.Request.QueryString["MyParameter"]
;
          ...
     };
};
 

The one caveat to this is if you need your custom parameter logic available to calls made to your entity via the SDK.  In that scenario, you may want to consider adding an additional field(s) to your entity which could store your parameter data and have your plug-in logic retrieve that data from your entity just as you would any other field on the entity. You would then create an OnLoad event on your entity's form view which would take that custom parameter data from the QueryString and store it within those new fields for storage.

Skip to main content