Logo
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
EPL Standings
 
 
Windows Azure

Integrating DataMarket Data with a Visual Web Part : Create a WCF Service to Retrieve DATA.gov Crime Data (part 2)

7/21/2011 5:03:33 PM
2. Deploy a WCF Service to IIS
  1. Open Windows Explorer, and create a new directory called DallasCrimeDataWCFService. Publish the WCF service project to the new directory by right-clicking the project, and selecting Publish. Then select File System in the Publish Method drop-down list, browse to the new directory location for the Target location, and finally click Publish.

  2. Open IIS 7.0 Manager, and navigate to Sites. Right-click Sites, and select Add Web Site.

  3. In the Add Web Site dialog box, add the Site name, select the physical path to the directory you just created (as shown in the following image), and then select Connect As and configure the access (for testing purposes only) using an account that has full administrator privileges on the server (such as the administrator account). Provide a password, click OK, and then click Test Settings. Be sure that you also select a separate port other than the default port 80 (for example, 2298).



  4. Click OK when done.

  5. Click the Content View tab, and right-click the DallasCrimeData.svc service file to ensure that it is working correctly from your local instance of IIS.


Note:

Ensure that your application pool is set to the Microsoft .NET Framework 4; if it’s set to an earlier version of the .NET Framework, you may get an error when trying to test the service from within IIS. To remedy this, navigate to Application Pools, select the DallasAzureCrimeData application pool, click Basic Settings, and then ensure that the .NET Framework version is similar to Figure 1.

Figure 1. Configuring the application pool in IIS.



After you’ve successfully tested the local deployment of your WCF service, you can use it in other applications. Copy the service URL from the test in your web browser to your Clipboard or a Notepad file.

3. Consume a WCF Service in a Visual Web Part
  1. Open Visual Studio 2010.

  2. From the menu, select File | New | SharePoint | Empty SharePoint Project. Name the project DallasCrimeDataWebPart.

  3. Be sure to select Deploy As Farm Solution when prompted by the Customization Wizard. When you’re done, click Finish.

  4. Right-click Reference, and select Add Service Reference.

  5. Paste the URL you copied from the IIS service test  (for example, http://blueyonderdemo:6622/DallasCrimeData.svc), and click Go.

  6. When the service definition successfully returns, name the service reference DallasCrimeDataService, and then click OK.

  7. After the project has been created, right-click the project in the Solution Explorer, select Add, and then select Class.

  8. Name the new class ReturnCrimeData, and add the bold properties shown in the following code to the newly created class:

    ...

    namespace GetDallasData
    {
    public class CrimeData
    {
    public string City { get; set; }
    public string Year { get; set; }
    public string Population { get; set; }
    public string ViolentCrime { get; set; }
    public string Robbery { get; set; }
    public string PropertyCrime { get; set; }
    public string Burglary { get; set; }
    }
    }

  9. Right-click the project, select Add, and then click New Item.

  10. In the New Item dialog box, select SharePoint and Visual Web Part. After the Visual Web Part is added to your SharePoint project, rename it to CrimeDataVisualWebPart.

  11. Right-click CrimeDataVisualWebPartUserControl.ascx, and select View Designer.

  12. Type in the short title Dallas Crime Data, and then drag a GridView and a Button control onto the designer surface. Name the GridView control datagrdDallasData and the Button control btnGetData. Your UI should look similar to the following image.



  13. Double-click the Button control to add an event handler.

  14. If Visual Studio does not toggle to the code view, right-click CrimeDataVisualWebPart UserControl.ascx and select View Code.

  15. Update the btnGetData_Click event (which was generated automatically when you double-clicked the button) with the following bold code:

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using DallasCrimeDataWebPart.DallasCrimeDataSvc;
    using System.Collections.Generic;
    using System.Collections;
    using System.ServiceModel;

    namespace DallasCrimeDataWebPart.CrimeDataVisualWebPart
    {
    public partial class CrimeDataVisualWebPartUserControl : UserControl
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btnGetData_Click(object sender, EventArgs e)
    {
    BasicHttpBinding mySvcbinding = new BasicHttpBinding();
    UriBuilder serviceURI = new UriBuilder("http://localhost:7833/
    DallasCrimeData.svc");
    DallasCrimeDataClient myWCFProxy = new DallasCrimeDataClient(mySvcbinding,
    new EndpointAddress(serviceURI.Uri));

    var myCrimeData = myWCFProxy.GetCrimeData(null);
    List<ReturnCrimeData> returnCrimeDataFromService = new
    List<ReturnCrimeData>();
    foreach (var item in myCrimeData)
    {
    ReturnCrimeData tempEntity = new ReturnCrimeData();
    tempEntity.City = item.City;
    tempEntity.Year = item.Year;
    tempEntity.Population = item.Population;
    tempEntity.ViolentCrime = item.ViolentCrime;
    tempEntity.Robbery = item.Robbery;
    tempEntity.PropertyCrime = item.PropertyCrime;
    tempEntity.Burglary = item.Burglary;
    returnCrimeDataFromService.Add(tempEntity);
    }

    myWCFProxy.Close();

    datagrdDallasData.DataSource = returnCrimeDataFromService;
    datagrdDallasData.DataBind();
    }
    }
    }


    The preceding code takes the return XML package from the WCF service call (which has already been filtered to return only a specific set of elements—that is, city, year, population, violent crime, robbery, property crime, and burglary). The Visual Web Part code then uses its own in-memory object to populate a list collection, which it binds to the DataGrid control.

  16. You can configure the properties of the Visual Web Part by using the .webpart and elements.xml files. For example, the bold code in Example 1 shows how you can change the Title and Description of the Web Part. In Example 2, the property in bold text shows how you can deploy the Web Part into a custom group within SharePoint called “SP And Azure.”

    Example 1. Changing the Web Part title and description
    <?xml version="1.0" encoding="utf-8"?>
    <webParts>
    <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
    <metaData>
    <type name=
    "DallasCrimeDataWebPart.CrimeDataVisualWebPart.CrimeDataVisualWebPart,
    $SharePoint.Project.AssemblyFullName$" />
    <importErrorMessage>$Resources:core,ImportErrorMessage;
    </importErrorMessage>
    </metaData>
    <data>
    <properties>
    <property name="Title" type="string">Crime Data Visual Web Part</
    property>
    <property name="Description" type="string">My visual web part that
    displays crime data from Dallas Azure.</property>
    </properties>
    </data>
    </webPart>
    </webParts>


    Example 2. Custom group for Web Part
    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/" >
    <Module Name="CrimeDataVisualWebPart" List="113" Url="_catalogs/wp">
    <File Path="CrimeDataVisualWebPart\CrimeDataVisualWebPart.webpart"
    Url="CrimeDataVisualWebPart.webpart" Type="GhostableInLibrary" >
    <Property Name="Group" Value="SP And Azure" />
    </File>
    </Module>
    </Elements>

  17. At this point, you can press F6 to build the Visual Web Part to ensure that it builds successfully. When it builds successfully, you can then press F5 to debug the Visual Web Part to SharePoint.

  18. Assuming no errors, right-click the project, and select Deploy. This will deploy the Visual Web Part to SharePoint.


    Note:

    If you run into any issues with this Web Part, you can right-click the project and select Retract to clean and pull the Web Part from SharePoint.


  19. After you deploy the Web Part, navigate to SharePoint to the Web Part page you created earlier. Click Site Actions and Edit Page.

  20. Remove any previously added Web Parts from the page by selecting Delete from each of the Web Part’s Options menu.

  21. Click Add A Web Part, navigate to the SP And Azure category, and click the Visual Web Part you just deployed. If you used the same properties as in this exercise, the name of the Web Part will be Crime Data Visual Web Part. Click Add.



  22. When the Web Part loads, click the Get Data button. This will trigger the btnGetData_Click event, which calls the WCF service deployed into IIS. The WCF service will return the DATA.gov crime data from the Windows Azure DataMarket. The returned data will then be bound to the DataGrid and displayed in the Web Part.

    Figure 2 shows the final set of filtered Windows Azure DataMarket data that is displayed in the Visual Web Part.

Figure 2. Visual Web Part displaying data from Windows Azure DataMarket.


Although using the Visual Web Part may fit into your plans, you may also want to use Silverlight to present the data. Although Silverlight does provide an enhanced user experience, it comes with some programmatic differences that you need to manage .

Other -----------------
- Integrating DataMarket Data with Excel and SharePoint - Consume Windows Azure Data in Excel 2010
- Using Cloud Services : Collaborating on Word Processing (part 2)
- Using Cloud Services : Collaborating on Word Processing (part 1)
- Using Cloud Services : Collaborating on Project Management
- Windows Azure Marketplace DataMarket (part 2)
- Windows Azure Marketplace DataMarket (part 1) - WCF Data Services and Publicly Consumable Data Feeds
- Accessing the Surveys Application : Content Delivery Network
- Accessing the Surveys Application : Authentication and Authorization
- Working with Data in the Surveys Application : Using SQL Azure
- Using Cloud Services : Collaborating on Contact Management - Exploring Contact Management and CRM Applications
- Using Cloud Services : Collaborating on Event Management - Exploring Event Management Applications
- Working with Data in the Surveys Application : Displaying Data (part 3) - Displaying Questions & Displaying the Summary Statistics
- Working with Data in the Surveys Application : Displaying Data (part 2) - Session Data Storage
- Working with Data in the Surveys Application : Displaying Data (part 1) - Paging through Survey Results
- Microsoft Azure: Enterprise Application Development - Queue Storage
- Microsoft : Azure Enterprise Application Development : Web Role
- Microsoft Azure: Enterprise Application Development - Worker Roles
- Working with Data in the Surveys Application : Saving Survey Response Data
- Working with Data in the Surveys Application : Testing and Windows Azure Storage
- Working with Data in the Surveys Application : A Data Model for a Multi-Tenant Application
 
 
Most view of day
- Creating a Home Network : Creating a Wired LAN, Creating a Wireless LAN
- Microsoft Lync Server 2010 : Planning for Voice Deployment - Voice Resilience
- Windows Vista Improvements for Hardware and Driver Troubleshooting
- Windows Server 2008 R2 high-availability and recovery features : Installing and Administering Network Load Balancing (part 2) - Creating a Network Load Balancing cluster
- Sharing Your Computer with Others : Create a User Account, Switch Between Accounts
- Microsoft Exchange Server 2013 : Creating new mailboxes (part 2) - Languages
- Mix and Match with Old Windows and Macs : Installing Optional Network Components
- Microsoft Project 2010 : Tracking Progress on Tasks (part 3) - Entering a Task’s Completion Percentage
- Maintaining Security : Restricting Access on the Computer
- Microsoft Exchange Server 2010 : Completing Transport Server Setup (part 6) - Verifying Edge Subscriptions, Removing Edge Subscriptions
Top 10
- Windows Server 2012 : DHCP,IPv6 and IPAM - Exploring DHCP (part 3) - Creating IPv4 DHCP Scopes
- Windows Server 2012 : DHCP,IPv6 and IPAM - Exploring DHCP (part 2) - Installing DHCP Server and Server Tools
- Windows Server 2012 : DHCP,IPv6 and IPAM - Exploring DHCP (part 1)
- Windows Server 2012 : DHCP,IPv6 and IPAM - Understanding the Components of an Enterprise Network
- Microsoft OneNote 2010 : Using the Research and Translate Tools (part 3) - Translating Text with the Mini Translator
- Microsoft OneNote 2010 : Using the Research and Translate Tools (part 2) - Translating a Word or Phrase with the Research Pane
- Microsoft OneNote 2010 : Using the Research and Translate Tools (part 1) - Setting Options for the Research Task Pane, Searching with the Research Task Pane
- Microsoft OneNote 2010 : Doing Research with Linked Notes (part 2) - Ending a Linked Notes Session, Viewing Linked Notes
- Microsoft OneNote 2010 : Doing Research with Linked Notes (part 1) - Beginning a Linked Notes Session
- Microsoft OneNote 2010 : Doing Research with Side Notes (part 3) - Moving Side Notes to Your Existing Notes
 
 
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro