Saturday, July 28, 2018



         Functions Can be used to Authenticate Directly with MSCRM Web services



1. On Premise MSCRM Authenication Function


public static IOrganizationService GetXrmService(string _crmUserName, string _crmURL, string _crmDomain, string _crmPassword)
        {

            Uri organizationUri = new Uri(_crmURL);
            ClientCredentials cCredentials = new ClientCredentials();
            cCredentials.UserName.UserName = string.Format("{0}\\{1}", _crmDomain, _crmUserName);
            cCredentials.UserName.Password = _crmPassword;
            OrganizationServiceProxy orgProxy = new OrganizationServiceProxy(organizationUri, null, cCredentials, null);
            orgProxy.EnableProxyTypes();
            _xrmService = (IOrganizationService)orgProxy;
            return _xrmService;
         

        }

2. Online Authenication function

       public static IOrganizationService GetOnlineXrmService(string _crmUserName, string _crmURL, string _crmDomain, string _crmPassword)
       {

           ClientCredentials credentials = new ClientCredentials();
           credentials.UserName.UserName = _crmUserName;
           credentials.UserName.Password = _crmPassword;
           credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
           //  credentials.Windows.ClientCredential = new System.Net.NetworkCredential(_userId, _password, _domain);

           Uri organizationUri = new Uri(_crmURL);
           Uri homeRealmUri = null;
         

           using (_serviceProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null))
           {
               _serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
               _xrmService = (IOrganizationService)_serviceProxy;
               _orgContext = new OrganizationServiceContext(_xrmService);
           }

           return _xrmService;

       }

Friday, February 9, 2018

CRM cannot find the plugin assembly or one of its dependencies”
OR
 How to use IL Merge with CRM Online ?
OR
 How to merge DLLs ?



Hi ,

D365 allows to deploy a single dll into the platform. Hence when required to use reference dll


CRM cannot find the plugin assembly or one of its dependencies  is a common scenario.


With Dynamics 365 round the corner, Situation of using  I L Merge is increasing every day.


While working on a plugin we encountered a situation where we had to use a reference dll.


CRM cannot find the plugin assembly or one of its dependencies.

To Resolve this We have used the Microsoft Tool ILmerge

There can be two methods to resolve the error

1) Copy the Dll which needs to be merged into the Ilmerge folder with references such as Microsoft.Xrm.Sdk.Proxy , Microsoft.Crm.Sdk , Strong Key File Which is used to sign the assemblies.

2) Also copy and paste mscorsn.dll from “C:\Windows\Microsoft.NET\Framework64\v4.0.30319” on your PC.

Remember Key must be of .snk type. (Dll Can be merged using .pfx but may  not work with CRM)




Type The Following Command to Merge the dll into cmd.

C:\Program Files (x86)\Microsoft\ILMerge>ilmerge /keyfile:Ilmerge.snk  /target:library /copyattrs /targetplatform:v4,"C:\Windows\Microsoft.NET\Framework64\v4.0.30319" /out:CompletePlugin.dll Plug_Ilmerge.dll Plug_Data.dll


Definition  :
CompletePlugin.dll – is a merged Dll
Plug_Ilmerge.dll  -- is a1st  plugin which is merged.
Plug_Data.dll – is a 2nd plugin which is to be merged.




Other Way is using Nugget Packages in VS 2013 onwards using IL which I would write off some other day.


Sunday, December 14, 2014



Preferred Error Handling Technique


function myErrorHandling()
{
// put in code that generates an error
// change this code to throw various error types
// and see how the errors are being captured
try
{
throw new URIError("This is a URIError thrown...");

// throw new RangeError("This is a RangeError thrown...");
// throw new TypeError("This is a TypeError thrown...");
// throw new SyntaxError("This is a SyntaxError thrown...");
// throw new Error("This is a Custom Error thrown...");
}
catch(err)
{
switch(err.name)
{
case "URIError":
alert(err.name + " || " + err.message);
break;
case "RangeError":
alert(err.name + " || " + err.message);
case "TypeError":
alert(err.name + " || " + err.message);
break;
case "SyntaxError":
alert(err.name + " || " + err.message);
break;
case "CustomError":
alert(err.name + " || " + err.message);
break;
}
}
finally
{
alert("This block executes at all times");
}
}

Blocking The Events 



1 ) function BlockSave(context)
{
var _canSave = Xrm.Page.getAttribute("new_cansave").
getSelectedOption().text;
if(_canSave == "No")
{
Xrm.Page.context.getEventArgs().preventDefault();
}
}

Associate the BlockSave function to the form OnSave event.
Make sure you check the checkbox for Pass execution context
as first parameter.



2)  Forcing a Save

Xrm.Page.data.entity.save();

3) Save and New
Xrm.Page.data.entity.save('saveandnew');

4) Save and Close

Xrm.Page.data.entity.save('saveandclose');

Error Handling in MS-CRM



1) Handling UnExpected User Input

Some standard field formats in CRM include elements of user input
validation. Upon data entry, users are prompted if their input is
invalid, and the focus returns to the field, while the incorrect
input is cleared. Take the date field as an example. Enter in a
date field a string of abc123 to observe the behavior. The
following prompt gets displayed:

Once you click on OK, your input is cleared, and the focus is
returned to the field you were trying to modify. But let's see how
we can achieve the same result on a standard text field.
We want to check that the input starts with a capital letter, is
longer than three characters, and does not contain spaces. If these
requirements are not met, we'll bring up a notification message,
clear the field, and return the focus to it.




HOW To Achieve Desired Result

function CheckUserInput()
{
var _userInput = Xrm.Page.getAttribute("new_singlename").
getValue();
var _isValid = false;
if(_userInput != null && _userInput != "")
{
if(_userInput.match(/^[A-Z][a-z]+$/))
{
www.it-ebooks.info
Chapter 5
111
_isValid = true;
}
}
if(_isValid == false)
{
// clear the field
_userInput = "";
Xrm.Page.getAttribute("new_singlename").
setValue(_userInput);
// alert
alert("The input is not valid!");
// set focus
Xrm.Page.getControl("firstname").setFocus(true);
Xrm.Page.getControl("new_singlename").setFocus(true);
}
}


In the first part, we retrieve the user input, we check to make
sure that there is in fact some input, and we run it against a regular
expression.
In the second part, once we have determined that there is no match,
we execute our script to clear the field, bring up the notification
alert for the user, and set the focus back to the same field.



2)  Types Of Error encountered in General with MS-CRM

a) Generating a URIError: We are decoding a string that is not a valid
URL, resulting in the following type of error:
try
{
decodeURIComponent("%");
}

b) Generating a RangeError: In this example we are checking that a
defined age is lower than 100, and we throw a new RangeError with
our own custom message:

var _age = 120;
try
{
if(_age > 100)
{
throw new RangeError("Age cannot be over 100");
}
}

c) Generating a ReferenceError: A reference error is the result of,
in this case, trying to access a variable or method that is not defined.
Observe though that when running this code, you will see a TypeError instead.
That is the default browser behavior, as follows:
try
{
// use an undeclared variable
trying.thisone;
// TypeError due to browser
}

d) Generating an EvalError: An EvalError, while not a common one, is the result
of using the eval function in an unsupported manner. In this case, we use it
as a declaration. Again, due to browser-specific interpretation, the actual
error returned is a TypeError:
try
{
var y = new eval();
// TypeError due to browser
}

e) Generating a SyntaxError: Contrary to the name of this error, it does not have
anything to do with improper syntax. When encountering improper syntax, the error
you will see will be more along the lines of the following screenshot:
A SyntaxError is the result of improper use of the eval() function. In our example
we are evaluating the sum of a string with an integer, thus resulting in a syntax
error:
try
{
var _x = "some string";
var _y = 10;
var _total = eval(_x + _y);
}

Different Types of Events Supported by Jscript in MS-CRM



1) OnLoad : This is a form event. Executes when a form is loaded.
Most common use is to filter and hide elements on the
form.

2) OnSave : This is a form event. It executes when a form is saved.
Most common use is to stop an operation from
executing, as a result of a failed validation procedure.

3) TabStateChange : This is a form event. It executes when the
DisplayState of the tab changes.

4) OnChange : This is a field specific event. It executes
when tabbing out of a field where you've changed
the value. Please note that there is no equivalent
for onfocus and onblur.

5) OnReadyStateComplete : This event indicates that the content
of an IFrame has completed loading.


2) Preventing the Default behaviour of MS-CRM form


function StopSave(context)
{
var _isSpecialSelection = null;
var _isSpecial = Xrm.Page.getAttribute("new_isspecialcustomer");
if(_isSpecial != null)
{
_isSpecialSelection = _isSpecial.getValue();
}
if(_isSpecialSelection == false)
{
alert("You cannot save your record while the Customer is not a
friend!");
context.getEventArgs().preventDefault();
}
}

3) Show and Hide a Tab

function ShowTab()
{
Xrm.Page.ui.tabs.get("notes and activities").setVisible(true);
}

function HideTab()
{
Xrm.Page.ui.tabs.get("notes and activities").setVisible(false);
}


4) Hide and Show a Section

function HideSection()
{
Xrm.Page.ui.tabs.get("notes and activities").sections.
get("activities").setVisible(false);
}

function ShowTab()
{
Xrm.Page.ui.tabs.get("notes and activities").setVisible(true);
}




Saturday, December 13, 2014



Using Filtered Lookup Funtionality in MS-CRM 



Shown with concept of Responsible contact on case form 
and  can also be used for city ,State , Country.

 And Finally Populating Values in the Base Field
address1_city , address1_state , address1_country in case of city state country lookup








One of the most common Customized  features in Microsoft Dynamics CRM has
 always been filtered lookup fields.  
For example, say you replace country and state
text fields with lookup fields—it is a very common request to filter the available states 
based on the country selected. 

 CRM has taken a major step forward with the introduction of 
filtered lookup fields.  In this example, I want to filter the Case form “Responsible Contact” 
lookup to only include the contacts associated with the selected Case account. From the
 case form customization view, I’m going to select the “Contact” field. By clicking 
the “Change Properties” button, I will see the field properties for the contact lookup.










  Under the Related Records Filtering section of this form, I can check a check box that will filter the available values of this lookup field to records related to the value selected in another lookup field.  In this example, I said to only show contacts contained in the case account. Note that I can select whether or not to allow users to turn off the filter—this gives me the ability to choose whether my filter is “hard,” limiting my users to just the filtered records, or “soft,” allowing users to select something outside of the filter.  This is important because some filters are there to prevent users from selecting invalid record combinations, while others are there for user convenience, but may need to be overridden for exceptions.




 JScript to be used with City , State and Country Look up Fields 




function UpdateCountry()
{
var _country = new Array();
_country = Xrm.Page.getAttribute("new_country").getValue();
if(_country != null)
{
Xrm.Page.getAttribute("address1_country").setValue(_country[0].name);
}
}
function UpdateState()
{
var _state = new Array();
_state = Xrm.Page.getAttribute("new_state").getValue();
if(_state != null)
{
Xrm.Page.getAttribute("address1_stateorprovince").setValue(_state[0].name);
}
}
function UpdateCity()
{
var _state = new Array();
_state = Xrm.Page.getAttribute("new_city").getValue();
if(_state != null)
{
Xrm.Page.getAttribute("address1_city").setValue(_state[0].name);
}
}