IT tutorials
 
Applications Server
 

SharePoint Applications Using Windows Azure : Create a Web Part That Uses the Windows Azure WCF Service

11/30/2012 6:07:17 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

Create a Web Part That Uses the Windows Azure WCF Service

  1. Return to your Visual Studio project. Right-click the solution, select Add, and then select New Project.

  2. Select Empty SharePoint Project as the project type.

  3. 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.

  4. Right-click the MortgageWebPart project and select Add | New Item.

  5. Select Web Part in the SharePoint 2010 Installed Templates category, and then provide a name for the Web Part (for example, MortgageInformation).

    image with no caption
  6. 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).

  7. 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

  1. 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>
  2. Build the project by pressing F6 to make sure that there are no build errors.

  3. Right-click the project and select Deploy to deploy the Web Part to your SharePoint site.

  4. 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.

image with no caption

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.

 
Others
 
- SharePoint Applications Using Windows Azure : Create a WCF Service and Deploy It to Windows Azure
- Deploying Exchange Server 2010 : Integrating Exchange Server Roles with Active Directory
- Deploying Exchange Server 2010 : Exchange Server Messaging Roles
- SharePoint 2010: Performing Backups and Restores (part 3) - Example of Performing a SharePoint 2010 Farm Backup and Restore
- SharePoint 2010: Performing Backups and Restores (part 2) - Using STSADM
- SharePoint 2010: Performing Backups and Restores (part 1) - Using Windows PowerShell
- Microsoft Lync Server 2010 : Director Installation (part 2) - Install Server
- Microsoft Lync Server 2010 : Director Installation (part 1) - Prerequisites, Create Director Pool
- Microsoft Lync Server 2010 : Director Overview
- Configuring Windows Server 2008 Active Directory : Creating Objects in Active Directory (part 4) - Finding Objects by Using Dsquery
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us