Ayman’s BizTalk Lab

November 12, 2007

BizTalk 2006 CV Management Application by Mail

Filed under: Uncategorized — Admin @ 8:54 am
Tags: , , , ,

The Idea:

The whole idea is to build an application that receives an employee CV accompanied with the pieces of information (First Name – Last Name – National ID) then validate the National ID against a web service (the web service internal implementation is not important) the decide wither to accept the CV or to reject it. In case of acceptance an email is sent a mail message containing an InfoPath form and the original CV to the Administrator to inform him, and in case of rejection the CV will be copied to the Rejected CVs folder.

Skills Involved:

  • BizTalk 2006 Pop3 adapter.
  • BizTalk 2006 SMTP Adapter
  • Multi-part messages
  • Calling web service from BizTalk 2006.
  • InfoPath 2003 (2007 is the same).
  • BizTalk 2006 Maps.
  • BizTalk 2006 Orchestrations.
  • ASP.NET 2 (an interface to send the mail to the mailbox that BizTalk 2006 listens to).

The Implementation:

First steps in our application are to receive the Mail containing the CV and the accompanied data.

We will send the CV and an XML file that holds the sender three pieces of data; we will user the following file schema:


Only CVID is integer while all other pieces of data are Strings.

Note: in the current implementation of this application you will notice that there’s no need to the CVID field, but in the first I thought I could use it to correlate on it, but discarded thus idea later.

Note: Set the Default value property of the status field to any string like “Not Defined” for example. Because we will send it the web service which will fill it with the acceptance/rejection status. And if you didn’t set its default value you will have a nice error that there is no Status field in the schema!

Multi-part Messages:

You will notice here that a single message sent to BizTalk 2006 will have multiple parts (multiple schemas). This is where we can make use of multi-part messages.

Let’s create our multi-part message:

1-Create a new orchestration and from orchestration view window. In the types section right click on (Multi-Part Message Types) then select (New Multi-Part Message Type)

2- By default it creates a type with a default part. Name the type “CVDataMultipartType” and name the default part created “EmpBody“.

3- Select the ” EmpBody” Message part and from the properties window set its type to “System.Xml.XmlDocument”.

4-Create another two parts in you type and name them “EmpCV” and “EmpData” respectively. And set the type of both of them to “System.Xml.XmlDocument”

5-Set the “Message Body Part” property of the part “EmpData” to true.

Note: I read in many places that it’s very important to set the types of each part to XmlDocument.

Our Multi-Part Message

EmpData” Message Part Properties

Now let’s create some Messages to use in our orchestration.

We need a message of the Multi-Part type we’ve just created.

We need a message for each part that our multi-part message contains.

The message that represent the incoming CV

Next we create three more messages:

Message

Type

Purpose

MsgCV

XmlDocument

Holds the CV attached to the mail message

MsgEmpData

CVSchema (the schema we made earlier)

Holds the XML file attached to the mail message

MsgBody

XmlDocument

Holds the body of the mail message (if any)

Now Lets play some music J

We will build the next orchestration.

Our Orchestration

 

Shape

Description

Receive_1

A receive shape that accepts a message (MsgIncomingCV) and is connected to a receive port that receive a message of type (CVDataMultipartType).

Shape

Description

C_EmpData

A message construction shape that constructs a (MsgEmpData) message

M_EmpData

A message assignment shape that has the following code

 

MsgEmpData=MsgIncomingCV.EmpData;

Shape

Description

C_CV

A message construction shape that constructs a (MsgCV) message

M_CV

A message assignment shape that has the following code

 

MsgCV=MsgIncomingCV.EmpCV;

Shape

Description

C_Body

A message construction shape that constructs a (MsgBody) message

M_Body

A message assignment shape that has the following code

 

MsgBody=MsgIncomingCV.EmpBody;

Very simple, isn’t it?

Calling a web service:

We will create a simple Web Service that will check if the National ID is exactly 4 characters long. The web service will accept an object of type PersonalData and returns an object of the same type.

I created a simple class to represent this object:

Next I created the web service, it contains a single method:

The idea here is to fill the status field of the object with the Acceptance/Rejection status.

Now we have our web service ready we will add a web reference to it in our BizTalk project then we will create a map J

BizTalk 2006 Maps:

We will create a map to transform our message from the received schema (CVSchema) to the schema required by the web service.

A question arises here. We didn’t create any schema for the web service request nor the response?!!

Do you remember that we added a reference to our web service, by doing this visual studio created a schema file for us containing the Request Schema and the Response Schema

Also there is a port type created for us that we will use later.

The destination schema will be the one created for us, as in the picture:

And the map will look like this:

Back to our web service let’s call it from the orchestration

We will complete out work on the very left side of the parallel action shape, because this is where we extracted the data we will validate.

Calling a web service is easy, it involves the next steps:

1- Create the message that will formulate the web service request.
In our case we will create a message “MsgWSrequest” of type that is created for us.

Here we user a transform shape to prepare the request message and we used the map we created earlier.

2- Create a send shape to send the request to the web service.
In our case this shape sends a message of the type created for us.

3- Create a receive shape to receive the response of the webservice
in our case the shape receives a “MsgWSresponse” of the type created for us.

4- Construct the message you will work on later (this is optional and depends on the application logic)
In our case we created a Message assignment shape that constructs a message “MsgValidationResult “of type “CVEmpData” the schema we created at the beginning. And the message assignment shape has the following code:

MsgValidationResult=MsgEmpData;

MsgValidationResult.Status=MsgWSresponse.ValidateNationalIDResult.Status;

Note: for this code to work properly we need to promote the Status field in both “CVEmpData” Schema and the schema that has been created for us when added the web service.

To Promote a field right click on the schema field the chose Promote -> Show Promotions

Then select the Distinguished Fields tab, and select the field(s) you want to promote from the list at the left then click Add

Repeat the steps for the web service generated schema, which you will find under Web References

I guess that this is a very suitable time to have a break with a cup of tea J

What’s Next?

We need to check the value of the status field to decide wither to accept or reject the CV. And here come the role of a Decide Shape.

The condition we will decide upon it will be like this:

MsgValidationResult.Status==”Accepted”

Then we will have two branches, we will begin with the rejection scenario:

Simply we use a send shape followed with a terminate shape. The send shape will send a message “MsgValidationResult” of type “CVEmpData” that send to a send port that has the same type.

Now, let’s have a look at the acceptance scenario:

The acceptance scenario it little more complicated or I wanted to make it like this to demonstrate some ideas.

I will create another Multi-Part message type here with the name “AdminMultipartType” with three Message parts too like the first type we created and with the same names also and the types have the type XmlDocument. But it differs in one thing only. We will set the “Message Body Part” Property of the EmpBody” to true.

Why we did that, I’ll explain later.

Next we will create a message “MsgToAdmin” of the Multi-Part Type we’ve just created.

Let’s discuss the shapes in the figure above:

Shape

Description

ConstructMessage_3

A message construct shape that constructs a “MsgToAdmin” Message

MessageAssignment_2

A message assignment shape that has the following code

 

MsgToAdmin.EmpBody=MsgBody;

MsgToAdmin.EmpCV=MsgCV;

MsgToAdmin.EmpData= MsgValidationResult;

MsgToAdmin.EmpCV(MIME.FileName)=MsgIncomingCV.EmpCV(MIME.FileName);

MsgToAdmin.EmpData(MIME.FileName)=”Related Data.xml”;

MsgToAdmin(SMTP.Subject)=”New CV”;

MsgToAdmin(SMTP.From)=”Admin@MyServer”;

MsgToAdmin(SMTP.SMTPHost)=”localhost”;

MsgToAdmin(SMTP.MessagePartsAttachments)=2;

Let me describe the meaning of each line of these:

MsgToAdmin.EmpBody=MsgBody;

MsgToAdmin.EmpCV=MsgCV;

MsgToAdmin.EmpData= MsgValidationResult;

These three lines set the two parts (EmpBody , EmpCV) of the message we will send to the admin with the parts we extracted before. And setting the third part EmpData to the message we constructed after calling the web service.

MsgToAdmin.EmpCV(MIME.FileName)=MsgIncomingCV.EmpCV(MIME.FileName);

Sets the name of the outgoing CV to the name of the incoming CV. If we didn’t do this step the outgoing CV will have a random name like (att00001).

MsgToAdmin(SMTP.MessagePartsAttachments)=2;

Setting the (SMTP.MessagePartsAttachments(to 2 means that we will send all the Message Parts as attachments except the message body part which will be the body of the new outgoing message.

MsgToAdmin.EmpData(MIME.FileName)=”Related Data.xml”;

Sets the name of the outgoing Employee Data xml file to “Related Data.xml”. If we didn’t do this step the outgoing file will have a random name like (att00001).

MsgToAdmin(SMTP.Subject)=”New CV”;

MsgToAdmin(SMTP.From)=”Admin@MyServer”;

MsgToAdmin(SMTP.SMTPHost)=”localhost”;

Sets the subject the From fields of the outgoing message.

Sets the SMTP server to the localhost. Cause we will use the local smtp server to send mails.

So what all this stuff is about?!!

Here, I’m creating a dynamic send port – a send port that is configured in code – that will be bind to a SMTP adapter after deploying the application.

Let’s go back to our orchestration:

Shape

Description

Expression_1

This shape has the following code

 

sndAcceptance_Port(Microsoft.XLANGs.BaseTypes.Address)=”mailto:ayman@MyServer”;

Here I’m setting the address to which we will send our acceptance message.

Finally we have a send shape and a send port which we will set its binding property to Dynamic

Now I think we completed a very large part of our application. Id we deploy it now and create the physical ports and sets the orchestration bindings we will have an application that receives a mail with two attachments, validate the data in one attachment against a web service then send a response in case of acceptance to another mail address with two attachments also.

Sample Application Configuration:

Configuring the receive location:

Here the most important property is “Body Part Index” and “Body Part Content type” I set the content type to “text/xml” and the index to 1. Here I have only one part with the content type “text/xml” to the index 1 will return my body part successfully.

Because you made the SMTP send port dynamic it will be created for you and will have a name like this

BTSCvWorkflow_1.0.0.0_BTSCvWorkflow.CVProcessingFromMail_sndAcceptance_Port_ba1c9e2f7f6b7a3c

To Be Continued …

see you in the next post.
Ayman

Advertisements

2 Comments »

  1. Hello Ayman,
    It is a nice idea and bst think that you introduced your solution and implemented it.
    I will read you approach and give you my feedback if you don’t mind.

    Very good start man.

    Comment by Tareq Ali — April 22, 2008 @ 6:45 pm | Reply

  2. Hello Tareq,

    I’s an honer to see you here in my humble blog, I’ve visited your blog a couple of times and read some posts and learned things from there.

    I’ll be more than happy to have any feedback, my first – and only – goal is to learn and if I can share my findings with others.

    Regards,

    Comment by Ayman Abdel-Salam — April 23, 2008 @ 11:04 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: