Tuesday, 24 January 2012

Programmatically Sharing a Record in CRM 2011


In this article, I am going to explain, how to share a record programmatically

To share a record we need to use these namespaces

Namespace need to include
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Sdk.Messages;


Code to share a record with specific user 


GrantAccessRequest grantRequest = new GrantAccessRequest()
{
   Target = new EntityReference(Account.EntityLogicalName, _accountId),
   PrincipalAccess = new PrincipalAccess()
   {
      Principal = new EntityReference(SystemUser.EntityLogicalName, _userId),
      AccessMask = AccessRights.WriteAccess | AccessRights.ReadAccess | AccessRights.ShareAccess
   }
};

// Execute the request.
GrantAccessResponse grantResponse = (GrantAccessResponse)_service.Execute(grantRequest);


Code to remove share access from specific user


ModifyAccessRequest modifyRequest = new ModifyAccessRequest()
{
  Target = new EntityReference(Account.EntityLogicalName, _accountId),
  PrincipalAccess = new PrincipalAccess()
  {
   Principal = new EntityReference(SystemUser.EntityLogicalName, _userId),
   AccessMask = AccessRights.None
  }
};

// Execute the request.
ModifyAccessResponse modifyResponse = (ModifyAccessResponse)_service.Execute(modifyRequest);


Code to modify share access for specific user


ModifyAccessRequest modifyRequest = new ModifyAccessRequest()
{
  Target = new EntityReference(Account.EntityLogicalName, _accountId),
  PrincipalAccess = new PrincipalAccess()
  {
   Principal = new EntityReference(SystemUser.EntityLogicalName, _userId),
   AccessMask = AccessRights.ReadAccess | AccessRights.ShareAccess
  }
};

// Execute the request.
ModifyAccessResponse modifyResponse = (ModifyAccessResponse)_service.Execute(modifyRequest);


Code to retrieve user share access  for specific record 


RetrievePrincipalAccessRequest retrieveRequest = new RetrievePrincipalAccessRequest()
{
    Target = new EntityReference(Account.EntityLogicalName, _accountId),
    Principal = new EntityReference(SystemUser.EntityLogicalName, _userId)
};

// Execute the request.
RetrievePrincipalAccessResponse retrieveResponse = (RetrievePrincipalAccessResponse)_service.Execute(retrieveRequest);


Code to retrieve all users share access from specific record


RetrieveSharedPrincipalsAndAccessRequest retrieveSharedRequest = new RetrieveSharedPrincipalsAndAccessRequest()
{
    Target = new EntityReference(Account.EntityLogicalName, _accountId)
};

// Execute the request.
RetrieveSharedPrincipalsAndAccessResponse retrieveSharedResponse = (RetrieveSharedPrincipalsAndAccessResponse)_service.Execute(retrieveSharedRequest);


Code to revoke the share access for specific record


RevokeAccessRequest revokeRequest = new RevokeAccessRequest()
{
    Target = new EntityReference(Account.EntityLogicalName, _accountId),
    Revokee = new EntityReference(SystemUser.EntityLogicalName, _userId)
};

// Execute the request.
RevokeAccessResponse revokeResponse = (RevokeAccessResponse)_service.Execute(revokeRequest);


Thursday, 19 January 2012

Show all sub grids data in CRM 2011 entity form (More then 4+ Sub grids)


In CRM 2011, Due to performance reason only four sub grids shows data and other sub grids will show data when we will click on "To load entity records - click here" link button
But some time we need to show all sub grids data and in this article I am going to explain , how we can show all sub grids data
With the help of little JavaScript code we can show all sub grids data.


function loadsubgrids() {
var _clsname = 'ms-crm-List-LoadOnDemand'
    var _body = document.getElementsByTagName("body")[0];
    var _arr = [];
    var _regexp = new RegExp("\\b" + _clsname + "\\b");
    var _lnks = _body.getElementsByTagName("a");

    for (var i = 0, j = _lnks.length; i < j; i++) {
        if (_regexp.test(_lnks[i].className))
            _arr.push(_lnks[i]);
    }
    for (var i = 0; i < _arr.length; i++) {
        _arr[i].click();
    }
}


Put this JavaScript code on form "onload" event, in this JavaScript code I am finding "To load entity records  - click here" link class name (To check class name of link use IE developer tool”) and forcefully executing its click event

Wednesday, 4 January 2012

CRM 2011 Solution package using Visual Studio 2011


 
Now latest version of MS CRM 2011 include “Developer Toolkit” . Developer toolkit
include in SDK at “SDK\Tools\DeveloperToolkit”. Double click the
“CrmDeveloperTools_Installer.msi” to install the toolkit. Using this toolkit we 
can create and deploy custom workflow activity ,
Plugin and javascript in VS2010
 
In this Article I am going to explain how this toolkit works
 
1. After installing toolkit , open VS2010 and in left templates pane you can see
 Template with name “Dynamics CRM”. Click on Template now you see, In middle pane have
five option and in this project I am using first one .
 

 
2. As you select and click on OK button . Dialog box to connect CRM Server will appear
 
In this dialog box enter CRM Server name, Port number and protocol and click on connect
button. After this “Authentication Details” section will enable and fill your username,password and domain and click “Log On” Button
 
After this Organization and Solution Name picklist will enable and you can select your organization
name and select solution for development and as you click ok , all Type of solutions
come in “Solution Explorer” window and new Silverlight application dialog will display
 
 
Now you will see two explorer windows
 
1) CRM Explorer
 
2) Solution Explorer
 
Now with the help of SDK, I am explaining all features of both explorer windows
 
CRM Explorer :
 
Item Organization Name
Command Action Refresh
Description Refreshes the whole tree


 
Item Entities
Command Action Click on Entity Expand button
Description When you will click on entity expand button . you will see list of all entities

Command Action Refresh
Description Refreshes Entities
Command Action Generate Wrapper
Description Executes CRMSVCUTIL.exe to generate the strongly typed proxy classes. If you only have one plug-in project, this class is automatically added to this project; otherwise , the Generate Entity Wrappers dialog box lets you select which project to create the classes in.

Steps I need to generate strongly typed proxy classes using developer tool

Right Click on Entities node and from context menu select “Generate Wrapper” option



















Now "Generate Entity Wrapper" Dialog box will appear and you need to select project and after that click on “Generate” button











Now processing dialog box will appear and it will take few second or minute depend on your system configuration











As this dialog box will disappear you will see “Entities.cs” class under “Plugins” project



 
Command Action Create Plug-in for specific entity
Description Steps to Create Plug-in

Right Click on specific entity node and from context menu select “Create Plugin” option


Now “Create Plug-in” dialog box will appear .
















Select Message and fill unsecure/secure configuration message and click on “OK” button
















After clicking “OK” button , new plugin class will appear under “Plugin” Project solution and write your plugin logic in this class
























Plugin also appear in “CRM Explorer” under “Plug-in Assemblies”
















After adding plug-in, Now you need to signing your both workflow and plugin project with a strong name key file















Rebuild all Solutions




















Now time to deploy “CrmPackage” Solution






















As you deploy , plugin will registered and you can see plugin and workflow dll in “Crm Explorer” with different style















You can Add/Edit/Delete plugin and its step



















Now I am explaining how “CrmPackage” Solution store all information need to deploy

All information store in “RegisterFile.crmregister” under “CrmPackage” Solution

When Project start RegisterFile.crmregister file status






After Creating Step in plugin RegisterFile.crmregister file status










After deploying Step in plugin RegisterFile.crmregister file status



 
Command Action Double click on specific entity
Description Now no need to go back in CRM web client for customization . double click on entity name and entity customization form will display in VS 2010


Item Option Sets
Command Action Refresh
Description Refreshes option sets













Expand “Option Sets” node show all option sets


 
Command Action Double click on specific options set
Description Opens the option set definition


 
Item Web Resources
Command Action Refresh
Description Refreshes Web resources


 
Command Action Add to packaging project
Description Adds web resources of all types from the organization to the project



















“Add to packaging project” will add new folder with name “WebResources” in “CrmPackage” Solution with all webresources present in CRM Solution
















You can add new webresources also from VS2010



 
Item Process
Command Action Refresh
Description Refreshes Processes

On expanding process node will show “Dialogs” and “Workflows” nodes


 
Command Action Refresh specific dialog
Description Refreshes dialogs
 
Command Action Double click on specific dialog
Description Opens the dialog definition
 
Command Action Add/Edit/Delete specific dialog
Description We can add, edit, delete dialog from solution














As you click on “Add Workflow” , Create Process dialog box will appear










Fill process name and select entity from Entity dropdown










As you click on create button new process will display under workflow node and its information will also appear for further steps as you see in CRM webclient


 
Item Security Roles
Command Action Refresh
Description Refresh Security roles


 
Command Action Double click on specific security role
Description Opens the security role definition
 
Item Field Security Profiles
Command Action Refresh
Description Refreshes field security profiles

 
Command Action Double click on specific Security Profile
Description Opens the field security profile definition
 
Solution Explorer

 
Project CrmPackage
Role The manifest project that contains all assets to be deployed to Microsoft Dynamics CRM, combined with their deployment settings
 
Project Plugins
Role The Plugins project contains a base class named “Plugin” and classes generated through the create plugin option of the CRM Explorer will derive from this class.
 
Project SilverlightApplication
Role This project is used for develop new Silverlight web resource.
 
Project SilverlightApplication.Web
Role A companion web project that host the above Silverlight project, and can be used to test the Silverlight project.
 
Project Workflow
Role The Workflow project provides support for implementing custom workflow activities to be used for your on-premises Microsoft Dynamics CRM instance.