Showing posts with label JSON. Show all posts
Showing posts with label JSON. Show all posts

Tuesday, April 29, 2014

SharePoint 2013: Create a Custom WCF REST Service Hosted in SharePoint

SharePoint 2013 provides a robust Representational State Transfer (REST) interface that allows any technology that supports standard REST capabilities to interact with SharePoint (sites, libraries, lists, etc).  In addition to the built-in SharePoint REST API, you can create your own custom Windows Communication Foundation (WCF) REST services that are hosted in SharePoint.  In this example, we'll explore the steps necessary to create a SharePoint-hosted WCF service with a REST interface that is deployed in a SharePoint solution (.wsp).

Download the source (50.8 KB)

Step-by-Step Instructions

  1. Run Visual Studio 2013 as an Administrator.
  2. Create a new project named Barkes.Services.Presidents using the SharePoint 2013 - Empty Project template from the Visual C# SharePoint Solutions category.
  3. Ensure the Deploy as farm solution option is selected.
  4. After the project is created, right-click on the project in the Solution Explorer, then Add -> SharePoint Mapped Folder.
  5. On the Add SharePoint Mapped Folder dialog, select the ISAPI folder and click OK.
  6. Right-click on the ISAPI folder in the Solution Explorer, then Add -> New Item.
  7. On the Add New Item dialog, select Text File from the General category and enter  PresidentsService.svc as the name, then click Add.  Make sure to change the default file extension from txt to svc.
     
  8. Right-click on the ISAPI folder in the Solution Explorer, then Add -> New Item.
  9. On the Add New Item dialog, select Code File from the Code category and enter PresidentsService.svc.cs as the name, then click Add.
  10. Right-click on the ISAPI folder in the Solution Explorer, then Add -> New Item.
  11. On the Add New Item dialog, select Interface from the Code category and enter IPresidentsService.cs as the name, then click Add.
  12. Add the required assembly references by right-clicking References, then Add Reference from the Solution Explorer.
  13. On the Reference Manager dialog, select Framework and check System.Runtime.Serialization, System.ServiceModel and System.ServiceModel.Web, then click OK.
  14. By default Visual Studio does not support token replacements in .SVC files.  In order to use the $SharePoint.Project.AssemblyFullName$ token, right-click on the project in Solution Explorer, then Unload Project.  If you are prompted to save the project, select yes.
  15. Right-click the project in the Solution Explorer, then Edit Barkes.Services.Presidents.csproj.
  16. In the first PropertyGroup (toward the top of the project file), add the TokenReplacementFileExtensions element beneath the SandboxedSolution element and set its value to svc.  Don't forget to save the changes to the project file.
    <PropertyGroup>
        ...
        <SandboxedSolution>False</SandboxedSolution>
        <TokenReplacementFileExtensions>svc</TokenReplacementFileExtensions>
    </PropertyGroup>
  17. After you've made the required manual project changes, right-click the project and select Reload Project.
  18. Open PresidentsService.svc and enter the following service declaration.  Note that the use of the SharePoint-specific MultipleBaseAddressWebServiceHostFactory replaces the need to specify endpoint configurations in a web.config.
    <%@ ServiceHost Language="C#" Debug="true"
        Service="Barkes.Services.Presidents.PresidentsService, $SharePoint.Project.AssemblyFullName$"
        CodeBehind="PresidentsService.svc.cs"
        Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory,
        Microsoft.SharePoint.Client.ServerRuntime, Version=15.0.0.0, Culture=neutral,
        PublicKeyToken=71e9bce111e9429c" %>
  19. Open IPresidentsService.cs and enter the following interface definition, with associated ServiceContract and OperationContracts.
    1. using System;
      using System.Collections.Generic;
      using System.ServiceModel;
      using System.ServiceModel.Web;
      using Barkes.Services.Presidents.Model;
      
      namespace Barkes.Services.Presidents
      {
          [ServiceContract]
          interface IPresidentsService
          {
              [OperationContract]
              [WebGet(UriTemplate = "GetAllPresidents",
                  ResponseFormat = WebMessageFormat.Json)]
              List<President> GetAllPresidents();
      
              [OperationContract(Name = "GetPresidentsByLastName")]
              [WebGet(UriTemplate = "GetPresidentsByLastName/{lastName}",
                  ResponseFormat = WebMessageFormat.Json)]
              List<President> GetPresidentsByName(string lastName);
      
              [OperationContract(Name = "GetPresidentsByLastFirstName")]
              [WebGet(UriTemplate = "GetPresidentsByLastFirstName/{lastName}/{firstName}",
                  ResponseFormat = WebMessageFormat.Json)]
              List<President> GetPresidentsByName(string lastName, string firstName);
      
              [OperationContract]
              [WebGet(UriTemplate = "GetPresidentById/{id}",
                  ResponseFormat = WebMessageFormat.Json)]
              President GetPresidentById(string id);
      
              [OperationContract]
              [WebInvoke(Method = "POST", UriTemplate = "AddPresident",
                  RequestFormat = WebMessageFormat.Json,
                  ResponseFormat = WebMessageFormat.Json)]
              bool AddPresident(President president);
          }
      }
      
  20. Open PresidentsService.svc.cs and enter the following code to implement the service interface.
    1. using Microsoft.SharePoint.Client.Services;
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.ServiceModel.Activation;
      using Barkes.Services.Presidents.Model;
      
      namespace Barkes.Services.Presidents
      {
          [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
          public class PresidentsService : IPresidentsService
          {
              #region Private Members
      
              private List<President> _presidents;
              private List<President> Presidents
              {
                  get
                  {
                      // If there aren't any presidents in our list, populate with samples
                      _presidents = _presidents ?? new List<President>(SampleData.SamplePresidents);
                      return _presidents;
                  }
              }
      
              #endregion
      
              #region IPresidentsService Implementation
      
              public List<President> GetAllPresidents()
              {
                  return Presidents;
              }
      
              public List<President> GetPresidentsByName(string lastName)
              {
                  return GetPresidentsByName(lastName, string.Empty);
              }
      
              public List<President> GetPresidentsByName(string lastName, string firstName)
              {
                  var query = from President p in Presidents
                              where p.LastName.ToLower().Contains(lastName.ToLower())
                                 && (string.IsNullOrWhiteSpace(firstName) 
                                      ? true 
                                      : p.FirstName.ToLower().Contains(firstName.ToLower()))
                              select p;
      
                  return query.ToList();
              }
      
              public President GetPresidentById(string id)
              {
                  var query = from President p in Presidents
                              where p.Id == id
                              select p;
      
                  return query.FirstOrDefault();
              }
      
              public bool AddPresident(President president)
              {
                  Presidents.Add(president);
                  return true;
              }
      
              #endregion
      
          }
      }
  21. Add a new folder named Model to the project by right-clicking on the project and selecting Add, then New Folder.
  22. Add a new class in the Model folder named President.cs and enter the following class definition, with associated DataContract and DataMembers.
    1. using System.Runtime.Serialization;
      
      namespace Barkes.Services.Presidents.Model
      {
          [DataContract]
          public class President
          {
              [DataMember]
              public string Id { get; set; }
      
              [DataMember]
              public string LastName { get; set; }
      
              [DataMember]
              public string FirstName { get; set; }
      
              [DataMember]
              public string EmailAddress { get; set; }
          }
      }
  23. Add a new class in the Model folder named PresidentsData.cs and enter the following sample data code.  In a production application, this would typically come from a database.  The presidents array is purposely abbreviated for readability - all the presidents are in the complete source.
    1. namespace Barkes.Services.Presidents.Model
      {
          public static class SampleData
          {
              // This array is purposely abbreviated for readability in this article.
              // The complete list of presidents is available in the source download.
              public static President[] SamplePresidents = new President[]
              {
                  new President { 
                      Id =  "1", FirstName = "George", LastName = "Washington", 
                      EmailAddress = "gwashington@email.com" },
                  new President { 
                      Id =  "2", FirstName = "John", LastName = "Adams", 
                      EmailAddress = "jadams@email.com" },
                  new President { 
                      Id =  "3", FirstName = "Thomas", LastName = "Jefferson", 
                      EmailAddress = "tjefferson@email.com" },
                  new President { 
                      Id =  "4", FirstName = "James", LastName = "Madison", 
                      EmailAddress = "jmadison@email.com" },
                  new President { 
                      Id =  "5", FirstName = "James", LastName = "Monroe", 
                      EmailAddress = "jmonroe@email.com" },
                  new President { 
                      Id = "43", FirstName = "George W.", LastName = "Bush", 
                      EmailAddress = "gbush@email.com" },
                  new President { 
                      Id = "44", FirstName = "Barack", LastName = "Obama", 
                      EmailAddress = "bobama@email.com" },
              };
          }
      }
  24. Now you're ready to build the solution and deploy the WSP.  After deployment, you'll find the PresidentsService.svc service declaration in the 15 hive at C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\BarkesServices.

Call the Service from Managed Code

There are a number of different options (tools, libraries, etc) and articles available to help you consume a WCF REST service from managed code.  The following is an excerpt from a Visual Studio unit test project that calls the service to return all presidents.   The custom JSON helper class used to simplify the object (de)serialization is shown below as well.

  1. // Be sure to update the url to point to the Presidents Service in your SP farm.
    string url = "http://sp13.dev/_vti_bin/BarkesServices/PresidentsService.svc/GetAllPresidents";
    string response = CallService(url);
    List<President> presidents = JsonHelper.Deserialize<List<President>>(response);
    
    private string CallService(string serviceUrl)
    {
        WebClient client = new WebClient();
        client.UseDefaultCredentials = true;
        client.Headers["Content-type"] = "application/json";
        client.Encoding = Encoding.UTF8;
        string response = response = client.DownloadString(serviceUrl);
    
        return response;
    }
  1. public class JsonHelper
    {
        public static string Serialize<T>(T obj)
        {
            MemoryStream stream = new MemoryStream();
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
            serializer.WriteObject(stream, obj);
            stream.Position = 0;
            StreamReader reader = new StreamReader(stream);
            return reader.ReadToEnd();
        }
    
        public static T Deserialize<T>(string data)
        {
            if (string.IsNullOrWhiteSpace(data)) return default(T);
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
            MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(data));
            return (T)serializer.ReadObject(stream);
        }
    }

Call the Service from JQuery

The following demonstrates how to call the service from a Script Editor Web Part using simple HTML, JavaScript and JQuery.

  1. <script src="http://sp13.dev/SiteAssets/jquery-1.10.2.min.js"></script>
    
    <h2>SharePoint 2013: Consume a custom WCF REST service hosted in SharePoint 2013.</h2>
    <h3>This is a quick sample to demonstrate calling a custom SharePoint-hosted WCF REST service from a
        Script Editor Web Part using simple HTML, JavaScript and JQuery.
    </h3>
    
    <div>
        <br />
        <p id="message">Loading presidents...</p>
    </div>
    
    <div id="resultsPanel"></div>
    
    <script type="text/javascript">
        $(document).ready(function () {
            getPresidentsData();
        });
      
    function getPresidentsData() {
        var serviceUri = _spPageContextInfo.webAbsoluteUrl +
            "/_vti_bin/BarkesServices/PresidentsService.svc/GetAllPresidents";
        $.ajax({
            type: "GET",
            contentType: "application/json",
            url: serviceUri,
            dataType: "json",
            success:
                function (response) {
                    showPresidentsList(response);
                    $('#message').html("<a href=" + serviceUri + ">" + serviceUri + "</a>");
                },
            error:
                function (err) {
                    alert(err);
                }
        });
    }
    
    function showPresidentsList(presidentsData) {
        $.each(presidentsData, function () {
            $('#resultsPanel').append($(this)[0].Id + ' - ');
            $('#resultsPanel').append($(this)[0].FirstName + ' ');
            $('#resultsPanel').append($(this)[0].LastName + ' (');
            $('#resultsPanel').append($(this)[0].EmailAddress + ')');
            $('#resultsPanel').append('<br><br>');
        });
    }
    </script>

Results Screenshots

Calling the Presidents Service from a Script Editor Web Part using simple HTML, JavaScript and JQuery.  Of course you can use the resulting JSON data with Backbone.js, Knockout and a variety of JavaScript/JQuery grids (JS Grid, simpleGrid, jqGrid, etc).

Interacting with the Presidents Service in Fiddler:



Download the source (50.8 KB)

For more SharePoint articles and samples, check out the Software Development Outpost.

Wednesday, July 11, 2012

Consuming a WCF REST Service in ASP.NET

REST (Representational State Transfer) services are quickly becoming the de-facto standard for web services in SOA (Solution Oriented Architecture) architectures.  This example demonstrates how to consume a REST service in WCF (Windows Communication Foundation) with Visual Studio 2010 with support for JSON (Javascript Object Notation) and XML formats.
This project, with complete source code is available for download at https://bitbucket.org/barkes/presidentsweb.


The PresidentsService project (required for this demo) is available for download at https://bitbucket.org/barkes/presidentsservice.

To Consume a WCF REST Service in ASP.NET

  1. Create a new project in Visual Studio named PresidentsWeb by selecting the ASP.NET Web Application project template in the Web category.

  2. Add a reference to our presidents WCF service by right-clicking the project in the Solutions Explorer and clicking the Add Service Reference menu.  On the Add Service Reference dialog enter the URI of the WCF service in the Address textbox.  In this example, we're using the custom Presidents WCF REST Service hosted locally at http://localhost:9140/PresidentsService.svc.  Click the Go button.  Enter PresidentsService as the Namespace and click the OK button.


  3. Add a new WFC Service named PresidentsService.svc.  Right-click your project, click Add.  Then click New Item.  Select the WCF Service item from the Web category in the Add New Item dialog.


  4. Open Default.aspx and add the following table - please note this has been shortened for readability purposes, but the source download contains the complete listing.
     Default.aspx
    <table cellpadding="5px">
        <tr><td>
            <asp:Label ID="ServiceUrlLabel" runat="server" Text="Service URL:">
            <⁄asp:Label>
        <⁄td><td class="style1">
            <asp:TextBox ID="ServiceUrlTextBox" runat="server" Width="600px">
            <⁄asp:TextBox>
        <⁄td>tr>
     
        <tr valign="top"><td>
            <asp:Label ID="ResultLabel" runat="server" Text="Results:">
            <⁄asp:Label>
        <⁄td><td class="style1">    
                <asp:TextBox ID="ResultsTextBox" runat="server" Width="600px" 
                    Height="200px" ReadOnly="True" TextMode="MultiLine">
                <⁄asp:TextBox>
        <⁄td><⁄tr>
     
        <tr><td>
             
        <⁄td><td class="style1">    
            <asp:Button ID="GetResultsButton" runat="server" 
                onclick="GetResultsButton_Click" 
                Text="Get Results" />
        <⁄td><⁄tr>
    <⁄table>
    
  5. Open Default.aspx.cs from the Solution Explorer and add the implementation for the GetResultsButton_Click event handler.  In this method, the .NET class WebClient is used to send and receive data from the URI resource and the DataContractJsonSerializer to de-serialize the data.
     Default.aspx.cs
    protected void GetResultsButton_Click(object sender, EventArgs e)
    {
        string url = ServiceUrlTextBox.Text;
             
        WebClient client = new WebClient();
        byte[] data = client.DownloadData(new Uri(url));
        Stream stream = new MemoryStream(data);
        StreamReader reader = new StreamReader(stream);
                
        DataContractSerializer dcs = 
            new DataContractSerializer(typeof(string));
        DataContractJsonSerializer jcs = 
            new DataContractJsonSerializer(typeof(PresidentsService.President));
     
        PresidentsService.President president = 
            (PresidentsService.President) jcs.ReadObject(stream);
     
        string result = 
            string.Format("~~ De-serialized JSON Object (DataContractJsonSerializer)\n{0} {1} ({2})",
            president.FirstName, president.LastName, president.EmailAddress);
     
        stream.Position = 0;
        result = 
            result + "\n\n~~ Raw JSON Data (StreamReader)\n" + reader.ReadToEnd();
     
        ResultsTextBox.Text = result;
    }
    

Test the ASP.NET Application

  1. Since this application utilizes the Presidents WCF REST Service, make sure the service is running before you execute the PresidentsWeb application.  In Visual Studio, select the Debug menu, then Start Debugging.  Alternately, the debugger may be launched using the F5 key.  In the Service URL, enter the REST URI, then click the Get Results button.



Monday, July 9, 2012

Create a WCF REST Service (JSON & XML)

REST (Representational State Transfer) services are quickly becoming the de-facto standard for web services in SOA (Solution Oriented Architecture) architectures.  This example demonstrates how to create a REST service in WCF (Windows Communication Foundation) with Visual Studio 2010 with support for JSON (Javascript Object Notation) and XML formats.  The basic steps in this process consist of creating the interface contract, implementing the interface methods, configuring the interface in Web.config and testing basic methods to prepare for consumption.
This project, with complete source code is available for download at  https://bitbucket.org/barkes/presidentsservice.

To create a WCF REST Service

  1. Create a new project in Visual Studio named PresidentsService by selecting the WCF Service Application project template in the WCF category.

  2. Delete the automatically generated IService1.cs and IService1.svc files.  Right-click the files in the Solution Explorer, select the Delete menu.


  3. Add a new WFC Service named PresidentsService.svc.  Right-click your project, click Add.  Then click New Item.  Select the WCF Service item from the Web category in the Add New Item dialog.


  4. Open IPresidentsService.cs and add the following OperationContracts with JSON and XML formatted responses:


    Web Method URI Format Description
    GET
    presidents/{lastname}
    JSON
    Returns president based on last name.
    GET
    presidents
    JSON
    Returns all presidents.
    POST
    presidents
    JSON
    Add or update a president.
    DELETE
    presidents/{lastname}
    JSON
    Delete a president based on last name.
    GET
    xml/presidents/{lastname}
    XML
    Returns president based on last name.
    GET
    xml/presidents
    XML
    Returns all presidents.
    POST
    xml/presidents
    XML
    Add or update a president.
    DELETE
    xml/presidents/{lastname}
    XML
    Delete a president based on last name.

    Although there are several techniques for supporting both JSON and XML formats, this example simply uses a different URI, with an 'xml' prefix and corresponding response formats of WebMessageFormat.Json and WebMessageFormat.Xml, respectively.
     IPresidentsService.cs
    [OperationContract]
    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = "presidents/{lastname}")]
    President GetPresident(string lastname);
     
    [OperationContract]
    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = "presidents")]
    List<President> GetAllPresidents();
     
    [OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = "presidents")]
    bool AddPresident(President president);
     
    [OperationContract]
    [WebInvoke(Method = "DELETE",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = "presidents/{lastname}")]
    bool DeletePresident(string lastName);
    
    [OperationContract]
    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Xml,
        BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = "xml/presidents/{lastname}")]
    President GetPresidentXml(string lastname);
     
    [OperationContract]
    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Xml,
        BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = "xml/presidents")]
    List<President> GetAllPresidentsXml();
     
    [OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Xml,
        BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = "xml/presidents")]
    bool AddPresidentXml(President president);
     
    [OperationContract]
    [WebInvoke(Method = "DELETE",
        ResponseFormat = WebMessageFormat.Xml,
        BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = "xml/presidents/{lastname}")]
    bool DeletePresidentXml(string lastName);
  5. Open PresidentsService.svc.cs and implement the service methods.  The following is the GetPresident method that returns a president based on last name.  Please note the rest of the code is included in the download.
     PresidentsService.svc.cs
    public President GetPresident(string lastName)
    {
        MIMEType = WebHelper.MimeTypes["htm"];
                
        var query = from President president in Presidents
                    where president.LastName.ToLower() == lastName.ToLower()
                    select president;
     
        return query.FirstOrDefault();
    }
    Notice, this implementation uses a custom helper named WebHelper.MimeType to set the outgoing response's ContentType to text/html so the JSON responses can be viewed directly in a browser.

    internal string MIMEType
    {
        get { return _mimeType; }
        set 
        { 
            _mimeType = value; 
            WebOperationContext.Current.OutgoingResponse.ContentType = _mimeType; 
        }
    }
    
  6. Open Web.config to configure the service Endpoint and Bindings.  The key here is using the REST webHttpBinding binding (not SOAP-based wsHttpBinding) to provide JSON and XML from the service through direct URL access.

    <?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.serviceModel> <services> <service name="PresidentsService.PresidentsService" behaviorConfiguration="PresidentsServiceBehavior"> <endpoint address="" binding="webHttpBinding" contract="PresidentsService.IPresidentsService" behaviorConfiguration="web"> </endpoint> </service> </services> <behaviors> <serviceBehaviors> <behavior name="PresidentsServiceBehavior"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="web"> <webHttp/> </behavior> </endpointBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration>

Test the WCF REST Service

  1. Since REST services are accessed via standard web methods, a web browser is the simplest way to quickly test service wiring and basic operations.  Navigate to the REST service by entering URI for the specified method into the browser's address bar (URL).  Right-click on PresidentsService.svc in the Solution Explorer.  Click View in Browser.

    The following returns a complete list of presidents in JSON:




    The following returns a complete list of presidents in XML:

  2. There is also a useful REST add-on for Firefox named RESTClient that can be extremely valuable developing, testing and debugging REST and XML services.
Now that we have a REST service, the next step is Consuming a WCF REST Service.