Kit Kai's Tech Blog

Blog about SharePoint

This Blog

Syndication

Search

Tags

News



  • Want to be notified when new posts are available? Click the icon below for more information on how to subscribe.

    Blog Flux MapStats: Stats and Counter for Kit Kai's Tech Blog Subscribe to Kit Kai's Tech Blog

    Locations of visitors to this page Add to Technorati Favorites

Community

Email Notifications

Archives

Sharepoint / CMS Blogs

MVPs', Leads' & RDs' Blogs

Singapore Usergroups

Overseas Usergroups

About Me

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;
 }

}

 

Published Monday, July 09, 2007 9:13 PM by kitkai

Comments

# Four part consuming web services in InfoPath article@ Tuesday, July 10, 2007 12:40 AM

I've quickly put together a 4 part series to illustrate how to consume web services in your browser-enabled

# Need to update the InfoPath 2003 Promoted Columns from SharePoint Designer | keyongtech@ Thursday, January 22, 2009 10:17 AM

Pingback from  Need to update the InfoPath 2003 Promoted Columns from SharePoint Designer | keyongtech