Consuming Web Services in InfoPath Part 1: Setting up the Web Services.
Scenario
Say, I have a scenario to write-off heavy equipments. Each equipment will have a certain quantity of metal which can be salvage. So in the disposal form, we are going to calculate the estimated worth we can recover.
To illustrate how to consume a web services, I'm going to start by creating one to consume. I'm not going to give too much details about this step. Basically, I've set up a custom list to store the metal prices. This list will have two columns, Metal and Price.

Next, I code the webservices to ask for 4 parameters, basically the weight of the four different metals, and return the worth of these metals. Below is the code I've created. Basically, the code uses the object model to retrieve the four metals, and compute accordingly.
using System;
using System.Configuration;
using System.Diagnostics;
using System.Security;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using Microsoft;
using Microsoft.SharePoint;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class MetalExchange : System.Web.Services.WebService
{
public MetalExchange()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public double GetPrice(int CopperWeight, int LeadWeight, int AluminiumWeight, int FerrousWeight)
{
try
{
SPWeb oWeb = new SPSite(ConfigurationManager.AppSettings["Url"].ToString()).RootWeb;
SPList oMetalList = oWeb.Lists["Metals"];
double Total = 0;
foreach (SPListItem oItem in oMetalList.Items)
{
if (oItem["Title"].ToString() == "Copper")
{
Total += (((double)oItem["Price"]) * (CopperWeight / 1000));
}
if (oItem["Title"].ToString() == "Lead")
{
Total += (((double)oItem["Price"]) * (LeadWeight / 1000));
}
if (oItem["Title"].ToString() == "Aluminium")
{
Total += (((double)oItem["Price"]) * (AluminiumWeight / 1000));
}
if (oItem["Title"].ToString() == "Ferrous")
{
Total += (((double)oItem["Price"]) * (FerrousWeight / 1000));
}
}
return Total * double.Parse(oItem["Rate"].ToString());
}
catch (System.Exception ex)
{
EventLog.WriteEntry("SharePoint WebServices", "Exception: " + ex.Message + ", Stack: " + ex.StackTrace);
}
return 0;
}
}