Blog, Development, Howto

How to get type of the instance (production/sandbox/something else) using code

I was working on the plugin where logic had to work differently depending on the type of the instance, i.e. if code is executed in Sandbox environment – do A, if code is executed in Production – do B. I never developed anything similar before and search engines did not bring anything valuable. After research I found RetrieveOrganizationInfo message with correspond request and response classes. After few tests and poking around I came up with following code that could be used (to make code shorter and easier to read I dropped part of it where IOrganizationService was instantiated):

var instanceSettings =
	(RetrieveOrganizationInfoResponse)crmService.Execute(new RetrieveOrganizationInfoRequest());

if (instanceSettings.organizationInfo.InstanceType == OrganizationType.Customer ||
    instanceSettings.organizationInfo.InstanceType == OrganizationType.Secondary)
{
	//this instance is production - run this logic here
} 
else if (instanceSettings.organizationInfo.InstanceType == OrganizationType.CustomerTest) 
{
	//this instance is sandbox - run this logic here
} 
else
{
	//this is some other type of the instance
	//add required code here if you want to handle this situation as well
}

If you’re interested what are other types you should check the description of OrganizationType enumeration. I checked few instances I had an access to and value of InstanceType for it was either Customer or Secondary – I don’t know what’s the difference between those 2 and if you have an explanation – please leave a comment under this post. Sandboxes I checked returned CustomerTest value.

If you need to get this information not from .Net code or from .Net code where SDK assemblies are not available for some reason – you can use WebApi and RetrieveOrganizationInfo function.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.