Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
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
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
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
 
 
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
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server