Click here to Skip to main content
Click here to Skip to main content

Dynamics CRM 4.0 Web Service SDK Example - Part III

, 12 Nov 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Create command Shell Custom Workflow Activity For Dynamics CRM 4.0

Introduction

In this third and last part of the Microsoft Dynamics CRM 4.0 SDK code examples, we shall develop a Windows Command Shell utility implemented as a Custom workflow activity that can be used from Dynamics CRM 4.0 Workflow engine to call / execute any Windows command or run any Windows application. It will take a command string as an input parameter then return with the standard output results as an output parameter. Further possible enhancement to this code base can add batch file exit codes to an output parameter that can branch actions / activities in a given CRM Workflow.

Background

This utility will help explain an easy to implement Microsoft Dynamics CRM 4.0 custom Workflow activity using the CRM SDK version 4.0 in C# using Visual Studio 2008. This example can be used to run the Part II Console Application example from a command line to copy / clone a user Calendar to another user who happens to have the same role and belong to the same business unit. (In fact, this example is a generic implementation that can run any Windows command or application).

Using the Code

The process starts by creating a project skeleton that can be used to implement the custom workflow activity, implement the methods required to create a command shell that takes a command as a string parameter and returns the DOS session standard output display results as an output parameter, then using CRM SDK 4.0 to deploy the Assembly to our Dynamics CRM 4.0 server which in my case was a Microsoft CRM Virtual PC version 2009 running as a guest OS inside my VISTA business edition Host. (Note: I have used Microsoft Dynamics CRM 4.0 SDK version 4.0.9 of June 2009 to develop the solution and used the solution pluginregisterationtool under the path C:\YourCrmSdkDirectory\tools\pluginregistration to deploy the custom workflow activity named CommandshellActivity. The solution required building and correcting the Web references of the CRM Web Service and Discovery service using the exact same names specified in the source code of the solution.)

The attached VS2008 project can be started by creating a new C# project of type Workflow Activity Library. You can download the attached VS 2008 solution and review all the project properties, references and source code. Knowledge on Microsoft Workflow Foundation as well as using Dynamics CRM 4.0 Workflow engine, creating Custom Workflow activities, setting up Microsoft Dynamics CRM 4.0 Virtual PC 2009 and using CRM 4.0 SDK plugin registration tool is out of the scope of this article. However, I shall hopefully be able to answer your questions and help with specific technical issues through your messages sent to this article forum at the bottom of the page on any of the topics above.

The attached project as mentioned earlier is a generic implementation so having a built version of the C# solution and deploying it to CRM server shall enable a new custom workflow activity in the workflow properties form as shown in the figure below:

AddStep.jpg

A typical use of this custom workflow activity utility is to add it as a step to execute a command or run a windows application, then on a following sequential step use the output parameter data in any business specific scenario, for example, a Send Email step that displays the output in the body of the Email and sends it to CRM operation or support personnel or on another scenario use the output to populate an entity field data to update or create entity records. Basically, it is a generic utility that is limited by your imagination.

I shall list here the complete source code structure for a quick snapshot overview of this custom workflow activity source code:

using statements ...
namespace Microsoft.Crm.RightSolutions

[CrmWorkflowActivity("CommandShellActivity", "RightSolutions Activities")]
public partial class CommandShellActivity : SequenceActivity
... Member  Variables and Properties ...

WindowsImpersonationContext impersonationContext;
...

{/* Class Constructor */
public CommandShellActivity()
{
    InitializeComponent();
}

protected override ActivityExecutionStatus Execute
	(ActivityExecutionContext executionContext)
{... /* Custom Workflow Activity code to run by the CRM Workflow Engine runtime */}


/* Helper Methods to Implement User Impersonation technique from within ASP.NET */
private bool impersonateValidUser
	(String userName, String domain, String password) { ... }

private void undoImpersonation() { ... }

Points of Interest

  1. The core code base that implements the Command Shell Custom Workflow Activity revolves around wrapping a CMD.EXE process in a code block that implements an ASP.NET Impersonation to a given user account, in our case the highest privileged account Administrator account to execute the command shell.
  2. The Input and output parameters of the custom work flow activity are implemented using the code below and shall appear in the Properties form of the Custom Workflow Activity step as a property named CommandShell of type nvarchar and the output parameter as a Dynamics value selected from the Entity dropdown list ComamndShell item named ShellOutput.
    public static DependencyProperty ShellCommandProperty = 
    	DependencyProperty.Register("ShellCommand", typeof(string), 
    	typeof(CommandShellActivity));
    [CrmInput("ShellCommand")]
    [CrmReferenceTarget("contact")]
    public String ShellCommand
    {
        get
        {
            return (String)base.GetValue(ShellCommandProperty);
        }
        set
        {
            base.SetValue(ShellCommandProperty, value);
        }
    }
    
    public static DependencyProperty ShellOutputProperty = 
    	DependencyProperty.Register("ShellOutput", typeof(string), 
    	typeof(CommandShellActivity));
    [CrmOutput("ShellOutput")]
    [CrmReferenceTarget("contact")]
    public String ShellOutput
    {
        get
        {
            return (String)base.GetValue(ShellOutputProperty);
        }
        set
        {
            base.SetValue(ShellOutputProperty, value);
        }
    }

    AddStep2.jpg

    AddStep3.jpg

  3. There is a huge security issue related to opening the use of this custom workflow activity to everyone since they are able to run any Windows Command or application of any sort, form or type. So, it is a MUST do for any administrator of the Dynamics CRM platform to give permissions to this Workflow step activity to operation trusted users or make sure any workflow created and using this activity is read-only and execute permissions are given to the right people in the organization.
  4. For simplicity reasons, I have hard-coded the credentials of the impersonated user in the source code but of course it can be read from a secured XML file or parameterized to the custom workflow activity.

History

This is the last article of the series on using Microsoft Dynamics CRM 4.0 SDK. You can find Part I and Part II of the series on CodeProject.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

AymanAminIbrahim
Technical Lead Ayman Soft
Egypt Egypt
With more than 16 years of IT experience, traveled to 6 different countries and worked in many multi-culture environments. Since returning from Sydney To stay in Cairo i have been working for a Microsoft partner in the middle east, focusing on Microsoft Dynamics CRM 4.0 platform, a very interesting business application platform that has state of the art application design and architecture with rich extension tools for developers.
Hope to help some one out there with an idea or a code example .. Smile | :)

Comments and Discussions

 
GeneralCannot run any executable PinmemberMember 787133025-Apr-11 8:36 
GeneralRe: Cannot run any executable PinmemberMember 793866519-May-11 11:07 
GeneralRe: Cannot run any executable PinmemberAymanAminIbrahim5-Jul-11 23:36 
QuestionHow to Deploy and publish customization with source code PinmemberDrChal2-Mar-11 23:04 
GeneralCannot execute cmd commands Pinmemberdevilsbrew694-Sep-10 10:48 
GeneralIssue with wf status not completing PinmemberTCP1027-Apr-10 3:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 12 Nov 2009
Article Copyright 2009 by AymanAminIbrahim
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid