/ Sharepoint

Consume WCF service using Sharepoint Designer

By default the SharePoint Designer is unable to consume WCF services because it fails to recognize the structure of the request messages. There is a workaround if you still need to consume WCF services using the Designer. However, this does not work via the datasource wizard but instead requires some manual intervention. There are two methods to do accomplish this:

  • Modify the datasource
  • Modify the code in the page

The first method is easier to implement and gives you more control over the output. The only downside is that the WCF service must use a wrapped request message contract. This means that you either need full control over the WCF service so you can enable the request message to be wrapped, or you have to be lucky that the WCF service you’re trying to consume uses wrapped message contracts. From the WSDL it’s difficult to see if wrapped message contracts are used. If you’re not sure you can try the first method. If the Designer crashes as soon as you want to use the modified data source, the WCF service probably doesn’t support it. If you have full control over your service, the first method will also describe how te enable wrapped message contracts.

Method 1: Modify the datasource

To consume a WCF service in the Designer by modifying the datasource you perform the following steps:

  • Enable wrapped message contracts
  • Create a datasource using the wizard
  • Obtain request message by calling the service with Webservice Studio
  • Modify the soap envelope of the datasource

1.1 Enable wrapped message contracts

In this step you’ll set the IsWrapped property of the request message contract to True.

If you used Web Service Software Factory for creating your WCF service you can set the request message contract to be wrapped in the service contract model. Open the service contract model and select the request message contract. In the properties window of the message contract you change Is Wrapped to True. Save the model and generate the code for the service contract.

If you didn’t use Web Service Software Factory you can have the request message contract set to wrapped by modifying the MessageContract atribute. Open the file containing the request message contract and locate the MessageContract attribute. Make sure that the attribute does not set IsWrapped to False. If it does you can simply remove the property assignment or you set it to True, which is the default value. You can also give the wrapper a name and namespace if you like, but this is not required. In the end your message contract should look a little similar to the code sample below:

[sourcecode language=”csharp”]
///

/// Service Contract Class – HelloWorldRequestMessage ///

[WCF::MessageContract(
WrapperName = “HelloWorldRequestMessage”,
WrapperNamespace = “urn:HelloWorldService.ServiceContracts”)]
public partial class HelloWorldRequestMessage
{
private string name;

[WCF::MessageBodyMember(Name = “Name”)]
public string Name
{
get { return name; }
set { name = value; }
}
}
[/sourcecode]

Whether you changed the wrapped value using WSSF or directly in the code, you should make sure to rebuild your project.

1.2 Create a datasource using the wizard

In this step you’ll create a new datasource using the Sharepoint Designer wizard. Open Sharepoint Designer, connect to your site and go to the Datasource Library tab on the right side of your screen. Click on the Connect to a webservice link underneath the XML Web Services. In de dialog you enter the URL to your services endpoint and click the Connect Now button. The wizard will connect to your web service and present you a list of endpoints and methods. It doesn’t matter which endpoint or method you select here, because we’ll change that later. Click OK to exit the wizard and create the new datasource.

1.3 Obtain request message by calling the service with Webservice Studio

In this step you’ll obtain the appropriate request message for calling your webservice. Because the request message generated by the Sharepoint Designer wizard is incorrect you need to replace that with the correct one. For this purpose we’ll use Webservice Studio (http://www.codeplex.com/WebserviceStudio).

Before calling the web service you should make sure you have the URL to the service’s WSDL and that the service is running.

To call a web service you start Webservice Studio, type the URL to the service’s WSDL in the WSDL Endpoint drop down list and click on the Get button. Webservice Studio attempts to create a proxy for the WSDL it found at the specified URL.

Once the creation of the proxy is finished you are promted with the web service’s methods on the left. By clicking on a method the required input parameters are shown and you are able to specify their values.

Once you’ve specified the required parameters you can call the web service by clicking the Invoke button. The response of the web service is shown in the Output window if the call was successful. We then move to the Request/Response tab to be able to view the raw request message. Copy the soap:Envelope record of the request message and store it somewhere so we can use it in our next step.

1.4 Modify the soap envelope of the datasource

In this step you’ll modify the XML Webservice datasource of the Sharepoint Designer.

To access the datasources you need to be connected to your site using the Sharepoint Designer. In the site’s tree you navigate to _catalogsfpdatasources. This directory contains xml files with the datasource configurations. Open the xml file that has the name of your service. The contents will look similar to this (reformatted for better reading):

[sourcecode language=”xml”]

HelloWorldService ]]> [/sourcecode]

The soap:Envelope part is what we’re interested in. Replace the complete soap:Envelope with the soap envelope you obtained using Webservice Studio. Now your datasource will look similar to this (reformatted for better reading):

[sourcecode language=”xml”]

HelloWorldService Sander ]]> [/sourcecode]

Note that the soap:Body is completely different from what the Designer’s wizard gave you. After modifying the soap:Envelope you can save the changes to the datasource. Refresh the list of datasources in the Datasource Library and use the datasource like you would any other. Be careful not to reconnect the datasource using the wizard because that would render it unusable again.

Method 2: Modify the code in the page

To consume a WCF service in the Designer by modifying the code in the page you perform the following steps:

  1. Obtain request message by calling the service with Webservice Studio
  2. Complete DataFormWebPart XML
  3. Paste XML in WebPart

2.1 Obtain request message by calling the service with Webservice Studio

This step is the same as in the previous method.

2.2 Complete DataFormWebPart XML

After obtaining the request message of the web service we can complete the XML of a DataFormWebPart. Below is the basis of the DataFormWebPart. Have a look at the structure and then continue reading on how to complete it.

[sourcecode language=”xml”]

{MeteringPointName}
{MeterName}






'

[/sourcecode]The SharePoint:SoapDataSource contains a couple of attributes that need to be set. The first is the WsdlPath, which is the same path as you used in Webservice Studio. Then there’s the select path, which in most cases is almost identical to the WsdlPath with ‘?wsdl’ removed from the end. The SelectAction is the soap action of the method you’d like to call. If you’re uncertain what this value should be, you can look it up in Webservice Studio, on the left side of the Request/Response tab. SelectPort is the name of the endpoint of the service. This is specified in the Wsdl as the name of the binding to which the operation belongs. The last attribute is the SelectServiceName attribute. It’s value is the name of the method. If you’re unsure of the values of the latter two attributes, you can use the add XML Webservice wizard to obtain them.

A little below the SharePoint:SoapDataSource you find the SelectCommand. The SelectCommand should contain the soap envelope you’ve obtained using Webservice Studio. You can substiture the values of the parameters by variable names enclosed by braces. This allows the value of the parameter to be set via Web Part connection. In you do this you should also make sure to add the parameter to the SelectParameters and ParameterBindings collections.

The last thing you probably want to do is to modify the XSL for the desired output. This is pretty straightforward if you’re familiar with XSL. If not, there’s a lot of content out on the web to help you get started.

2.3 Paste XML in WebPart

All the hard work is done in the previous steps. The only thing to do now is to copy the completed XML from the previous step and paste it inside a WebPart. The easiest way to do this is by selecting the split view in the SharePoint Designer and then clicking on a WebPart. This selects the code of the WebPart. Find the tags and paste the XML between them. Once you save the document it will update the design and call the WCF service.