Related sites Web Part

In addition to making it easy for the users to execute ad hoc site searches, it may also be valuable to dynamically display a listing of related web sites. To provide this feature, one approach would be to create a Web Part that allows the site owner to specify some related keywords, and then perform the Site Directory search and display a list of relevant sites.

Creating the Web Part

The Related sites Web Part will be added to the previously created SPBlueprints.WebParts project created in Chapter 2, Building an Out of Office Delegation Solution.

To add the additional Web Part:

  1. Open the SPBlueprints.WebParts project in Visual Studio 2010.
  2. Browse the installed templates and select Visual C# | SharePoint 2010.
  3. Right-click on the project file and select Add | New Item.
  4. From the template selection screen select the Web Part option.
  5. Provide the name RelatedSites and click on the Add button.
  6. Edit the RelatedSites.webpart file, and add in the custom properties as shown in the following:
    <property name="Title" type="string">Related Sites</property>
    <property name="Description" type="string">SPBlueprints - The Related Sites web part will search for sites with matching keywords.</property>
    <property name="SearchProxyName" type="string">Search Service Application</property>
    <property name="SearchScopeName" type="string">Site Directory</property>
    <property name="DisplayLimit" type="int">5</property>
    <property name="KeywordList" type="string">sites</property>
    
  7. Start by editing the RelatedSites.cs file and add in the following references:
    using System.Collections;
    using System.Data;
    using System.Text;
    using Microsoft.SharePoint.Administration;
    using Microsoft.Office.Server.Search;
    using Microsoft.Office.Server.Search.Query;
    using Microsoft.Office.Server.Search.Administration;
    
  8. Next we will need to define the Web Part's properties starting with the Search Proxy Name property. This property will be used to manage the connection to the Search service application.
    private string _searchProxyName;
    [WebBrowsable(true),
    Category("Configuration"),
    WebDisplayName("Search Proxy Name"),
    WebDescription("Please provide the name of your Search Service Application."),
    Personalizable(PersonalizationScope.Shared)]
    public string SearchProxyName
    {
    get { return _searchProxyName; }
    set { _searchProxyName = value; }
    }
    
  9. Next we will define the Search Scope Name property which can be used to target the desirable content for display.
    private string _searchScopeName;
    [WebBrowsable(true),
    Category("Configuration"),
    WebDisplayName("Search Scope Name"),
    WebDescription("Please provide the name of your Search Scope."),
    Personalizable(PersonalizationScope.Shared)]
    public string SearchScopeName
    {
    get { return _searchScopeName; }
    set { _searchScopeName = value; }
    }
    
  10. Next we will define the Display Limit property used to determine how many records to display.
    private int _displayLimit;
    [WebBrowsable(true),
    Category("Configuration "),
    WebDisplayName("Result limit"),
    WebDescription("The number of items to display."),
    Personalizable(PersonalizationScope.Shared)]
    public int DisplayLimit
    {
    get { return _displayLimit; }
    set { _displayLimit = value; }
    }
    
  11. Next we will define the Keywords property where the site administrator will actually set the keywords.
    private string _keywordList;
    [WebBrowsable(true),
    Category("Configuration"),
    WebDisplayName("Keywords"),
    WebDescription("Comma delimited list of keywords"),
    Personalizable(PersonalizationScope.Shared)]
    public string KeywordList
    {
    get { return _keywordList; }
    set { _keywordList = value; }
    }
    
  12. The output will be built within a Literal control defined within the class, and instantiated within the CreateChildControls() method as shown in the following:
    protected Literal _output;
    protected override void CreateChildControls()
    {
    this._output = new Literal();
    this._output.ID = "output";
    this.Controls.Add(this._output);
    }
    
  13. With all of the setup work complete, we can now define the Display() method that can be called from the OnLoad() method. The method starts by defining StringBuilder that we will use to build the output of the Web Part, and then checks to see if there are any keywords set. Since the keywords are stored within a single string property and are comma delimited, we will do a simple split command to load the values into an array. If there are no keywords, there will be no content to display.
    protected void Display()
    {
    StringBuilder messages = new StringBuilder();
    string[] keywords = this._keywordList.Split(','),
    if (keywords[0] != "")
    {
    
  14. Next we attempt to connect to the Search Proxy specified in the Web Part properties. There is a try/catch block here in order to handle issues related to connecting to the Search service application differently than errors returned as part of a search.
    try
    {
    SearchQueryAndSiteSettingsServiceProxy settingsProxy = SPFarm.Local.ServiceProxies.GetValue<SearchQueryAndSiteSettingsServiceProxy>();
    SearchServiceApplicationProxy searchProxy = settingsProxy.ApplicationProxies.GetValue<SearchServiceApplicationProxy>(this.searchProxyName);
    // Query and Display of Web Part
    Catch
    {
    this.EnsureChildControls();
    this._output.Text = "Error: Please specify a Search Service Application.";
    }
    
  15. Now we can instantiate FullTestSqlQuery and prepare the data objects.
    FullTextSqlQuery mQuery = new FullTextSqlQuery(searchProxy);
    try
    {
    ResultTableCollection resultsTableCollection;
    DataTable results = new DataTable();
    
  16. The formatted query will be broken into two parts, with the first part being the same in all cases and then the addition of the dynamic keywords with a variable number of items. We will then define a simple for loop to append the query to include a dynamic part that covers each keyword. Since we are looking for matches for any of the keywords, the OR operator will be used, which will require that we set the scope predicate starting with the second keyword. The query can also be tailored to exclude other content in your environment as needed.
    mQuery.QueryText = "SELECT Title, Path, SiteName FROM SCOPE() Where ";
    for (int i = 0; i <= keywords.GetUpperBound(0); i++)
    {
    if (i > 0) mQuery.QueryText += " OR ";
    mQuery.QueryText += " (("scope" = '" + _searchScopeName + "') AND Contains('" + keywords[i] + "'))";
    }
    
  17. The remaining FullTextSqlQuery properties can now be set and the query executed. The returned DataTable object can now be checked for results to see if the list needs to be rendered.
    mQuery.ResultTypes = ResultType.RelevantResults;
    mQuery.TrimDuplicates = true;
    mQuery.RowLimit = DisplayLimit;
    resultsTableCollection = mQuery.Execute();
    if (resultsTableCollection.Count > 0)
    {
    ResultTable relevantResults = resultsTableCollection[ResultType.RelevantResults];
    results.Load(relevantResults, LoadOption.OverwriteChanges);
    
  18. The output can be as simple or as complex as needed. For this example, I will create a simple HTML bulleted list with a link to the site. A DIV container and the list will be defined, and then we will iterate through the rows, and write out each link.
    messages.AppendFormat(@"<div id='RelatedSites'><ul>");
    foreach (DataRow row in results.Rows)
    {
    messages.AppendFormat(@"<li><a href='{1}'>{0}</a></li>", row["Title"].ToString(), row["Path"].ToString(), row["SiteName"].ToString());
    }
    messages.AppendFormat(@"</ul></div>");
    }
    
  19. With the display complete we can now render the output, complete the catch block to handle any exceptions, and dispose our Query object.
    this.EnsureChildControls();
    this._output.Text = messages.ToString();
    }
    catch (Exception ex)
    {
    this.EnsureChildControls();
    this._output.Text = "Error: " + ex.Message.ToString();
    }
    finally
    {
    mQuery.Dispose();
    }
    
    

Display Related sites Web Part

Once deployed, the Related sites Web Part can be configured to set the desired keywords in a comma delimited list. The rendered screen is shown as follows:

Display Related sites Web Part
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.5.86