Tuesday, 7 March 2017

Generating Early Bound and MS CRM Proxy for MSCRM Online

1) First please Generate MSCRM proxy file by using below CRMSVCUTIL.EXE

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="url" value="https://.crm8.dynamics.com/XRMServices/2011/Organization.svc"/>
    <add key="out" value="TestAppXrmProxy.cs"/>
    <add key="namespace" value="TestMsApp"/>
    <add key="serviceContextName" value="XrmServiceContext"/>
    <add key="username" value="username"/>
    <add key="password" value="password"/>
  </appSettings>
</configuration>

2) Add helper class
#region Assembly Needed
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Sdk;
using System.ServiceModel.Description;
using System.Net;
using Microsoft.Xrm.Sdk;
#endregion

namespace TestMsApp
{
    public static class ServiceHelper
    {
        private static IOrganizationService _service = null;
        private static OrganizationServiceProxy _serviceProxy = null;
        private static XrmServiceContext _ServiceContext = null;
        //Check error to be logged in CRM
        static bool isErrorLogged = false;

        /// <summary>
        /// InitializeCRMService
        /// </summary>
        /// <returns></returns>
        private static IOrganizationService InitializeCRMService()
        {
            try
            {
                string url = "https://r/XRMServices/2011/Organization.svc";
                ClientCredentials Credentials = new ClientCredentials();
                //Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
                //Not using default credentials since we need WCF Service for IFD deployment
                Credentials.UserName.UserName = "user name";
                Credentials.UserName.Password = "password";
                Uri organizationUri = new Uri(url);
                _serviceProxy = new OrganizationServiceProxy(organizationUri, null, Credentials, null);
                _serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
                _serviceProxy.EnableProxyTypes();
                _service = (IOrganizationService)_serviceProxy;
            }
            catch (Exception ex)
            {
           
                    throw ex;
            }
            return _service;
        }

        /// <summary>
        /// GetXRMCRMServiceContext
        /// </summary>
        /// <returns></returns>
        public static XrmServiceContext GetXRMCRMServiceContext()
        {
            try
            {
                _service = InitializeCRMService();

                _ServiceContext = new XrmServiceContext(_service);
               
            }
            catch (Exception ex)
            {
               
                    throw ex;
            }
            return _ServiceContext;
        }
    }
}

3) Define proxy Object
   private static XrmServiceContext serviceContext;
   serviceContext = ServiceHelper.GetXRMCRMServiceContext();

Enjoy MSCRMING !