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
Description
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)
CrmService
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
Lookup classes change to EntityReference
Lookup entity = (Lookup) context.InputParameters.Properties["Target"];
Microsoft.Xrm.Sdk.EntityReference entity = (EntityReference) context.InputParameters["Target"];
Moniker
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
BusinessEntityCollection has been changed to Entity]


Picklist
Picklist in CRM 4.0 has been changed to OptionSetValue in CRM 2011
Var pick =  new Picklist(1);
Var options = new OptionSetValue(1);
Key
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
SoapException has been changed to FaultException in CRM 2011
Catch(SoapException ex){
}
catch (FaultException<OrganizationServiceFault> ex){
        // Handle the exception.
            }

CrmSdk
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
IPluginExecutionContext change
public void Execute(IPluginExecutionContext context)
public void Execute(IServiceProvider serviceProvider)
IPluginExecutionContext
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)
    serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext)
Customer/Owner
Customer/Owner changed to Lookup


CrmDateTime -> Datetime

CrmBoolean-> Boolean

CrmDecimal->Decimal

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




Hope this helps,
Yusuf

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.
EnableViewState="true"

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)
        {
            try
            {
                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.Close();

                    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);
                LogWriter.Close();


            }
            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"))
                {
                    todaysFiles.Add(file);

                }

            }
            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

1.Quality
2.Develop
3.Propose
4.Close

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 same.it 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);

Result:


 


Hope it helps,
Yusuf




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.
http://xrmrocks.com/2014/09/08/chrome-37-breaks-crm-2011-functionality/

http://www.crmanswers.net/2014/09/google-chrome-registry-fix-for-dynamics.html


Hope this helps,

Yusuf





Thursday, 12 December 2013

Get the record ID from the URL

As a developer, you will find yourself extracting record ID from the URL.Most of the time we open the notepad and using our  ID extracting skills we strip out the ID from the URL.To make this task more easier just use URL Decoder/Encoder


1. Copy and paste your URL in the textbox
2. Press Decode button two times.
3.Your URL is decoded you can easily copy the ID.



Hope this helps.

Wednesday, 14 August 2013

Showing only selected Tab on the MS CRM 2011 Form

When you open any form it loads all the tabs on the form.Suppose you want to display the selected tab on the form at a time.Here is a JavaScript function to hide the other tabs.This function can be called on the Form OnLoad and TabStateChange events.

1.JavaScript Function:

function ToggleTabDisplayState(name) {
 
    var currentTab = Xrm.Page.ui.tabs.get(name);
    if (currentTab != null && currentTab.getDisplayState() == "expanded") {
        var currentTabId = currentTab._control.get_id();
        var tabs = Xrm.Page.ui.tabs.get();
        for (var i in tabs) {
            var tab = tabs[i];
            var tabId = tab._control.get_id();
            if (tabId != currentTabId) {
                if (tab.getDisplayState() == "expanded") {
                    tab.setDisplayState("collapsed");
                }
                document.getElementById(tabId).style.display = "none";
            }
        }
    }
}
2.Before Calling the Function :
Form shows all the tabs before calling the above function Form OnLoad event.
 
 
 
 3.Calling the Function :
 
3.1 Call on Form OnLoad event :
 
Suppose on Form OnLoad event I want to show only "General" tab and hide other tabs then call the above function as below.
 
function Form_OnLoad() {
    ToggleTabDisplayState("general"); //"general" is the name of the tab
}
 
3.2 Call on TabStateChange event:
 
Call the same function on the TabStateChange event 
 
Go to Form Properties =>Select Control as tab name=>Add Handler =>Select Library=>add function as ToggleTabDisplayState =>Provide name of the tab in Comma separated parameters list for example 
"contacts" => Save and publish


4.After Calling the Function :
Form shows only selected tab after calling the above function Form OnLoad event.
 
 










Hope it helps,
Regards,
Yusuf