Create a Web Part That Uses the Windows Azure WCF
Service
-
Return to your Visual Studio project. Right-click the
solution, select Add, and then select New Project.
-
Select Empty SharePoint Project as the project
type.
-
Provide a name for the project (such as MortgageWebPart) and click OK. In the
SharePoint Customization Wizard, select Deploy As A Farm
Solution and click Finish.
-
Right-click the MortgageWebPart project and select Add |
New Item.
-
Select Web Part in the SharePoint 2010 Installed Templates
category, and then provide a name for the Web Part (for example,
MortgageInformation).
-
Right-click the project and select Add Service Reference.
Copy and paste the Windows Azure service URI (for example,
http://contosomortgage.cloudapp.net/AzureService.svc)
into the Address field and click Go. Provide a name for the
service reference (such as AzureServiceProxy).
-
Open the main Web Part class file (for example,
MortgageInformation.cs) and amend the code shown here:
using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using MortgageWebPart.AzureServiceProxy;
namespace MortgageWebPart.MortgageInformation
{
[ToolboxItemAttribute(false)]
public class MortgageInformation : WebPart
{
Label titleLabel = new Label();
DataGrid azureMortgageData = new DataGrid();
LinkButton lnkbtnRefresh = new LinkButton();
DataTable mortgageTable = new DataTable("Mortgage");
DataColumn mortgageCol;
DataRow mortgageRow;
double[] returnMortgageData = new double[6];
double mortgateAPR = .18;
double[] pastMortgageRates = new double[6];
string strTrend = "";
protected override void CreateChildControls()
{
titleLabel.Text = "Contoso Mortgage Rates";
lnkbtnRefresh.Text = "Refresh Data";
this.Controls.Add(new LiteralControl("<table><tr><td>"));
this.Controls.Add(titleLabel);
this.Controls.Add(new LiteralControl("</td></tr><tr><td>"));
this.Controls.Add(azureMortgageData);
this.Controls.Add(new LiteralControl("</td></tr><tr><td>"));
this.Controls.Add(lnkbtnRefresh);
this.Controls.Add(new LiteralControl("</td></tr><table>"));
lnkbtnRefresh.Click += new EventHandler(lnkbtnRefresh_Click);
}
void lnkbtnRefresh_Click(object sender, EventArgs e)
{
loadDailyMortgageRates();
}
private void loadDailyMortgageRates()
{
createColumnHeadingsForTable();
AzureServiceClient myProxy = new AzureServiceClient();
returnMortgageData = myProxy.getDailyInterestRate();
double dblDailyInterestRate = 0.13;
for (int i = 0; i < 6; i++)
{
returnMortgageData[i] += dblDailyInterestRate;
}
pastMortgageRates[0] = .36;
pastMortgageRates[1] = .34;
pastMortgageRates[2] = .36;
pastMortgageRates[3] = .32;
pastMortgageRates[4] = .33;
pastMortgageRates[5] = .28;
mortgageRow = mortgageTable.NewRow();
mortgageRow["ID"] = 1;
mortgageRow["Products"] = "30 YR Fixed";
mortgageRow["Rate"] = returnMortgateData[0].ToString();
mortgageRow["APR"] = calculateAPR(returnMortgateData[0]);
mortgageRow["Change"] = (returnMortgateData[0] -
pastMortgageRates[0]).ToString();
mortgageRow["Trend"] = calculateTrend(returnMortgateData[0],
pastMortgageRates[0]);
mortgageTable.Rows.Add(mortgageRow);
mortgageRow = mortgageTable.NewRow();
mortgageRow["ID"] = 2;
mortgageRow["Products"] = "15 YR Fixed";
mortgageRow["Rate"] = returnMortgateData[1].ToString();
mortgageRow["APR"] = calculateAPR(returnMortgateData[1]);
mortgageRow["Change"] = (returnMortgateData[1] -
pastMortgageRates[1]).ToString();
mortgageRow["Trend"] = calculateTrend(returnMortgateData[1],
pastMortgageRates[1]);
mortgageTable.Rows.Add(mortgageRow);
mortgageRow = mortgageTable.NewRow();
mortgageRow["ID"] = 3;
mortgageRow["Products"] = "3/1 ARM";
mortgageRow["Rate"] = returnMortgateData[2].ToString();
mortgageRow["APR"] = calculateAPR(returnMortgateData[2]);
mortgageRow["Change"] = (returnMortgateData[2] -
pastMortgageRates[2]).ToString();
mortgageRow["Trend"] = calculateTrend(returnMortgateData[2],
pastMortgageRates[2]);
mortgageTable.Rows.Add(mortgageRow);
mortgageRow = mortgageTable.NewRow();
mortgageRow["ID"] = 4;
mortgageRow["Products"] = "5/1 ARM";
mortgageRow["Rate"] = returnMortgateData[3].ToString();
mortgageRow["APR"] = calculateAPR(returnMortgateData[3]);
mortgageRow["Change"] = (returnMortgateData[3] -
pastMortgageRates[3]).ToString();
mortgageRow["Trend"] = calculateTrend(returnMortgateData[3],
pastMortgageRates[3]);
mortgageTable.Rows.Add(mortgageRow);
mortgageRow = mortgageTable.NewRow();
mortgageRow["ID"] = 5;
mortgageRow["Products"] = "7/1 ARM";
mortgageRow["Rate"] = returnMortgateData[4].ToString();
mortgageRow["APR"] = calculateAPR(returnMortgateData[4]);
mortgageRow["Change"] = (returnMortgateData[4] -
pastMortgageRates[4]).ToString();
mortgageRow["Trend"] = calculateTrend(returnMortgateData[4],
pastMortgageRates[4]);
mortgageTable.Rows.Add(mortgageRow);
mortgageRow = mortgageTable.NewRow();
mortgageRow["ID"] = 6;
mortgageRow["Products"] = "10/1 ARM";
mortgageRow["Rate"] = returnMortgateData[5].ToString();
mortgageRow["APR"] = calculateAPR(returnMortgateData[5]);
mortgageRow["Change"] = (returnMortgateData[5] -
pastMortgageRates[5]).ToString();
mortgageRow["Trend"] = calculateTrend(returnMortgateData[5],
pastMortgageRates[5]);
mortgageTable.Rows.Add(mortgageRow);
azureMortgageData.DataSource = mortgageTable;
azureMortgageData.DataBind();
}
private string calculateAPR(double mortgageRate)
{
double APR = mortgageRate + mortgateAPR;
return APR.ToString();
}
private string calculateTrend(double currentMortgageRate,
double pastMortgageRate)
{
double mortgageTrend = 0.00;
mortgageTrend = currentMortgageRate - pastMortgageRate;
if (mortgageTrend > 0.00)
{
strTrend = "+";
}
else if (mortgageTrend < 0.00)
{
strTrend = "-";
}
else if (mortgageTrend == 0.00)
{
strTrend = "NC";
}
return strTrend;
}
private void createColumnHeadingsForTable()
{
mortgageCol = new DataColumn();
mortgageCol.DataType = System.Type.GetType("System.Int32");
mortgageCol.ColumnName = "ID";
mortgageCol.Caption = "ID";
mortgageCol.ReadOnly = true;
mortgageCol.Unique = true;
mortgageTable.Columns.Add(mortgageCol);
mortgageCol = new DataColumn();
mortgageCol.DataType = System.Type.GetType("System.String");
mortgageCol.ColumnName = "Products";
mortgageCol.Caption = "Products";
mortgageCol.ReadOnly = true;
mortgageCol.Unique = false;
mortgageTable.Columns.Add(mortgageCol);
mortgageCol = new DataColumn();
mortgageCol.DataType = System.Type.GetType("System.String");
mortgageCol.ColumnName = "Rate";
mortgageCol.Caption = "Rate";
mortgageCol.ReadOnly = true;
mortgageCol.Unique = false;
mortgageTable.Columns.Add(mortgageCol);
mortgageCol = new DataColumn();
mortgageCol.DataType = System.Type.GetType("System.String");
mortgageCol.ColumnName = "APR";
mortgageCol.Caption = "APR";
mortgageCol.ReadOnly = true;
mortgageCol.Unique = false;
mortgageTable.Columns.Add(mortgageCol);
mortgageCol = new DataColumn();
mortgageCol.DataType = System.Type.GetType("System.String");
mortgageCol.ColumnName = "Change";
mortgageCol.Caption = "Change";
mortgageCol.ReadOnly = true;
mortgageCol.Unique = false;
mortgageTable.Columns.Add(mortgageCol);
mortgageCol = new DataColumn();
mortgageCol.DataType = System.Type.GetType("System.String");
mortgageCol.ColumnName = "Trend";
mortgageCol.Caption = "Trend";
mortgageCol.ReadOnly = true;
mortgageCol.Unique = false;
mortgageTable.Columns.Add(mortgageCol);
}
}
}
There’s quite a bit of code in this Web Part, and it
accomplishes several things. First, you create user controls for the
Web Part, a data construct, and an array that will house the return
data from the WCF service call to Windows Azure. You can see these
represented as class-level variables and objects. In the
CreateChildControls method, you set the
properties for the UI Controls and then add
them to the Controls collection so that they will be displayed when
the Web Part is added to SharePoint. You’ll also notice that the
button click event triggers the
lnkbtnRefresh_Click event, where in turn you’re
setting up the data construct, calculating the correct interest
rates, and then creating rows and adding them to the
DataTable object. You finally data-bind the
DataTable after you populate it with
data.
To set up the DataTable object, you call
the createColumnHeadingsForTable method, which
is a helper function that creates all of the necessary columns for
the table—the DataTable object will eventually
be data-bound to the DataGrid object
(azureMortgageData). The code that creates a
column for the DataTable is shown
here:
mortgageCol = new DataColumn();
mortgageCol.DataType = System.Type.GetType("System.Int32");
mortgageCol.ColumnName = "ID";
mortgageCol.Caption = "ID";
mortgageCol.ReadOnly = true;
mortgageCol.Unique = true;
mortgageTable.Columns.Add(mortgageCol);
The WCF service that you deployed to Windows Azure is
called by using the service proxy you created
(myProxy). You can see the instantiation of the
proxy in the following code, along with the calling of the
service:
...
AzureServiceClient myProxy = new AzureServiceClient();
returnMortgateData = myProxy.getDailyInterestRate();
...
A large portion of the code in this application is taking a
baseline interest rate (dblDailyInterestRate)
and then assigning or setting values that will then be added to the
mortgageTable object. The following code shows
the creation of the row and how calculated data (which uses the
return data from the Windows Azure service call) and other helper
methods finalize the product rates and then add a new row to the
DataTable object:
...
mortgageRow = mortgageTable.NewRow();
mortgageRow["ID"] = 1;
mortgageRow["Products"] = "30 YR Fixed";
mortgageRow["Rate"] = returnMortgateData[0].ToString();
mortgageRow["APR"] = calculateAPR(returnMortgateData[0]);
mortgageRow["Change"] = (returnMortgateData[0]
- pastMortgageRates[0]).ToString();
mortgageRow["Trend"] = calculateTrend(returnMortgateData[0],
pastMortgageRates[0]);
mortgageTable.Rows.Add(mortgageRow);
...
The result of this code is the creation and display of
mortgage offerings along with a calculated rate, APR, change to the
current rate from the historical rate, and then an indicator to show
the trend. The key, however, is that although there are several
calculations happening within the Web Part, the core rate numbers are pulled from the WCF
service that is deployed to Windows Azure.
Now that you’ve completed the code for the Web Part, before
you deploy the Web Part you need to ensure that you have configured
the SharePoint server web.config file; otherwise your WCF service call will not be successful.
Configure the SharePoint Server web.config File
-
To amend the web.config file, open the app.config file in
your Web Part project and copy the binding
and endpoint elements (bolded in the
following listing) to the web.config file:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IAzureService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288"
maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192"
maxArrayLength="16384" maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None"
proxyCredentialType="None" realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://yourservername.cloudapp.net/AzureService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IAzureService"
contract="AzureServiceProxy.IAzureService"
name="BasicHttpBinding_IAzureService" />
</client>
</system.serviceModel>
-
Build the project by pressing F6 to make sure that there
are no build errors.
-
Right-click the project and select Deploy to deploy the
Web Part to your SharePoint site.
-
When the Web Part has been deployed, navigate to a webpage
in your SharePoint site. Click Site Actions | Edit Page, and
then click Add A Web Part. Select the Insert tab and navigate to
your newly added Web Part. Select it, and click Add.
What should result is something similar to the following
graphic.
This exercise walked you through the process of creating a Web Part that consumes the WCF service you deployed to Windows Azure. It also
used the data returned from this service to create rates for a set
of mortgage offerings.
The next exercise exposes the same WCF service to a Silverlight application that you will host in the
native Silverlight Web Part.