Tuesday, August 9, 2011

Dynamics CRM 2011 : Customize CRM Outlook Ribbon


Microsoft Dynamics CRM ribbon customization allows ribbon to be customized for entity forms, grids, sub grids etc. and it supports both Web and Outlook clients but there are few Ribbons bars that are only specific to Outlook client and ribbon customization does not allow them to change by making changes into ribbon xml customization. The step by step guide below will help outlook user to customize the Outlook specific CRM ribbon bars as per required. In the guide i have taken the example of customizing the E-mail message outlook ribbon bar.

[Instructions]
1. Run Microsoft Office Outlook and open email message

2. Right click on email message ribbon bar and select Customize the Ribbon…



















3. Outlook will not allow renaming the existing CRM group commands; you can delete the existing group and add a new group or can add a new command. In the screenshot below i have added a New Group “My Custom CRM Group”

4. After adding the group, in choose commands from: drop down list select Main Tabs expand Message item and in CRM node under Convert To select Case and click on the Add >> button
5. Select the Case and click on rename button and type Convert to Case




































6. Click OK button to close the window

[Instructions for Exporting and Importing Ribbon Customization]
You can export the ribbon customization and import into other outlook clients or can take customization backup by exporting.

1. In ribbon customization window, after making changes into the ribbon bar select custom group and click on Import/Export button and then select Export all customization and save customization file.



















2. In ribbon customization window, after making changes into the ribbon bar select custom group and click on Import/Export button and then select Import customization file and select the customizations file.
If the customizations are required to be deploy across many CRM outlook clients then follow the instructions as mentioned in the URL to deploy customizations: http://msdn.microsoft.com/en-us/library/ee704589.aspx










Saturday, August 6, 2011

Dynamics CRM 2011 : Perform JScript Validations on Entity Form Before Execution of Special Events


In many development scenarios there is a requirement to perform Jscript validations on entity form before the execution of special events by clicking on the entity form buttons i.e. Activate, Deactivate, Lead Qualify, Resolve Case, Cancel Case  etc. You can use the step by step guidelines below to overcome this scenario. You may also reffer to the table at bottom of this guide for a list of record saved event model values.

[Instructions]

1. Create a new JScript web resource or you can use existing JScript web resource, copy the following Jscript code in the web resource. Make sure to replace <Value> in code line wod_SaveEventVal = <Value>; with the event mode value as per required. For record saved event model values you can refer to the table at the bottom of the guide.



Saturday, July 30, 2011

Dynamics CRM 2011 : C# & JScript Code for Retrieving User Assigned Security Roles


You can use the code below to retrieve the user assigned security roles in C# plugin and JScript.
[Plugin: C# Code]


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;

namespace wod.Crm.UserSecurityRoles
{
    public class wod_Plugin : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            IOrganizationServiceFactory jj_serviceFactory = null;

            IOrganizationService jj_CrmService = null;

            Guid jj_UserGuid = Guid.Empty;

            EntityCollection jj_UserRoles = null;

            string jj_SecurityRole = string.Empty;

            Try
            {
                // Obtain the service factory to get the service object
                jj_serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

                // Obtain service object Imporsonated as plugin calling user
                jj_CrmService = jj_serviceFactory.CreateOrganizationService(context.InitiatingUserId);

                // Setting UserId to plugin initiating User Id
                jj_UserGuid = context.InitiatingUserId;

                //Security Role to Search
                jj_SecurityRole = "Customer Service Representative";

                #region Retrieve records from an intersect table via QueryExpression

                //Create Query Expression to fetch Role Entity
                QueryExpression jj_Query = new QueryExpression()
                {
                    //Setting the link entity condition and filter condition criteria/
                    LinkEntities =
                        {                           
                            new LinkEntity
                            {
                                LinkFromEntityName = "role",
                                LinkFromAttributeName = "roleid",
                                LinkToEntityName = "systemuserroles",
                                LinkToAttributeName = "roleid",
                                LinkCriteria = new FilterExpression
                                {
                                    FilterOperator = LogicalOperator.And,
                                    Conditions =
                                    {
                                        new ConditionExpression
                                        {
                                            AttributeName = "systemuserid",
                                            Operator = ConditionOperator.Equal,
                                            Values = { jj_UserGuid }
                                        }
                                    }
                                }
                            }
                        }
                };

                jj_Query.ColumnSet = new ColumnSet(true);

                // Obtain results from the query expression.
                jj_UserRoles = jj_CrmService.RetrieveMultiple(jj_Query);

                // Searching for a specified Security Role into the list
                Entity jj_UserSecurityRole = jj_UserRoles.Entities.ToList().ToList<Entity>().Find(delegate(Entity jj_RoleEntity)
                {
                    return (string)jj_RoleEntity.Attributes["name"] == jj_SecurityRole;
                });

                if (jj_UserSecurityRole == null)
                {
                    throw new InvalidPluginExecutionException("Security role not assigned to User.");
                }

                #endregion

            }

            catch (System.Web.Services.Protocols.SoapException ex)
            {
                throw new InvalidPluginExecutionException(ex.Detail.InnerText);
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }

    }
}



 [JScript Code]


// Retrieving current login user security roles by using CRM client API method Xrm.Page.context.getUserRoles();
var jj_UserSecurityRoles = Xrm.Page.context.getUserRoles();

// Security role GUID (You can get it by opening Security Role record copy web address and go to the website
// http://meyerweb.com/eric/tools/dencoder , paste the URL and click on Decode button twice then copy Id value

var jj_CustomerServicesRoleId = “{E324246D-B8AF-E011-AA34-C95682B33448}”;
var jj_UserHaveSecurityRole = false;

// Checking if Security Role Id found in User Security Roles
for (var x = 0; x < UserSecurityRoles.length; x++) {

    if (UserSecurityRoles[x] == jj_CustomerServicesRoleId) {
       jj_UserHaveSecurityRole = ture;   
    }
}

if (jj_UserHaveSecurityRole == false) {
   throw new exception (“User has not been assigned Customer Service security role.”)
}