Writing a simple custom workflow activity in CRM 4.0


Let’s create our first Custom Workflow Activity for CRM 4.0.

Check this post as well

https://nishantrana.wordpress.com/2008/10/30/custom-workflow-activities-in-crm-40/

We’ll take here a very simple scenario just to understand how to write custom activity.

This Custom Activity will take as input First Name and Last Name from an entity ( say Lead) and will update it’s description attribute with (First Name + Last Name).

Create a new Workflow Activity Library within workflow project type in Visual Studio.

(Keep the version as 3.0)

Name it MyFirstCustomWFlowActivity

Add references to Microsoft.crm.sdk.dll and Microsoft.crm.sdktypeproxy.dll.

Add the following line of code in your Activity1.cs.

using Microsoft.Crm.Workflow;

using Microsoft.Crm.Sdk;

We are creating a simple activity, so here we can have our class inherit from Activity class instead of SequenceActivity.

Delete the following class Activity1.Designer.cs as our simple custom activity doesn’t require that.


Decorate the class with CrmWorkflowActivity attribute

The first parameter is the user friendly name of the custom activity that will appear in the Workflow Editor and second parameter is the name of group, it can be used for grouping similar custom workflow activity.

namespace MyFirstCustomWFlowActivity

{

[CrmWorkflowActivity(“Get First and Last Name”, “Custom Steps”)]

public class Activity1: Activity

{

}

}

Now create three dependency properties, two as input for first name and last name and one as output where we will have full name i.e. first name and last name combined.

To create dependency property

Right click inside the visual studio editor then select insert snippet and followed by Workflow then DependencyProperty – Property.

We have used CrmInput and CrmOutput Attribute to make the dependency property visible within the workflow editor and to provide a user friendly name to them.

public static DependencyProperty FirstNameProperty = System.Workflow.ComponentModel.DependencyProperty.Register(“FirstName”, typeof(string), typeof(Activity1));

[CrmInput(“First Name”)]

public string FirstName

{

get

{

return ((string)(base.GetValue(Activity1.FirstNameProperty)));

}

set

{

base.SetValue(Activity1.FirstNameProperty, value);

}

}

public static DependencyProperty LastNameProperty = System.Workflow.ComponentModel.DependencyProperty.Register(“LastName”, typeof(string), typeof(Activity1));

[CrmInput(“Last Name”)]

public string LastName

{

get

{

return ((string)(base.GetValue(Activity1.LastNameProperty)));

}

set

{

base.SetValue(Activity1.LastNameProperty, value);

}

}

public static DependencyProperty FullNameProperty = System.Workflow.ComponentModel.DependencyProperty.Register(“FullName”, typeof(string), typeof(Activity1));

[CrmOutput(“Full Name”)]

public string FullName

{

get

{

return ((string)(base.GetValue(Activity1.FullNameProperty)));

}

set

{

base.SetValue(Activity1.FullNameProperty, value);

} }

Now comes the main part wherein we will override the Execute method and place our custom code to combine the first name and second name.

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

{

this.FullName = this.FirstName + this.LastName;

return ActivityExecutionStatus.Closed;

}

Now Sign the assembly

Right Click the project in Solution Explorer

Select Properties

Select Signing

Check Sign The Assembly Check box and Select an existing or new file.

Now build the assesbly.

Now we’ll register the assembly

Open the plugin registration tool

Select Register-Register New Assembly and Select the custom workflow assembly there (MyFirstCustomWFlowActivity.dll)

Click on Register Selected Plugins button to register it in the database.

Now open CRM and go to settings then workflow and create a new workflow there.

In Workflow editor you can see our custom steps group at Add Step.

Now you can proceed as you would with other workflow and add your business logic around the custom workflow activity.

Within Add Step you would be able to set value for first name and last name property within the form assistant panel.

And than we can add an Update step wherein we can get the fullname output property within local values section inside form assistant panel.

Bye..

Send Email in CRM using a Template (SendEmailFromTemplateRequest)


Hi using the following code we can send an email programmatically that uses the email-template withing CRM

Suppose you have created a web application and have a button named btnSendEmail over there.

Put the following code in it’s event handler

protected void btnSendEmail_Click(object sender, EventArgs e)

{

CrmAuthenticationToken token = new CrmAuthenticationToken();

token.AuthenticationType = 0; // AD ( On-Premise)

token.OrganizationName = “nameofyourorganization”;

CrmService myService = new CrmService();

myService.Credentials = System.Net.CredentialCache.DefaultCredentials;

myService.CrmAuthenticationTokenValue = token;

// Create an instance of email

email myEmail = new email();

//set the owner of the mail

myEmail.ownerid = new Owner();

myEmail.ownerid.type = EntityName.systemuser.ToString();

myEmail.ownerid.Value = new Guid(“26063267-7B85-DD11-B2E0-00164145E126”);

// specify the from part of the email

activityparty from = new activityparty();

from.partyid = new Lookup();

from.partyid.type = EntityName.systemuser.ToString();

// guid of the system user who is sending the mail

from.partyid.Value = new Guid(“24543267-7B85-DD11-B2E0-00164145E126”);

myEmail.from = new activityparty[] { from };

// specify the to part of the email

activityparty to = new activityparty();

to.partyid = new Lookup();

to.partyid.type = EntityName.systemuser.ToString();

to.partyid.Value = new Guid(“26063267-7B85-DD11-B2E0-00164145E126”);

myEmail.to = new activityparty[] { to };

TargetSendFromTemplateEmail myTargetSendFromTemplateEmail = new TargetSendFromTemplateEmail();

myTargetSendFromTemplateEmail.Email = myEmail;

SendEmailFromTemplateRequest mySendEmailFromTmpRequest = new SendEmailFromTemplateRequest();

// Specify entity instance id for RegardingID

mySendEmailFromTmpRequest.RegardingId = new Guid(“132C847E-028A-DD11-8E7E-00164145E126”);

// Specify the type of entity

mySendEmailFromTmpRequest.RegardingType = EntityName.opportunity.ToString();

// Specify the email instance to be sent

mySendEmailFromTmpRequest.Target = myTargetSendFromTemplateEmail;

// Specify the template to be used ( Either a global or created specfially for that entity)

mySendEmailFromTmpRequest.TemplateId = new Guid(“6963788F-048A-DD11-8E7E-00164145E126”);

SendEmailFromTemplateResponse mySendEmailFromTmpResponse =(SendEmailFromTemplateResponse)                                  myService.Execute(mySendEmailFromTmpRequest);

}

Bye….

This message has not yet been submitted for delivery (email activity status pending send)


Hi,

I was writing a custom code to send email using templates in CRM 4.0.

The code was working properly but the mails weren’t getting sent.

Checking at activities in CRM i found out the email activities have got created but were showing the status as “job pending”.

While searching for the same i found out that making modification to this file

C:\Program Files\Microsoft CRM Email\Service\Microsoft.Crm.Tools.EmailAgent.xml

would solve this problem.

Especially these tags !!

<MaxMessageCount>30</MaxMessageCount>

<PendingStatusDelay>30</PendingStatusDelay>
<SendingStatusDelay>60</SendingStatusDelay>

Check this link for detailed information

Email router configuration XML file explained

and this post as well

http://niths.wordpress.com/2008/08/27/this-message-has-not-yet-been-submitted-for-delivery/

http://msdn.microsoft.com/en-us/library/cc906237.aspx

http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?pg=2&p=1&tid=8e427cc6-f88a-4a56-b2c3-321418398718&dg=microsoft.public.crm

Bye

CRM Customization Import failed Error: Invalid name prefix


Hi,

While importing customization from development to staging server we were receiving the error.

It was giving the error on a particular relationship “opportunity_new_dataaudit”.

What we did was to comment the entire EntityRelationShip tag for that particular relationship along with it’s corresponding EntityMap entry in the customization.xml

i.e.

<EntityRelationship Name=”Opportunity_New_DataAudit“>
<EntityRelationshipType>OneToMany</EntityRelationshipType>
<ReferencingEntityName>New_DataAudit</ReferencingEntityName>
<ReferencedEntityName>Opportunity</ReferencedEntityName>
<CascadeAssign>NoCascade</CascadeAssign>
<CascadeDelete>RemoveLink</CascadeDelete>
<CascadeReparent>NoCascade</CascadeReparent>
<CascadeShare>NoCascade</CascadeShare>
<CascadeUnshare>NoCascade</CascadeUnshare>
<ReferencingAttributeName>new_opportunityid</ReferencingAttributeName>
<RelationshipDescription>
<Descriptions>
<Description description=”Unique identifier for Opportunity associated with Data Audit.” languagecode=”1033″ />
</Descriptions>
</RelationshipDescription>
<field name=”new_opportunityid” requiredlevel=”none” imemode=”auto” lookupstyle=”single” lookupbrowse=”0″>
<displaynames>
<displayname description=”Opportunity” languagecode=”1033″ />
</displaynames>
</field>
<EntityRelationshipRoles>
<EntityRelationshipRole>
<NavPaneDisplayOption>UseCollectionName</NavPaneDisplayOption>
<NavPaneArea>Details</NavPaneArea>
<NavPaneOrder>10000</NavPaneOrder>
</EntityRelationshipRole>
</EntityRelationshipRoles>
</EntityRelationship>

AND

<EntityMap>
<EntitySource>opportunity</EntitySource>
<EntityTarget>new_dataaudit</EntityTarget>
<AttributeMaps />
</EntityMap>
<EntityMap>

And than we tried importing it and it worked for us.

And there was another way we were able to resolve this error, quite interestingly when we changed the name of EntityRelationship from

Opportunity_New_DataAudit

to

new_Opportunity_New_DataAudit
<EntityRelationship Name=”new_Opportunity_New_DataAudit“>
<EntityRelationshipType>OneToMany</EntityRelationshipType>
<ReferencingEntityName>New_DataAudit</ReferencingEntityName>

i.e simply appending new_ to relationship name in the customization.xml it imported successfully !

Bye…

Specifying user id and password in application defination file for BDC


Hi,

To specify connection string with user id and password we need to have the following things in the application defination file (.xml)

<?xml version=1.0?>

<LobSystem xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:schemaLocation=http://schemas.microsoft.com/office/2006/03/BusinessDataCatalog BDCMetadata.XSD Type=Database Version=1.0.0.0 Name=NorthwindLOBSystem xmlns=http://schemas.microsoft.com/office/2006/03/BusinessDataCatalog>

<Properties>

<Property Name=WildcardCharacter Type=System.String>%</Property>

</Properties>

<LobSystemInstances>

<LobSystemInstance Name=NorthwindInstance>

<Properties>

<Property Name=DatabaseAccessProvider Type=System.String>SqlServer</Property>

<Property Name=AuthenticationMode Type=System.String>PassThrough</Property>

<Property Name=RdbConnection Data Source Type=System.String>d-2926</Property>

<Property Name=RdbConnection Integrated Security Type=System.String />

<Property Name=RdbConnection Initial Catalog Type=System.String>Northwind</Property>

<Property Name=RdbConnection User ID Type=System.String>sa</Property>

<Property Name=RdbConnection Password Type=System.String>sa</Property>

<Property Name=RdbConnection Pooling Type=System.String>false</Property>

</Properties>

</LobSystemInstance>

</LobSystemInstances>

<Entities>

We need not to specify any value for Integrated Security as we are using sql authentication

And can specify user id and password with RdbConnection User ID and Password Property.

Bye…

Using SQL backup and restore for MOSS


Hi,

  • Back Up and Restore the content database to a new Sql server
  • In Central Administration page
  • Go to Application Management and Click on Create or extend web application.
  • Therein Create a new web application. Once application is created don’t create Site Collection.
  • Now again go back to Application Management.
  • Therein click Content Databases
  • Change the web application to point to web application you just created.
  • Remove the default content database created while creating the web application.
  • Then click on Add a content database
  • Specify the Database Server where the content database was restored and in database name specify the name of the content database.
  • The site should get restored properly.

Check out this good information

http://blah.winsmarts.com/2007-10-Backup_and_Restore_Strategies_in_MOSS_2007.aspx

Bye

Nishant Rana's Weblog

Everything related to Microsoft .NET Technology

Skip to content ↓