Wednesday, April 11, 2012

REST Web Service in InfoPath 2010

This blog will be underline the verily basic step needed to consume the REST Web service in Info Path 2010. Here I have created the WCF Service using Factory method.There will not be the endpoint config entries.I just removed all the config entries in this WCF application. On service file(.svc) used the Factory="System.ServiceModel.Activation.WebServiceHostFactory" in the markup view. Download the Microsoft.Http.dll and Microsoft.Http.Extension.dll from the Microsoft site. I have two method to retrieve the single record and one is for multiple records. WCF interface implementation code
sing System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using Microsoft.Http;
using System.ServiceModel.Web;
using System.Text;

namespace RestWCF
{
   
    [ServiceContract]
    public interface RestWCFInfoPathCalculator
    {

       
        [OperationContract(Name = "SomeValue")]
        [WebGet(UriTemplate = "/GetAllDetails")]
        List GetAllDetails();

        [OperationContract(Name = "GetBillValueByInv")]
        [WebGet(UriTemplate = "/GetBillValue")]
        BillDetails GetBillValueByInv();
        
    }
    [DataContract]
    public class BillDetails
    {
        [DataMember]
        public string InvoiceNumber;
        [DataMember]
        public double InvoiceAmount;
        [DataMember]
        public DateTime InvoiceDate;
    }
  
}
Interface code implementation
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace RestWCF
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : RestWCFInfoPathCalculator
    {
       
       public BillDetails GetBillValueByInv()
        {
           return GetAllDetails().FirstOrDefault(c => c.InvoiceNumber == "2");
      
        }
       public List GetAllDetails()
       {
           List list = new List();
           list.Add(new BillDetails { InvoiceNumber = "2", InvoiceDate = Convert.ToDateTime("12/03/2012"), InvoiceAmount = 34343.45 });
           list.Add(new BillDetails { InvoiceNumber = "3", InvoiceDate = Convert.ToDateTime("08/03/2012"), InvoiceAmount = 6457.7 });
           return list;
           }
    }
}

Host this WCF service in IIS.Video to help you on this [http://www.youtube.com/watch?v=iptFJaeJ-F0] Now open your InfoPath Designer 2010 - > select the "Blank" form template On Ribbon select the "Data" tab and click on "Data Connection" follow the screenshot.
Infopath Output.