How to enable and disable entities in Microsoft CRM 2011

The problem :

In my current project I need to enable and disable some entities, and because there is no direct method on generated proxy class (via crmsvcutil.exe), I created small helper with help from CRM SDK. Particular chunk of code that i’ve used, can be found here : or here

The code :

Following is the snippet (method is static as you see, I placed it in static class CRMHelpers and call it as neccessary) :

         /// <summary>
        /// Sets the state of the entity.
        /// </summary>
        /// <param name="serviceProxy">The service proxy.</param>
        /// <param name="EntityId">The entity id.</param>
        /// <param name="EntityLogicalName">Name of the entity logical.</param>
        /// <param name="StateValue">The state value.</param>
        /// <param name="StatusValue">The status value.</param>
        static public void SetEntityState(IOrganizationService serviceProxy, Guid EntityId, string EntityLogicalName, int StateValue, int StatusValue) {
            SetStateRequest setStateRequest = new SetStateRequest() {
                EntityMoniker = new EntityReference {
                    Id = EntityId,
                    LogicalName = EntityLogicalName
                State = new OptionSetValue(StateValue),
                Status = new OptionSetValue(StatusValue)

You can download the source code here :

How to use the code :

Following is the snippet in one of my classes where I use the helper code.

	/// <summary>
	/// Opens the opportunity.
	/// </summary>
	/// <param name="entity">The entity.</param>
	protected void OpenOpportunity(Opportunity entity) {
		CRMHelpers.SetEntityState(crmService, entity.Id, Opportunity.EntityLogicalName, (int)OpportunityState.Open, -1);

There are two values, that you supply to the SetEntityState : StateValue and StatusValue. These make a pair and if you supply unavailable pair, you will get an exception. You can obtain valid pair from existing entity, or you can use this sample : Retrieving State and Status Options via the MetadataService that worked with CRM 4.0. I haven’t tried it on CRM 2011 (aka 5.0). As you can see I’m using cast from Enum that is generated via crmsvcutil.exe utility from SDK for StateValue and hard coded value for StatusValue.

Additional stuff :

In addition there are some other related tasks, that I found on the internet and you may be interested in :

How to enable and disable whole organization in CRM 2011

General discussion about enabling/disabling entities in CRM2011

Sample code to activate / deactivate record in CRM 2011


In case of any questions, ask them in the comments.


One comment

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

  1. Tarek · July 4, 2011

    Hi, i found this VERY helpful, but i have a problem and i need you help.
    the problem is that i have this project and a i need to make a plugin that check if the record is active or inactive, i have done some of it but still need help hope you can help me with it