Saturday, 27 December 2014

Microsoft Dynamics CRM 4.0 to 2011/2013 Plugins Migration

Below table consists of major code change while migrating plugins from CRM 4.0 to CRM 2011/CRM2013.

Data type
Microsoft Dynamics CRM 4.0 version
Microsoft Dynamics CRM  2011/2013 version
Dynamic Entity
DynamicEntity has been changed to Entity

if (context.InputParameters.Properties.Contains("Target") &&     context.InputParameters.Properties["Target"] is DynamicEntity)
if (context.InputParameters.Contains("Target") &&
    context.InputParameters["Target"] is Entity)
ICrmService has been changed to IOrganizationService
ICrmService sdk = context.CreateCrmService(true);
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService sdk = factory.CreateOrganizationService(context.UserId);
Lookup classes change to EntityReference
Lookup entity = (Lookup) context.InputParameters.Properties["Target"];
Microsoft.Xrm.Sdk.EntityReference entity = (EntityReference) context.InputParameters["Target"];
Moniker classes change to EntityReference
Moniker entity = (Moniker) context.InputParameters.Properties["Target"];
Microsoft.Xrm.Sdk.EntityReference entity = (EntityReference) context.InputParameters["Target"];
GetProperties / PropertyAccess
Properties property of DynamicEntity no longer exists.
Getting and Setting values no longer use Property classes.

if (context.InputParameters.Properties.Contains("Target") &&     context.InputParameters.Properties["Target"] is DynamicEntity)
if (context.InputParameters.Contains("Target") &&
    context.InputParameters["Target"] is Entity)
BusinessEntityCollection has been changed to Entity]

Picklist in CRM 4.0 has been changed to OptionSetValue in CRM 2011
Var pick =  new Picklist(1);
Var options = new OptionSetValue(1);
Key is no longer a separate thing in CRM 2011, it’s just a System.Guid
Var key = new Key(“<value>”);
Var key = new System.Guid(“<value>”);
SoapException has been changed to FaultException in CRM 2011
Catch(SoapException ex){
catch (FaultException<OrganizationServiceFault> ex){
        // Handle the exception.

Namespace change from Microsoft.Crm.Sdk to Microsoft.Xrm.Sdk
public class ClassName : Microsoft.Crm.Sdk.IPlugin
public class ClassName : Microsoft.Xrm.Sdk.IPlugin
IPluginExecutionContext change
public void Execute(IPluginExecutionContext context)
public void Execute(IServiceProvider serviceProvider)
To get a reference to the IPluginExecutionContext you now need to call the GetService method of the IServiceProvider interface.
public void Execute(IPluginExecutionContext context)
Microsoft.Xrm.Sdk.IPluginExecutionContext context =(Microsoft.Xrm.Sdk.IPluginExecutionContext)
Customer/Owner changed to Lookup

CrmDateTime -> Datetime

CrmBoolean-> Boolean


Crm types change to generic types
attributeValue = ((Microsoft.Crm.Sdk.CrmBoolean)propertyCollection[attribute]).Value.ToString();
attributeValue = ((bool)propertyCollection[attribute]).Value.ToString();

Hope this helps,

Thursday, 18 December 2014

MS 2013 Custom Application not working under CRMAPPPOOL

Recently I faced an Issue in MS CRM2013 while migrating application form other ports to default PORT under MS CRM website (under crmapppool).

Some applications were not working at all.After some analysis I came to know that controls view state was not preserving for the application(I do not know why).

By enabling the viewstate at Page level resolved this issue.

Example :
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default"  EnableViewState="true" %>

Hope this helps,

Wednesday, 1 October 2014

Create a new log file When the Size of Current log file Crosses size limit

Logging messages is always a good practice in development.You can check the log file to know where the application is breaking.Sometimes a log file becomes huge in size and takes some time to open.

Below is C# code to create a new log file When the Size of Current log file Cross size limit.You can define your maximum size of file say 5 MB.

          public static void writeLog(string text)
                long length = 0;
                string fileName = "My_Logger_1" + "_" + DateTime.Now.Year.ToString("0000") + "_" + DateTime.Now.Month.ToString("00") + "_" + DateTime.Now.Day.ToString("00") + ".log";
                string filePath = string.Empty;
                string[] logfileVersion = new string[] { };

                string date = System.DateTime.Now.Year.ToString() + "_" + System.DateTime.Now.Month.ToString() + "_" + System.DateTime.Now.Day.ToString();
                string logpath = @"D:\Logger";
                int MAX_FILE_SIZE = 5242880;//5MB

                filePath = logpath + "\\" + fileName;

                if (File.Exists(filePath))
                     List<String> files = getTodaysFiles(logpath);

                    length = new System.IO.FileInfo(files[files.Count - 1]).Length;
                    logfileVersion = Path.GetFileName(files[files.Count - 1]).Split('_');
                    filePath = files[files.Count - 1];

                StreamWriter LogWriter = new StreamWriter(new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.Read));

                if (length > MAX_FILE_SIZE)
                    int nextVersion = Convert.ToInt32(logfileVersion[2]) + 1;

                    LogWriter = new System.IO.StreamWriter(new FileStream(string.Format(
                    logpath + "\\My_Logger_{0}" + "_" + DateTime.Now.Year.ToString("0000") + "_" + DateTime.Now.Month.ToString("00") + "_" + DateTime.Now.Day.ToString("00") + ".log", nextVersion), FileMode.Append, FileAccess.Write, FileShare.Read));

                LogWriter.WriteLine(DateTime.Now.ToString() + "  -  "+text);

            catch (Exception ex)
                throw ex;

        public static List<String> getTodaysFiles(String folderPath)
            List<String> todaysFiles = new List<String>();
            foreach (String file in Directory.GetFiles(folderPath))
                DirectoryInfo di = new DirectoryInfo(file);
                if (di.CreationTime.ToShortDateString().Equals(DateTime.Now.ToShortDateString()) && di.Name.Contains("My_Logger"))


            return todaysFiles;

Friday, 19 September 2014

MS CRM 2013 Showing a Specific Process Stage

Suppose you have a Business Process Flow with multiple Stages on entity Opportunity say


Now I want to create the opportunity from Stage 3(Propose).Using JavaScript you can easily achieve this.

Solution: We should get the ProcessID and StageID for the Given Business Process Flow.

Get the ProcessID and  use ProcessStage table to get the Stages  where ProcessID equals will display StageID for all the stages. Get the Propose StageID.

Use JavaScript to open the new record :

var parameters = {};
parameters["formid"] = "A837E4A7-01B8-4F82-A475-BE9ABD67E667";//Specific form id
parameters["stageid"] = "1B015DCA-9790-D8D0-0425-54EECEE24900"; // Stage ID for a specific Stage say "Propose"
Xrm.Utility.openEntityForm("opportunity", null, parameters);



Hope it helps,

MS CRM 2011 and 2013 not working properly with Chrome.

One day while opening MS CRM 2011 in chrome browser I found out that it is not showing  modal dialog. Although it was working fine in IE.I thought there is some bug in application.

Some other day I was working on CRM 2013 and I noticed some functionalities are broken in Chrome.

1.Button Click events were not working for a dynamatically created HTML DIV.
2.On Form customization I faced some issues.

Later I found out that it is due to update of Chrome browser (37).They removed  number of API which results in such issues.

Below is the explanation  and workaround of this  issue.

Hope this helps,