06/01/2012

Creating service proxy and context in CRM 2011

In this post i'll talk about two different ways that I have used for creating service context for accessing CRM information through code.
After creating the service context we can access CRM entities using "EntitySets".
For instance, after we've created our service context (Let's name the variable "context"), we can use the following code to retrieve contacts who's birthday is today:


var contacts = context.ContactSet.Where(c => c.BirthDate == DateTime.Now).ToList();
So Let' generate the context.


first generate your early bound classes using CrmSvcUtil. open a command window and change the directory to the Bin folder of your downloaded CRM SDK and modify the following and run it:


CrmSvcUtil.exe /codeCustomization:"Microsoft.Xrm.Client.CodeGeneration.CodeCustomization, Microsoft.Xrm.Client.CodeGeneration" /out:Xrm\Xrm.cs /url:http://YourCrm/YourOrg/XRMServices/2011/Organization.svc /domain:YourDomain /username:AdminUserName /password:AdminPassword /namespace:Xrm /serviceContextName:XrmServiceContext

this will generate Xrm.cs file which will include our early bound classes.


Create a new C# console app, add an "Application Configuration File" to the project, change the project target framework to ".NET Framework 4.0"

the above should be straight forward, so i will skip elaborating on it but the following screen shot should clarify what needs to be done prior to generate service context:


We are now ready for jumping inot the code


Approach 1: Using CRMServiceContext

1-1 App.config

update the following and copy it to your App.config file

  
    
this will add the Xrm to your context.

Now you can easily paster this code into program.cs file to retirve the contacts with the birthday value as today's from CRM.

class Program
    {
        public static ClientCredentials ClientCredentials { 
            get { 
                var clientCredentials = new ClientCredentials();

                clientCredentials.Windows.ClientCredential = new NetworkCredential("CRMAdmin","CRMAdminPass","Domain"); 

                return clientCredentials;

            }
        }
        static readonly string TargetCrmService = ConfigurationManager.AppSettings["TargetCrmService"];

        static void Main(string[] args)
        {
            var serviceProxy = new OrganizationServiceProxy(new Uri(TargetCrmService),
                                                             null,
                                                             ClientCredentials,
                                                             null);
            serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
            

            var context = new CrmServiceContext(serviceProxy);

            
            var contacts = context.ContactSet.Where(c => c.BirthDate.Value.Date == DateTime.Today.ToUniversalTime().Date).ToList();

            foreach (var contact in contacts)
            {
                if (contact.BirthDate != null)
                    Console.WriteLine(string.Format("{0}'s Birthday is Today {2}", contact.FirstName, contact.BirthDate.Value.Date ));
            }

            Console.ReadLine();
        }
    }

Approach 2: Using XRMServiceContext

Here is another way to have the same results. Modify and copy the following into your app.config file


  
    

and the following into program.cs:

    class Program
    {
        static void Main(string[] args)
        {
            var context = new XrmServiceContext("Xrm");

            var contacts = context.ContactSet.Where(c => c.BirthDate.Value.Date == DateTime.Today.ToUniversalTime().Date).ToList();

            foreach (var contact in contacts)
            {
                if (contact.BirthDate != null)
                    Console.WriteLine(string.Format("{0}'s Birthday is Today {2}", contact.FirstName, contact.BirthDate.Value.Date));
            }

            Console.ReadLine();

            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();

        }
    }


No comments:

Post a Comment