Calling a WCF Service from a SharePoint Workflow (using Visual Studio)
Greetings all, Alex Malek here - I'm a member of the SharePoint workflow team. This week at PDC, I did a breakout session on how to call an external system from a SharePoint Workflow. Let's walk through what that process looks like!
My Setup:
- Office SharePoint Server (Express Setup)
- Visual Studio 2008 Professional Edition
- Pro or better is required for the SP Workflow Tools
Note: I've already gone and created a WCF Service called "EmployeeUpdate” that has a single Operation called "UpdateEmployeeDB"
Step 0: Create a new SharePoint Workflow
- Click “New Project” in the VS File Menu
- Under the "Office/2007" group, choose "SharePoint 2007 Sequential Workflow". I've called my workflow "SharePoint Workflow 1”
Make your way through the wizard, and pick a site to test the workflow against. Then click Finish.
Step 1: Add a Service Reference to your Project
The easiest way to work with a WCF from .net is to have VS create a “proxy class”, which abstracts all the service calls behind a nice interface. You can do this easily using the “Add Service Reference” feature.
- In the "Project" menu, click "Add Service Reference".
- Paste in the address for your service and click "Go".
- Give your reference a friendly name and press OK - I've chosen "CustomEmployeeDatabase”.
- Once the file dialog goes away, you’ll notice a new child in your project pane, called “Service References”. If your service changes at some point, just right click your reference and choose “Update Service Reference”
Step 2: Register this endpoint with SharePoint
Creating the Service Reference endpoint also adds a "App.config" file to your local project, with a "System.ServiceModel" element. However, SharePoint doesn’t read from app.config files, so for SharePoint to be able to call this endpoint, you need to copy that configuration data to your local web.config.
- Open web.config in VS for editing. You can find the file in the "\inetpub\wwwroot\wss\VirtualDirectories\80" directory, where 80 corresponds to the port of your SharePoint application.
- Copy the "System.ServiceModel" element from app.config into web.config. If you already have a "System.ServiceModel” element, you’ll need to merge the <Bindings> and <Client> elements in manually.
- To complete your changes, open a windows command window (Start/Run/cmd.exe) and type "iisreset", which will cycle the sharepoint web application, so it can pick up the web.config changes.
Step 3: Use a Code Activity to call the WCF service endpoint
Since SharePoint is built on .net 3.0, it is not supported to use the new Send/Receive Activities that were added as part of .net 3.5. Instead, we’ll use a code activity to work with the Service via code.
Drag a "Code" activity to the workflow designer canvas.
Next, double-click the activity to generate the code handler.
You can new up the service proxy client by using the proxy class name you gave.
CustomEmployeeDatabase.Workflow1Client client = new CustomEmployeeDatabase.Workflow1Client("WSHttpContextBinding_IWorkflow1");
4. Since my service is local, I’ll use the default network credentials to interact with the service. You’ll need to modify depending how your service is secured.
- client.ClientCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
5. Last, but not least, you can now call your operation, just as if it was a local function.
string result = client.UpdateEmployeeDB();
client.Close(); <- don’t forget to close your connection
That's it! You can now hit F5 to test your workflow.
Wrap-up
There you have it - calling web services from SharePoint workflows. If you run into trouble, double-check that you properly copied the configuration information from app.config into your SharePoint web.config.
Thanks!
-Alex Malek