Hide Entities from Advanced Find List – MS CRM

What is Issue?

Ms CRM provides the way to query the records in system with or without some filter criteria. There might be some conditions where we need to restrict user from querying for the entity, but still we want to have read privilege to the entity so that we can provide the visibility to the records in some desire areas. But the MS Dynamics CRM don’t have any separate option to hide/show the entities in Advanced find.

The Reason-

Ms CRM handles the entities to be visible in advanced find using Security Role. If Any entity don’t have read privilege for the security role, the entity will be hidden in advanced find for that security role. This is internally handled by “IsValidForAdvancedFind” read-only flag.

Solution-

There are multiple solution to make entity hidden from advanced find like –
     A. Security Role
     B. Database Query
C. RetrieveMultiple Message Override
D. Custom Code

A. Security Role –

Remove the read privilege for security Role you want to hide entity. But this security role will don’t have any read permission for the entity Records.

B. Database Query –

If you have on-premise setup, you will have database control.
So you can make “IsValidForAdvancedFind” flag False to make entity hidden in advanced find only.

C. RetrieveMultiple Message Override –

This is the simple and reliable way to make records available to the user. Create the plugin for the entity with the modified query, so that user will have visibility to the records by default  which you set. This will affect overall application i.e. in subgrids, home view and where ever the retrievemultiple is called.
But this is not suitable for our requirement i.e. Hiding Entity from advanced find without any access changes to the records.

D. Custom Code – Javascript

The way I found is very easy. Within few steps you can achieve it.

Let’s say I want to hide Account and Contact entity from Advanced find.

This slideshow requires JavaScript.

1.Create a new solution with component “Application Ribbon” in it.

AFHideEntities0

2. Create a New Javacript webresource with below Code-
in my case – Name and Display Name – “mct_/Scripts/AdvancedFind.js”

//entities to be hidden
var entities = [
    'account',//Account
    'contact',//Contact
    ];
//slctPrimaryEntity is id of dropdown for entities displayed in Advanced Find.
function hideEntities() {
    var select = $("#slctPrimaryEntity");
    if (select.length > 0) {
        for(var i = 0; i < entities.length; i++)
            $("#slctPrimaryEntity option[value='" + entities[i] + "']").remove();
    }
    //find and hide button which we are going to create in next step.
    var button = window.top.document.getElementById("new.ApplicationRibbon.Button.RemoveEntities-Large");
    if (button !== undefined && button != null) button.style.display = "none";
    return false;
}

3. Open your new solution in Ribbon Workbench.

4. You will find the set of commands which are used in Advanced find. Add new button with minimum information, next to the “DOWNLOAD FETCHXML” button or at the end of this group.

ID : new.ApplicationRibbon.Button.RemoveEntities

AFHideEntities8

AFHideEntities5

5.Create a Command

ID : new.ApplicationRibbon.Command.RemoveEntities
add this command in newly added button :
Command :- new.ApplicationRibbon.Command.RemoveEntities
CommandCore :- new.ApplicationRibbon.Command.RemoveEntities

6. Add Enable Rule to the Command –
AFHideEntities6Enable rule details-

ID : new.ApplicationRibbon.EnableRule.RemoveEntities

Add Custom Rule :-

Default - True
Function Name - hideEntities (our custom Javacsript function name)
Library - $webresource:mct_/Scripts/AdvancedFind.js  (our custom Javascript)

AFHideEntities7

7. Publish the Solution.

And Done.

You can check advanced find – There is no Account and Contact entities available.

This slideshow requires JavaScript.

Hope this will help…

Enjoy Ms CRM!!!

Generate Early Bound Classes for selected entities using CrmSvcUtil.exe in MS CRM v9.x

What is Issue?

There is CrmSvcUtil.exe to generate early bound entity classes. But this utility generates the classes for all the entities in the organization. But these entities are not required all the time. So how to generate classes for selected entities.

The Reason-

The Microsoft provided the CrmSvcUtil.exe utility in Microsoft.crmsdk.coretools which is used to generate early bound classes for entities, but there is no option to select particular entity or entities.

Solution-

CrmSvcUtil.exe is available in Microsoft.crmsdk.coretools package and you can download this package from nuget.

The simple solution is provided by Erik Pool for Ms CRM 2011 and this will work D365 CRM v9.x as well. Just follow the below –

Create a new C# class library project in Visual Studio called SvcUtilFilter.

Add the below assemblies to your class Library-

svcUtil1

Important reference is – CrmSvcUtil.exe
– This exe has interface ICodeWriterFilterService which needs to be implemented.
– Microsoft.Xrm.Sdk is mandatory
– Microsoft.Xrm.Tooling.Connector is important in case of v9.x.
– If you want to connect to your organization by interactive UI add
Microsoft.Xrm.Tooling.Ui.Styles and Microsoft.Xrm.Tooling.CrmConnectControl.

Add the following class to the project:

using System;
using System.Collections.Generic;
using System.Xml.Linq;
using Microsoft.Crm.Services.Utility;
using Microsoft.Xrm.Sdk.Metadata;

namespace SvcUtilFilter
{
    /// CodeWriterFilter for CrmSvcUtil that reads list of entities from an xml file to
    /// determine whether or not the entity class should be generated.
     public class CodeWriterFilter : ICodeWriterFilterService
    {
        //list of entity names to generate classes for.
        private HashSet _validEntities = new HashSet();

        //reference to the default service.
        private ICodeWriterFilterService _defaultService = null;

        /// constructor
        public CodeWriterFilter(ICodeWriterFilterService defaultService)
        {
            this._defaultService = defaultService;
            LoadFilterData();
        }

        /// loads the entity filter data from the filter.xml file
         private void LoadFilterData()
        {
            XElement xml = XElement.Load("filter.xml");
            XElement entitiesElement = xml.Element("entities");
            foreach (XElement entityElement in entitiesElement.Elements("entity"))
            {
                _validEntities.Add(entityElement.Value.ToLowerInvariant());
            }
        }

        /// /Use filter entity list to determine if the entity class should be generated.
        public bool GenerateEntity(EntityMetadata entityMetadata, IServiceProvider services)
        {
            return (_validEntities.Contains(entityMetadata.LogicalName.ToLowerInvariant()));
        }

        //All other methods just use default implementation:

        public bool GenerateAttribute(AttributeMetadata attributeMetadata, IServiceProvider services)
        {
            return _defaultService.GenerateAttribute(attributeMetadata, services);
        }

        public bool GenerateOption(OptionMetadata optionMetadata, IServiceProvider services)
        {
            return _defaultService.GenerateOption(optionMetadata, services);
        }

        public bool GenerateOptionSet(OptionSetMetadataBase optionSetMetadata, IServiceProvider services)
        {
            return _defaultService.GenerateOptionSet(optionSetMetadata, services);
        }

        public bool GenerateRelationship(RelationshipMetadataBase relationshipMetadata, EntityMetadata otherEntityMetadata, IServiceProvider services)
        {
            return _defaultService.GenerateRelationship(relationshipMetadata, otherEntityMetadata, services);
        }

        public bool GenerateServiceContext(IServiceProvider services)
        {
            return _defaultService.GenerateServiceContext(services);
        }
    }
}

Build the project.

Next step to create XML which specifies the entities names for which you want to have early bound class.

Create filter.xml file with below structure-

<filter>
<entities>
<entity>account</entity>
<entity>contact</entity>
</entities>
</filter>

To add entity same way in this XML to create early bound class for it.

Add this filter.xml in same build folder debug/bin i.e. where the CrmSvcUtil.exe and all the other references located.

Now your build folder should be look like this-

svcUtil3

Your tool is ready now.

How to run this?
Open Command prompt. Change directory to the bin folder and run below command-

crmsvcutil.exe /url:http://your_org_url/XrmServices/2011/Organization.svc /out:output.cs /namespace: /il /codewriterfilter:SvcUtilFilter.CodeWriterFilter,SvcUtilFilter

 

(For more information about parameters execute command : crmsvcutil.exe ? )

This will open the UI to connect Organization-

svcUtil2

Connect to your Organization and then wait till your output.cs is ready.

You will be able to find output.cs file in same bin folder once command executed successfully. This file will be having early bound entity classes for your selected entities.

Hope this will help…

Enjoy Ms CRM!!!


		

Color Picker in MS CRM ?

What is Issue?

How to add Color Picker in MS CRM?
There might be situations where you have to use colors in MS CRM and you wish to have wide range of colors. But in MS CRM there is no field or option available for now.

The Reason-

You have multiple custom controls available in MS CRM right now but there is no option for color picker and there is no way to add your own custom control for now.There is no out-of-box Color picker available in MS CRM.

Solution-

There are 2 solution for the color pickers –

  1. You can use Option set and provide user to select limited options of colors.
  2. You can use Custom web resource with your own libraries or open-source libraries.

The first option is reliable, but not much attractive if user wants some colorful UI and wide range of colors.

Using second option is not supported way but it gives you attractive interfaces.

There is open-source library available for color Picker – spectrum

You can use this library with dynamics CRM as web resources. You can have very nice color picker in MS CRM with these libraries.

ColorPicker

 

Hope this will help…

Enjoy Ms CRM!!!

Error -Newtonsoft.JsonJsonSerializationException: Self referencing loop detected for property ‘Entity’ …while setting Icons and ToolTips to the Columns in System Views -MS CRM

What is the Issue?

Microsoft Dynamics CRM provided the feature to set Icons and tooltips to the columns in System views based on some conditions like priority task, critical records, Expiry Dates on records, etc. This helps user to take action on or analyze the records.

But sometimes it simply fails and throws exception before loading the view itself. It becomes difficult to figure out issue behind this.

The Reason-

In earlier version of MS CRM i.e. previous to v9.1 where Microsoft provided the feature to add the icons and tooltips to columns, this is working fine for system views of all the entities except Activity entities like email, appointments, Tasks.

The reason behind this is the Party list fields used in entities which are also used in views. Party List fields are lookup fields of multiple entities.

Let’s have an example-

We have the “All Emails” system view for Email entity which is Activity entity.
In system view , there are two fields “From” and “To” as below-

setIcon3

Let’s add javascript on some field to show the icons and ToolTips-

SetIcon7

And publish the Customizations.
When We try to load the view got error message as belows-

setIcon4

When I downloaded the Log file I got some exact error like-

setIcon5

Even If you try to debug the function, you will not be able to do so. It shows exception before hitting the function. So if you take a look closely to the exception from log file it is showing some Error highlighted in above screenshot.

And here you have exact reason-

'from_Value[0].EntityType.Metadata.PrimaryKey'.

From field is Party List field and System is trying to set metadata value for entity of lookup and MS CRM is failing to parse the rowData for this multi-entity type lookup.

Solution-

Only you can remove the all party List columns from system view to make your javascript work for setting Icons and Tool Tips. (This will not the great solution which makes you view useless). So there is no OTHER solution to resolve this issue in earlier versions of MS CRM.

In latest version of MS CRM i.e. V9.1 and above MS has resolved this bug and you can see this working in new versions on MS CRM-

SetIcon2

You can see the “from” and “to” fields are ignored in these views.
Below is the Json rowData value where you can find the blank values for “from” and “to” fields-

SetIcon1

So the issue resolved in latest versions of MS CRM.

Hope this will help you.

Enjoy MS CRM!!!

Calendar and Calendar Rules in MS CRM

What is the Issue?

Calendar is the great feature provided by MS CRM which is used by the scheduling system to define when an appointment or activity is to occur. But when it comes to be used by other entities in case of calculation of dates which will consider the holidays and weekends , it becomes difficult to handle. OOB there is no option to use calendar directly in entities.

The Reason-

Calendar is only entity directly available in MS CRM to be used by developers, You will not be able to access Calendar Rules entity type record which holds the information of free/busy times for a service and for resources or resource groups, such as working, non-working, vacation, and blocked.

 Solution-

As there is no Out-Of-Box option, You need to go with custom code.

First let’s understand the calendar, Holidays Calendar and Calendar Rules-

You will find the calendar under Setting > Service Management >Customer Service Schedule. You can follow the below screens to configure your Business calendar-
Calender1

You can create Calendars as below with work days, work Hours and Holidays details .
This is the main calendar (with type 1) which is used to consider weekend and work hour in activities and appointments.
Calender2

Holiday Schedule has a lookup for another calendar entity record with type 2 i.e. Holiday Scheduler calendar.
Calender3

You have to create this Holiday calendar as below-Calender4Calender5

You can configure multiple holidays here-
Calender6
Calender7

There is Calendar Rule entity record associated with the Calendar entity ( Service Schedule and Holidays Schedule calendar) which holds information inside calendar like week days , Work Hour and Holiday start , holiday end, etc.

Now coming to the solution for our issue-

As we have the option for custom code,

And In case of c# code- we can use entity  “calendar” and “calendarrule” like-
Entity calendar= new Entity(“calendar”);
Entity calendarRule= new Entity(“calendarrule”);
And use OrganizationService object to play around calendar.

In case of javaScript, we can access the information using WebApi like
[organization URI]/api/data/v9.1/calendars
Use WebApi methods GET, POST, PATCH and DELETE operations.

[organization URI]/api/data/v9.1/calendarrules
It is not possible to perform GET, POST, PATCH and DELETE operations with this entity. This can be retrieved along with Calendar WebApi call, as a sub entity record.

You can refer the below code for javaScript to retrieve the Calendar and Holiday Schedule information-

Calender8

 

Hope this will help you to play with calendar.

Enjoy MS CRM!!!