Thursday, June 25, 2009

Creation of Shortcut to record in Microsoft Dynamics CRM 4.0 in Workflows

There are a lot of such tasks to create shortcut of record which had initialized a workflow execution to be used in email notifications.

Idea of realization of such functionality is not mine. Author of idea - Artem 'Enot' Grunin (Артем Enot Грунин) and I'm very appreciated to him for his bright ideas, help and criticism.


Code of the workflow:

using System;
using System.Collections.Generic;
using System.Text;
using System.Workflow.Activities;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Workflow;
using Microsoft.Crm.Workflow.Activities;
using System.Workflow.ComponentModel;
using Microsoft.Crm.Sdk.Query;
using Microsoft.Win32;

namespace ShortcutCreation
{
[CrmWorkflowActivity("Chortcut Creation")]
public class TaskShortcutCreationWF : SequenceActivity
{
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
IWorkflowContext workflowContext = contextService.Context;
Guid _entityid = workflowContext.PrimaryEntityId;
string _entitytype = workflowContext.PrimaryEntityName;

ICrmService crmservice = workflowContext.CreateCrmService();

//I need organization name for Url Creation
organization org = (organization)crmservice.Retrieve(EntityName.organization.ToString(), workflowContext.OrganizationId, new ColumnSet(new string[] { "name" }));

string orgname = org.name;

url = string.Format("{0}{1}/CRMReports/viewer/drillopen.aspx?ID={2}&LogicalName={3}",
new object[] {
//I retrieve url of crm server with MSCRMServices part and clear it
((string)(Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\MSCRM").GetValue("ServerUrl"))).Replace("MSCRMServices", ""),
orgname,
_entityid,
_entitytype});

return ActivityExecutionStatus.Closed;
}

public static DependencyProperty urlProperty = DependencyProperty.Register("url", typeof(string), typeof(TaskShortcutCreationWF));

[CrmOutput("url")]
public string url
{
get
{
return (string)base.GetValue(urlProperty);
}
set
{
base.SetValue(urlProperty, value);
}
}
}
}


To use this custom workflow you have to build this code and register it in CRM using pluginregistrator.

And the demonstration - for example email with shortcut to case will be sent when case is created:

Worflow which will trigger on creation of case:


Email that will be sent:





Url in the body of email:


And the video demonstration: