Prexens team dev' corner

How-to: Develop your own Charting for SharePoint WebService

Overview
Web services offers you the best level of integration between Charting Web Parts and your databases or back-end systems. A powerful Web service mapping editor allows you to integrate any service that exposes its WSDL schema as a data source for a Chart Web Part. Contextual information extracted from SharePoint can be passed as input parameters to the Web service. Data are then retrieved asynchronously to maximize user experience, consumed by Chart Web Parts and exposed through visually-exciting charts.

Implementing a sample Web service

The simple chart below is generated from an external database, if you want to customize your charts, you can develop your own Web Service as explained in this post.
Charting

Chart Web Parts data series are based on the XML schema below. Compliant Web services have to output their results according to the following specifications.
 

<?xml version="1.0" encoding="utf-8"?>
<
xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<
xs:element name="Segments">
  <
xs:complexType>
    <
xs:sequence>
      <
xs:element maxOccurs="unbounded" name="Segment">
        <
xs:complexType>
          <
xs:attribute name="Title" type="xs:string" use="required" />
          <
xs:attribute name="Value" type="xs:unsignedByte" use="required" />
        </
xs:complexType>
      </
xs:element>
    </
xs:sequence>
  </
xs:complexType>
</
xs:element>
</
xs:schema>

The XML content below exposes a data series containing three segments A, B and C with respective values of 10, 15 and 25.

<?xml version="1.0" encoding="utf-8"?>
<
Segments>
   <
Segment Title="A" Value="10" />
   <
Segment Title="B" Value="15" />
   <
Segment Title="C" Value="25" />
</
Segments>

The following source code is a C# class that returns our sample data series and exposes it through an ASP.net Web service

File SampleSeriesService.cs
using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System.Web.Services;

namespace ChartingForSharePoint
{
   public struct Segment
   {

     [XmlAttribute]
     public string Title;

     [XmlAttribute]
     public int Value;

     public Segment(string title, int value)
     {
       Title = title;
       Value = value;
     }
   }

   [WebService(Namespace = http://api.acme.com/services)]
   [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
   public class SampleSeriesService : WebService
   {

     [WebMethod]
     [return: XmlRoot("Segments")]
     public List<Segment> GetSampleSeries(string inParam)
     {
        List<Segment> segments = new List<Segment>();
        segments.Add(new Segment("A", 10));
        segments.Add(new Segment("B", 15));
        segments.Add(new Segment("C", 25));
        return segments;
     }
   }
}

File SampleSeriesService.asmx

<%@ WebService Language="C#" Class="ChartingForSharePoint.SampleSeriesService,ChartingForSharePoint, Version=1.0.0.0, Culture=neutral,PublicKeyToken=f71dfbc876472393" %>


Registering the service

The C# class has to be compiled into a signed assembly. This assembly has to be registered into the GAC of your server, or copied into the bin directory of your Web application (in this case, make sure CAS policies are correctly configured for your assembly).

The ASMX file has to be copied at root level or into a subdirectory of folder C:\Program Files\Common file\Microsoft Shared\web server extensions\12\ISAPI on your Web front-end server.

The Web service is ready to run, but you must register it via an STSADM command in order to make SharePoint exposing its WSDL schema correctly. If you omit this step, no WSDL schema will be exposed when trying to access the service description via the special WSDL query string token.

Use the registerwebservice STSADM operation with syntax below to register the Web service to SharePoint. This custom STSADM command is automatically deployed when setting-up Charting for SharePoint on your system.

You can use the syntax below from a command prompt to obtain the list of parameters supported by the operation

C:\...\12\BIN>stsadm.exe -help registerwebservice

The table below gives details about each parameter

Parameter
Details Comments
url
Absolute URL of the Web service
If the ASMX file is stored in folder 12\ISAPI, the service URL will be http://<host>/<sitecollection-url>/_vti_bin/<service-file>.asmx
Note that you only need to register the service for one site collection on one Web application to make it globally available to Charting Web Parts
username
Login of the account used to access the service
This parameter is optional. You only need to specify credentials if the Web application uses basic authentication. Login can be composed of a username only, or it can contain a domain and a username separated by a backslash character (e.g. DOMAIN1\userA)
password

Password of the account used to access the service

Warning: the password will be sent in clear when calling the service

Below is a screenshot of how to register our sample service on the localhost Web application. If you receive the message Operation completed successfully, the Web service definition file is available to SharePoint.

Registering Web Service

You can ensure that the WSDL definition file is available by accessing the service URL from a Web browser and then click the Service description link, or by appending ?WSDL at the end of the service URL (see sample below)

http://localhost/_vti_bin/SampleSeriesService.asmx?WSDL

Authentication methods

Charting Web Parts are relying on Web browser credentials cache to access Web services. Thus, we advise you to connect to Web services located on the same Web application as Web Part pages. This way, Charting Web Parts are compliant with native SharePoint authentication methods:

  • Basic authentication
  • Windows integrated authentication
  • Forms authentication
  • Anonymous authentication

Using a Web service as a data source

1. From any Web Part page on your site collection, add a new Chart Web Part.

Using Web PArt

Note: make sure Charting for SharePoint is correctly deployed on your site collection. Refers to § Deploy features on a site collection for more details about setup procedure.

2. From the new Web Part, open the tool pane by clicking on the message displayed into the Web Part.

Open Mapping

3. From the Data mapping section of the tool pane, click on Edit mappings.

Edit Mapping

4. Select Web service as a data service type from the header of the popup window

Edit Web Service Mapping
5. Enter the absolute or relative service description URL into Address of the service description file. Then press Enter or click on the magnifier button

WebService URL
6. Choose a prefix and specify the service relative URL if you want to use relative path, or leave the default URL if you prefer an absolute URL.

Note: We advise you to work with relatives URLs for both service description and service address. You can then streamline support of extended Web applications.

7. Select a Web method from available operations. When a new operation is selected from the list, the corresponding input parameters are populated in the Input parameters mapping grid. The first parameter is ready for binding.

Web Method

8. For each input parameter, click on parameter name, map it to its contextual value by selecting a macro from the list of predefined values, and then click on the Save link. Repeat this operation for all input parameters.

Parameters

9. If no macro is convenient for your input parameter, you can hard code its value or use an expression builder registered for the Web application.

Note: if you want to improve the list of available macros with custom macros, you can register some ExpressionBuilder expressions, and add your custom macros into the following file

C:\...\12\TEMPLATE\XML\Prexens\SharePoint\ApplicationPages\Services\Mapping\ParameterMappingMacros.xml

10. When all settings are OK, click on the Save button. Settings are saved and the corresponding chart is displayed.

Charting

11. You can adjust the look & feel of the chart by editing chart’s settings from the tool pane. Different categories of settings are available:

  • Chart settings section give access to settings available for all chart types
  • Sections below the Chart settings section are more dedicated to one or more particular chart types.


Possible errors and resolutions

The table below is a non-exhaustive list of possible errors that can occurs when using mapping editor.

Error message
Resolution
Unable to cast object of type 'System.Web.Services.Description.HttpAddressBinding' to type 'System.Web.Services.Description.SoapAddressBinding'
 Try to register the Web service on the same Web application as the Web Part page. Refers to § “Registering the service” for more details about how to register a Web service.

There is an error in XML document (3, 2)

 WSDL description address is invalid. Make sure that you’ve appended the WSDL query string token to the URL of service description file. If yes, try to access the URL you’ve specified from a Web browser to ensure that it returns the WSDL schema of the service.
Exception: Protocol error
URL of the service description file is not well formed. Open a Web browser and make sure you can access the service with URL you’ve specified.
404 not found
The URL you’ve specified as the service description file address does not exist. Open a Web browser and make sure you can access the service with URL you’ve specified.

 


Categories
Links
  Prexens Web Site
  Charting for SharePoint
  Charting for SharePoint (Codeplex)
  Charting for SharePoint Starter kit

Microsoft Certified Partner