Share via


Step 2: Add the Code for the Federated Search Virtual Earth Map Connector

Note

This topic describes functionality that is part of the Infrastructure Update for Microsoft Office Servers. To download the update, see Description of the SharePoint Server 2007 infrastructure update: July 15, 2008.

The following code passes a user query to the MapPoint Web service and then publishes the location coordinates in an RSS feed that serves as the connector.

The complete code for this sample is available in Federated Search Connector for Virtual Earth Maps Sample.

Create the RSS Feed

  1. Create a file named Map.aspx (either by renaming Default.aspx or creating a new Web form).

  2. In the Map.aspx file, add or change the Inherits page property so that it will use the new class you will create in the Map.aspx.cs (code-behind) file.

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="Map" %>
    
  3. In the Map.aspx.cs file, add the following namespace directives.

    using System;
    using System.Configuration;
    using System.Data;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Text;
    using System.IO;
    using System.Web.Services.Protocols;
    using net.mappoint.staging;
    
  4. Modify the default class declaration so that it uses the class name that is used for the connector.

    public partial class Map : System.Web.UI.Page
    
  5. Replace the default Page_Load method with the following code.

        protected override void Render(HtmlTextWriter writer)
        {
            //Retrieve query term from query string; construct search url
            string queryTerm = Request.QueryString["q"];
            Response.ContentType = "text/xml";
            //Write the RSS document to the HTMLTextWriter
            writer.Write(GetResultsXML(queryTerm));
        }
    
  6. Add the code for the GetResultsXML method, which passes the query term to the GetMapPointMap method. If the query term is a valid location for which latitude and longitude coordinates are available, it creates an RSS feed that publishes the location's coordinates.

        //Return results in RSS format for federated location
        private string GetResultsXML(string queryTerm)
        {
            //Query MapPoint Web service with the URL's query string
            FindResults myFindResults = GetMapPointMap(queryTerm);
            //Begin writing RSS document
            StringBuilder resultsXML = new StringBuilder();
            resultsXML.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            resultsXML.Append("<rss version=\"2.0\">");
            resultsXML.AppendFormat("<channel><title><![CDATA[HTML to RSS Conversion: {0}]]></title><link/><description/><ttl>60</ttl>", queryTerm);
            //Populate RSS feed with latitude and longitude coordinates, if available, for the first location found.
            if (myFindResults.NumberFound > 0 )
            {
                string title = "Map of " + queryTerm;
                string link = "http://maps.live.com/?q=" + queryTerm;
                double latitude = myFindResults.Results[0].FoundLocation.LatLong.Latitude;
                double longitude = myFindResults.Results[0].FoundLocation.LatLong.Longitude;
                //string description = "&lt;img src=&quot;" + RenderMap(latitude, longitude) + "&quot;&gt;";
                string description = title;
                resultsXML.AppendFormat("<item><title>{0}</title><latitude>{3}</latitude><longitude>{4}</longitude></item>", title, link, description, latitude, longitude);
            }
    
            //Complete RSS document
    
            resultsXML.Append("</channel></rss>");
            return resultsXML.ToString();
        }
    
  7. Add the code for the GetMapPointMap method. This method assumes that the query string passed by the URL is a valid structured address, consisting of street address, city, and state. If the query string is not a valid address, the method will return an empty result. The method looks in your site's web.config file for valid MapPoint credentials.

        private FindResults GetMapPointMap(string queryTerm)
        {
    
            // Create and populate Address object for the FindAddress() operation.
            Address myAddress = new Address();
            myAddress.FormattedAddress = queryTerm;
    
            // Set the ThresholdScore option value to zero so maximum results are returned.
            FindOptions myFindOptions = new FindOptions();
            myFindOptions.ThresholdScore = 0;
    
            // Set up the specification object.
            FindAddressSpecification findAddressSpec = new FindAddressSpecification();
            findAddressSpec.InputAddress = myAddress;
            findAddressSpec.Options = myFindOptions;
            findAddressSpec.DataSourceName = "MapPoint.NA";
    
            // Create a FindResults object to store the results returned by FindAddress.
            FindServiceSoap myFindService = new FindServiceSoap();
            // Set credentials for MapPoint Web service -- retrieve and decrypt the user/password combination from the web.config file.
            myFindService.Credentials = new System.Net.NetworkCredential(ConfigurationSettings.AppSettings["mpUser"], ConfigurationSettings.AppSettings["mpPwd"]);
            myFindService.PreAuthenticate = true;
            FindResults myFindResults = new FindResults();
    
            // Create a FindResults object to store the results returned by FindAddress.
            try
            {
                myFindResults = myFindService.FindAddress(findAddressSpec);
            }
    
            catch (Exception ex)
            {
                //Handle exception
            }
    
            return myFindResults;
    
        }
    
  8. Add the necessary MapPoint user credentials to your web.config file. The MapPoint Web service looks for required entries in the system.net and applicationSettings nodes of your web.config file. These entries are added for you when you add the MapPoint Web service to your site. The code in this sample also looks up the MapPoint user credentials that are stored in this file. Add the following entries in the configuration node of your site's web.config file, and supply the appropriate values for the mpUser and mpPwd keys.

      <appSettings>
        <add key="mpUser" value="user"/>
        <add key="mpPwd" value="password"/>
      </appSettings>
    
  9. Deploy this solution to your Office SharePoint Server 2007 site. Save the contents of this solution in your _layouts directory. For more information, see How to: Create a Web Application in a SharePoint Web Site.

  10. Load the Map.aspx file in your Web browser or RSS reader to verify that it is creating an RSS feed. Add test addresses (?q=address) to the URL, and view the source to verify that the feed is returning results.

Next Steps

Step 3: Create the Virtual Earth Map Location and Customize the XSL

See Also

Concepts

Step 1: Set Up the Project for the Federated Search Virtual Earth Map Connector