Tuesday, March 23, 2010

Consuming web service in workflow

Hi All,

Workflow gives us very decent and useful activity for fetching web service from your local computer, network or even from internet.

Let’s us directly dive in to the way how we can achieve calling web service from the workflow. We do not have to write any custom activity or write some code to call web service method. All is given by the built in activity called Invoke web service.

To demonstrate the example here, I have created one small ASP.Net web service project which calculates the square root for the given number. Now I know. This is not something very useful in real world. As I said earlier as well, that I believe in giving simple example and then leave user to play with complex example. Most welcome for any query.

The moment you drop the Invoke Web Service activity on designer surface, it opens up the window asking you for the web service URL, it is very similar to what you see when you add web reference in normal ASP.NeT project.

I have web service in my own machine so I chose find in machine option, if you have to use any of the service residing anywhere in the world, give that as a URL and fetch the web service.

Workflow does all the work for you for creating proxy class ready for you to use and call the methods.

(You can take console workflow as test project here)

Interesting part of this activity is, it knows about the parameter binding. Now there can be one or more parameters for the specific method. Now I have default Helloworld method and my own method called GetSquareRoot which takes parameter of type double.

Moment you are done with setting the web reference with your web service, you can see the red circle error showing you that the MethodName is not set. Hence we are going to go ahead and set the MethodName to my own method.

Now as you can see we have been asked for setting the number value which is the parameter for the GetQuareRoot method. Also notice return value at the top in property window. There has to be a return value after calculating the square root. So go ahead and click on each one of them and bind it to new properties.

Just make sure that you create properties, not fields.

Now go ahead and add two more code activities. One before invokewebservice activity and one after that as shown in figure below.

Double click on Code Activity1 and write following code. Here we are setting the value of our input parameter that we have given as input to the method.

private void codeActivity1_ExecuteCode_1(object sender, EventArgs e)
this.InputNumber = 49;

And then double click on Code Activity2 and write following code. Here we are fetching the value from the web service and storing the result of the return value from web service method and then writing a simple statement so that we can come to know the output.

private void codeActivity2_ExecuteCode(object sender, EventArgs e)
Console.WriteLine("Square root of " + this.InputNumber + " is : " + this.ReturnNumber);


See the result. In this way we have now consumed web service from the workflow with the help of very helpful InvokeWebservice activity.

1 comment:

Anonymous said...

Simple and nice post!
Thank You.

Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com