Wednesday, October 29, 2014

How to hide Add Existing Associated Ribbon Button for specific Custom Entity in MSCRM 2013/ Use of “Form Entity Context Rule”

Following is the link with helped me solve the issue, however to explain in detail I will take my own example
Consider we have a custom entity called “new_demo” which has 1:N relationship with account , contact and any other custom entity

Now say for example we want to hide the “+ Add NEW DEMO” sign that shows up on the associated view only if the “new_ demo” entity subgrid is placed on account otherwise in all other cases we want to keep showing the  “+ Add NEW DEMO”.

To achieve the same via Ribbon workbench we need to follow the following steps.
·         Create a new solution and add the “new_demo” entity to that solution.

·         Open the solution in Ribbon work bench 2013

·         Select the “new_Demo” entity in the entities section of the ribbon workbench

·         Go to the subgrid section and select the “+ Add NEW DEMO” (note demo will be replaced with the name of the entity you are working with)


·         Clicking on “Customize command” will add a new command to the command tab

·         Select the command and right Click on it to edit display rules


·         Clicking on “Edit display rules will pop up the screen where you can add a new display rule by clicking on “Add New”


·         Click Add new and give a name to the new display rule and then click add step


·         In the steps select “form Entity Context Rule” and then set the values as following 


·         The above values will hide the “+ Add NEW DEMO” button from the associated view of the “new_demo” entity on account entity, on all other entities the button will keep showing up on the associated view of the new_demo entity. You can use the above to hide the button for your specific entity. Similarly changing these values can tweak the results in a way we want to hide/show the button on different entities.

How to refer multiple files in a ribbon workbench/editor or in JavaScript code

Sometimes we need to make a reference to multiple JavaScript files in our code. Say for e.g. you want to refer to some functions written in a common JS file in the new JS code you are writing in an entity specific file. At the form level you can include those files in the form properties however it becomes a problem when we are referring to multiple files from a ribbon. There are multiple ways to resolve the issue
Ribbon Workbench
Ribbon workbench is the preferred tool we use to do customizations of the ribbon in MSCRM 2013.You can refer to multiple file in a ribbon workbench in the following way
·         Add all those web resources you want to refer in a solution.
·         Open that solution in the ribbon workbench
·         Go to the command definition where you want to refer to multiple JS files
·         Right click and then click on “Edit Actions” as shown in image below


·         It will pop up an Actions window which will have an “Add” button at the bottom of the screen.
·         Click on “Add” and it will give you the option to select “JavaScript Function Action”
·         Click on that and then enter “isNaN” as the function name, also select the web resource which you want to refer in your other web resource but do not call any function directly from that web resource. (shown in image below)
·         Set the sequence of the web resources and you are good to go.
Manual Export way:
If however you are not using ribbon workbench then you can manually export the solution with the entity in whose ribbon you want to refer to multiple file and include the file names in the actions tag of the command definition e.g.

<Actions>
       <JavaScriptFunction FunctionName="isNaN" Library="$webresource:demo_sharedLibrary" />
       <JavaScriptFunction FunctionName="Getdetails" Library="$webresource:demo_entity1_Ribbon.js" />
</Actions>
Refer in code
Another approach to address the problem is directly referring the files in the code. Use the following line of code to achieve that

<reference path="../WebResources/demo_sharedLibrary/" />

Friday, September 12, 2014

Get a date without time in JavaScript

Very often we need to compare two dates in javascript

So for example you need to get today’s date to compare it with any other date you will use the following var todayDate = new Date();

But again with this you will also get the time in this, if however you do not need to time part use the following function
var todayDate = new Date(new Date().setHours(0, 0, 0, 0));

So to do a date compare your function will look something similar to the function below


function comapreDates(context) {
    var date1  = Xrm.Page.ui.controls.get("new_date1").getAttribute().getValue();
    var todayDate = new Date(new Date().setHours(0, 0, 0, 0));
    if (date1  != null) {
        if (date1  < todayDate) {
            alert("date1 cannot be less than today");
            context.getEventArgs().preventDefault();
            return false;
        }
    }

}

Sunday, May 4, 2014

How to call custom cmdlet’s from PowerShell Script/How to create a custom cmdlet project in PowerShell

·         Open visual Studio

·         Create a Class library project

·         Add a new class file
The file will have be inheriting “System.Management.Automation.Cmdlet” .The file will also decorate the class file with the following verb [Cmdlet(VerbsCommon.Add, "NameOfYourFunctionality")]
Some sample code showing a sample powershell file


    [Cmdlet(VerbsCommon.Add, " NameOfYourFunctionality ")]
    public class MyClass : System.Management.Automation.Cmdlet
    {
        [Parameter(Position = 0, Mandatory = true)]
  public string para1;
   // define more parameters here
protected override void ProcessRecord()
        {

              //write your functionality here
 }

Add more class files if you need to build your functionality

·         Add a new installer file
Add the following code to the installer class
    [RunInstaller(true)]
    public class Installer : PSSnapIn
{
       public override string Name
        {
            get
            {
                return "NameWithWhichYouWantToRegisterWhenYouImportusing Add-PSSnapin";
            }
        }
}
}

·         Strong sign your assembly
·         In order to call this custom cmdlet from a script file you may have to first register this assembly in the GAC using the GACUTIL , once that is done you can use this custom cmdlet using the following commands in the script file

Add-PSSnapin NameWhichYoudefinedInTheNamePropertyOfinstallerFile -ErrorAction "Stop"

How to invoke/call custom cmdlet from another custom cmdlet in powershell

For example you have a requirement where you are writing custom cmdlet cmdLET1 which in turn needs to invoke cmdLET2.In such a scenario the challenge remains how to pass the parameters to the second cmdlet being invoked and also how exactly to invoke it.

Shown below is the sample code which shows how do we achieve such a scnerio

     PowerShell pShell = PowerShell.Create();
            Runspace runSpace = RunspaceFactory.CreateRunspace();
            runSpace.Open();
            Pipeline pipeline = Runspace.DefaultRunspace.CreateNestedPipeline();
            Command cmd = new Command("Add- cmdLET2");

            CommandParameter cmdPara1 = new CommandParameter("parameter1", value1);
            cmd.Parameters.Add(cmdPara1);

            CommandParameter cmdPara2 = new CommandParameter("parameter2", value2);
            cmd.Parameters.Add(cmdPara2);

            pipeline.Commands.Add(cmd);

            pShell.Commands.AddCommand("import-module").AddParameter("Name", "Add- cmdLET2");
            pipeline.Invoke();


The above code when called from cmdLET1 will invoke cmdLET2 with values passed in the CommandParameter’s

How to debug PowerShell custom Cmdlet project

In order to debug PowerShell go to the following path

Solution explorer à Right Click properties à debug

Here we need to configure the following three values

Start external program : mention the location of the powershell directory here usually it is “C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe”

Next define Command line argument : -noexit -command Add-PSSnapin XXXX.PowerShell (replace  XXXX.PowerShell with the value you have defined in the “Name” property of the installer file )


Finally define the Working Directory : This is location where your project is building and placing the dlls, 

Note : In some cases you may require to copy dll from this location to the the GAC of the server, therefore every time you make a change in your code you may be required to update the dlls in the GAC. You may also need to sign your assembly with a strong name in case the same has to be copied to GAC.


Tuesday, December 10, 2013

remove an class object from a List in C# using IEqualityComparer

While removing simple datatypes(int, string, datetime etc) from List is quite simple it is not the case with Complex data types (some custom class) if it has to be removed from needs to be done using IEqualityComparer
 
I had this requirement where I needed to compare two set of Lists to identify which are the new sets of records to be created and which are the one already existing and therefore needs to be just updated.
 
A had a custom class called LookupNameValue which has certain name , value properties , now in order to compare two set of Lists in C# to find out I had to implement the IEqualityComparer for the LookupNameValue class
 
Sample code to implement IEqualityComparer : note that we have to implement method Equals and GetHashCode to use IEqualityComparer
public class ValueComparer : IEqualityComparer<LookupNameValue>
    {
 
        public bool Equals(LookupNameValue x,LookupNameValue y)
        {
            if (x.Value.Equals(y.Value))
                {
                    return true;
                }
                else
                {
                    return false;
                }
        }
        public int GetHashCode(LookupNameValue obj)
        {
            return obj.Name.GetHashCode();
        }
    }
 
Sample LookUpNameAndValue class
public class LookupNameValue : IComparable
    {
        private Guid _value;
        private string _name;
public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
 
        public Guid Value
        {
            get
            {
                return _value;
            }
            set
            {
                _value = value;
            }
        }
}
 
Now once we have implemented the IEqualityComparer interface we need to use the same for filtering out the existing dataset.This is done by using Except method of the List
Sample Code to use the custom comparer
 
ValueComparer comparer = new ValueComparer();
IEnumerable<LookupNameValue> finalCreateTeamDataset = createTeamDatasetTemp.Except(updateDatasetToBeRemoved, comparer);
 
Where createTeamDatasetTemp and updateDatasetToBeRemoved are List of my generic class LookupNameValue
List<LookupNameValue> createTeamDatasetTemp = new List<LookupNameValue>();
List<LookupNameValue> updateDatasetToBeRemoved = new List<LookupNameValue>();
The filtered dataset could then be used as following
 
foreach (var data in finalCreateTeamDataset)
{
 //do ur job
}
 
Hope this is helpful to you all