SPSVB – Developer’s Introduction to SharePoint Search

The morning session on Search seemed to go over well with a lot of great questions.  Here is the slide deck from the session:
Source Code for the Manager Web Part is below:
<code>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 Microsoft.Office.Server;
using Microsoft.Office.Server.Search.Query;
using Microsoft.Office.Server.Search.Administration;
using Microsoft.Office.Server.UserProfiles;
using System.Collections.Specialized;
using Microsoft.SharePoint.Administration;
using System.Collections;
using System.Data;
using System.Text;
<div>namespace NextConnect.Search.Presentation.DisplayManagers
{
    [ToolboxItemAttribute(false)]
    public class DisplayManagers : WebPart
    {
        protected Literal literalMessage;</div>
<div>        protected override void CreateChildControls()
        {
            this.literalMessage = new Literal();
            this.literalMessage.ID = "literalMessage";
            this.Controls.Add(this.literalMessage);
        }</div>
<div>        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            DisplayFullText();
        }</div>
<div>        protected void DisplayFullText()
        {
            StringBuilder messages = new StringBuilder();
            DataTable results = new DataTable();</div>
<div>            string searchProxyName = "Search Service Application";
            SearchQueryAndSiteSettingsServiceProxy settingsProxy = SPFarm.Local.ServiceProxies.GetValue();
            SearchServiceApplicationProxy searchProxy = settingsProxy.ApplicationProxies.GetValue(searchProxyName);
            FullTextSqlQuery mQuery = new FullTextSqlQuery(searchProxy);</div>
<div>            try
            {
                mQuery.QueryText = "SELECT LastName, FirstName, JobTitle, accountname FROM SCOPE() WHERE ("scope" = 'People') AND CONTAINS(JobTitle, 'Manager') AND NOT CONTAINS(Department, '1099')";</div>
<div>                mQuery.ResultTypes = ResultType.RelevantResults;
                mQuery.TrimDuplicates = false;
                mQuery.RowLimit = 100;
                //mQuery.KeywordInclusion = KeywordInclusion.AnyKeyword;
                //mQuery.AuthenticationType = QueryAuthenticationType.NtAuthenticatedQuery;</div>
<div>                ResultTableCollection resultTables = mQuery.Execute();
                if (resultTables.Count &gt; 0)
                {
                    ResultTable relevantResults = resultTables[ResultType.RelevantResults];
                    results.Load(relevantResults, LoadOption.OverwriteChanges);
                }
                messages.AppendFormat(@"
<div>Found {0} result(s).</div>
<div>
<ul>", results.Rows.Count);</ul>
</div>
<div>                foreach (DataRow row in results.Rows)
                {
                    messages.AppendFormat(@"
	<li><a href="/my/person.aspx?Accountname={3}">{0}, {1} - {2}</a></li>
<div>", row[0].ToString(), row[1].ToString(), row[2].ToString(), row[3].ToString());
                }
                messages.AppendFormat(@"</div>
<div>");</div>
<div>                this.EnsureChildControls();
                this.literalMessage.Text = messages.ToString();
            }
            catch (Exception ex)
            {
                this.EnsureChildControls();
                this.literalMessage.Text = "Error: " + ex.Message.ToString();
            }
            finally
            {
                mQuery.Dispose();
            }
        }
    }
}</div>
</div>
</div>
</code>
<div></div>

Tags:

Leave a Comment