How to change State/Status in CRM 2013 using c#

GCL picture GCL · Dec 17, 2013 · Viewed 16.4k times · Source

How do I change State and/or Status of an entity in CRM 2013 using C#?

Neither CRM 2011's SetStateRequest nor the earlier SetStateDynamicEntityRequest are being recognized. Searching online didn't seem to help either... Am I missing a namespace or something?

Thanks in advance

Namespaces:

using System;  
using System.Collections;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Net;  
using Microsoft.Xrm.Sdk;  
using Microsoft.Xrm.Sdk.Client;  
using Microsoft.Xrm.Sdk.Query;  
using Microsoft.Xrm.Sdk.Discovery;  
using Microsoft.Xrm.Sdk.Messages;  
using System.ServiceModel;  
using System.ServiceModel.Description;  
using System.Web.Services.Protocols;  

The function:

private void changeUserStatus(Guid userId, IOrganizationService service, int state)  
{  
    SetStateRequest setStateRequest = new SetStateRequest  
    {  
        EntityMoniker = new EntityReference("systemuser", userId),  
        State = new OptionSetValue(state),  
        Status = new OptionSetValue(-1),  
    };  
    service.Execute(setStateRequest);  
}  

IMPORTANT: My SetStateRequest is NOT recognized by visual studio 2012, so unlike the above example it's not highlighted and actually red underlined with the "the type or namespace not found" message displayed when hovering

Edit: I see the question got downvoted and I'm sorry if I'm missing something but I really have no more information to provide apart from the fact that I've spent 4 hours yesterday looking for this online (including stackoverflow) without any avail. I may be using the wrong search criteria or looking in the wrong places, but if that's the case could anyone at least point me in the right direction? I'm a beginner on all of this (both stackoverflow and crm2013) and I'm kind of stuck on this. Thanks again

Answer

Guido Preite picture Guido Preite · Dec 18, 2013

You are not referencing microsoft.crm.sdk.proxy.dll and its namespace Microsoft.Crm.Sdk.Messages

so the code will be

using Microsoft.Crm.Sdk.Messages;

Another thing, be sure that the int values for State and Status are valid for the entity (in your case systemuser)