Un esempio di come creare un WCF per SharePoint 2010 richiamabile lato client tramite JQuery secondo il formato JSON.

Il WCF va messo nella cartella ...14\ISAPI\Sgart\PublicService.svc e sarà richiamabile tramite la url

Text

http://sharepoint2010/.../_vti_bin/Sgart/PublicService.svc

Il WCF è composto dal contratto (l'interfaccia IPublicService) e dal servizo stesso (PublicService.cs e PublicService.svc)

C#: IPublicService.cs (ServiceContract)

using System;
using System.ServiceModel;
using System.ServiceModel.Web;

namespace SgartWCF
{
  [ServiceContract]
  public interface IPublicService
  {
    [OperationContract]
    [WebInvoke(UriTemplate = "SPTestConParametri", Method = "POST",
      BodyStyle = WebMessageBodyStyle.Wrapped,
      RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    int SPTestConParametri(string codiceLingua, string tipologiaFondo);
  }
}

C#: PublicService.cs

using System;
using System.Runtime.Serialization;
using System.ServiceModel;
using Microsoft.SharePoint.Client.Services;
using System.ServiceModel.Activation;

namespace SgartWCF
{
  [BasicHttpBindingServiceMetadataExchangeEndpointAttribute]
  [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
  public class PublicService : IPublicService
  {
    public string SPTestConParametri(string codiceLingua, string tipologiaFondo)
    {
      return string.Format("Risultato: {0} - {1}", codiceLingua, tipologiaFondo);
    }
}

HTML: PublicService.svc

<%@ServiceHost Language="C#" Debug="true"
    Service="SgartWCF.PublicService, $SharePoint.Project.AssemblyFullName$"
    Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
Attenzione, per poter chiamare il WCF tramite POST ed avere una risposta in formato JSON è importante avere la classe MultipleBaseAddressWebServiceHostFactory e non quella messa di default che è MultipleBaseAddressBasicHttpBindingServiceHostFactory.
Altra cosa importante, sostituite ???variable:SharePoint??? con il full name dell'assembly (l'assembly andrà messo nella GAC).

Per provare il web service si può mettere il seguente file sotto la cartella ...14\TEMPLATE\LAYOUTS e richiamarlo con http://sharepoint2010/_layouts/TestWCF.htm

JavaScript: TestWCF.htm

<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
</head>
<body>
  <input type="button" value="test" onclick="callWCFService()" />
  Reponse: <span id="resp" />

<script type="text/javascript">
  function callWCFService() {
    $.ajax({
      type: "POST",
      url: "/_vti_bin/Sgart/PublicService.svc/SPTestConParametri",
      data: '{"codiceLingua":"IT", "tipologiaFondo": "tip"}',
      processData: false,
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function (data) {
        $("#resp").html("ok: " + data.SPTestConParametriResult);
      },
      error: function (jqXHR, textStatus, errorThrown) {
        $("#resp").html("error: " + textStatus + " - " + errorThrown);
      }
    });
  }
</script>

</body>
<html>
di default il parametro di ritorno è il nome del metodo (SPTestConParametri) seguito dalla parola Result (SPTestConParametriResult).
Tags:
AJAX14 C#235 JavaScript184 JQuery33 JSON31 SharePoint498 SharePoint 2010224 WCF18
Potrebbe interessarti anche: