Monday, July 15, 2013

How to use InvokeWebService activity

Hi everybody,
I needed to use InvokeWebService activity and as usual there were some obstacles on the way to do so :)
If  you find this error message in the SharePoint log:
System.Net.WebException: The request failed with HTTP status 401: Unauthorized.     at System.Workflow.Activities.InvokeWebServiceActivity.Execute(ActivityExecutionContext executionContext)     at System.Workflow.ComponentModel.ActivityExecutor`1.Execute(T activity, ActivityExecutionContext executionContext)     at System.Workflow.ComponentModel.ActivityExecutor`1.Execute(Activity activity, ActivityExecutionContext executionContext)     at System.Workflow.ComponentModel.ActivityExecutorOperation.Run(IWorkflowCoreRuntime workflowCoreRuntime)     at System.Workflow.Runtime.Scheduler.Run()
You may need to pass correct credential to your web service.This should be short briefing how to use this activity and pass those credentials to it.

1) Add InvokeWebService activity into your project from the Toolbox :)
2) The add URL to your web service (Choose required method)
3) After that you should see all parameters of the method in the Properties window of the activity
4) Set parameters as needed
If the web service doesn't require any type of permission I believe you can finish here. In my case I needed to pass credential to it.
5) Set Invoking property and add the following method there:
using ApprovalWorkflowProject.projects.server;


//Set correct credentials
Private void Invokingwebservice (object sender, InvokeWebServiceEventArgs e)
{
  YourWebServiceWCF svc = (YourWebServiceWCF)((InvokeWebServiceEventArgs)e).WebServiceProxy;
  svc.Credentials = System.Net.CredentialCache.DefaultCredentials;

}

After you add a InvokeWebService activity the new web reference is created (for more information check this site http://msdn.microsoft.com/en-US/library/d9w023sx(v=vs.80).aspx or look through whole tutorial http://msdn.microsoft.com/en-us/library/t745kdsh(v=vs.80).aspx). You have to add this reference to your project(that's why that using statement) then you will find YourWebServiceWCF class inside the new namespace.


Hope that helped...

No comments:

Post a Comment