Friday, July 1, 2011

Server Side example – SetState

Hi,
Here is an example of setstate entity in CRM 2011.
Example project for Console application.

Before run the application musat add reference to dlls:
1. Microsoft.Xrm.Client.dll ( get this file from SDK 2011)
2. Microsoft.Xrm.Sdk.dll ( get this file from SDK 2011)
3. Microsoft.Crm.Sdk.Proxy.dll ( get this file from SDK 2011)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Description;
using System.Net;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk;

namespace crm2011Test1
{
    class SetState
    {
        //static void Main(string[] args)
        //{
        //    Button1_Click();
        //}
        static void Button1_Click()
        {
            //Authenticate using credentials of the logged in user;       
            ClientCredentials Credentials = new ClientCredentials();
            Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
            //This URL needs to be updated to match the servername and Organization for the environment.
            Uri OrganizationUri = new Uri("http://crmserver:5555/crm2011dev/XRMServices/2011/Organization.svc");
            Uri HomeRealmUri = null;
            using (Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy serviceProxy = new Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null))
            {
                serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
                IOrganizationService service = (IOrganizationService)serviceProxy;
                //set state of contact to disable (add microsoft.xrm.client.dll)
                OrganizationServiceContext osc = new OrganizationServiceContext(service);
                Microsoft.Xrm.Client.Messages.OrganizationServiceContextExtensions.SetState(osc, new EntityReference("contact", new Guid("{contactGuid}")), new OptionSetValue(1), new OptionSetValue(2));
                Console.ReadKey();
            }
        }
    }
}

Thanks,
Rami Heleg

No comments:

Post a Comment