Tuesday, August 9, 2011

AJAX Textbox control + 'ontextchanged' event with SharePoint Enterprise Search

usercontrol.ascx



<asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged" AutoPostBack="True" />
        </ContentTemplate>
    </asp:UpdatePanel>

usercontrol.ascx.cs



protected void TextBox1_TextChanged(object sender, EventArgs e)
{
        if (queryText != "")
            {
                SearchQueryAndSiteSettingsServiceProxy settingsProxy = SPFarm.Local.ServiceProxies.GetValue<SearchQueryAndSiteSettingsServiceProxy>();
                SearchServiceApplicationProxy proxy = settingsProxy.ApplicationProxies.GetValue<SearchServiceApplicationProxy>("Search Service Application");

                FullTextSqlQuery query = new FullTextSqlQuery(proxy);

                query.ResultsProvider = Microsoft.Office.Server.Search.Query.SearchProvider.Default;
             
               //query.QueryText = queryText;
                query.QueryText = String.Format(@"SELECT Title FROM SCOPE() WHERE Title Like '"+queryText+"%' AND (""Scope""='SiteTitle')");

                query.ResultTypes |= ResultType.RelevantResults;
                ResultTableCollection searchResults = query.Execute();

                if (searchResults.Exists(ResultType.RelevantResults))
                {
                    ResultTable searchResult = searchResults[ResultType.RelevantResults];
                    DataTable result = new DataTable();
                    result.TableName = "Result";
                    result.Load(searchResult, LoadOption.OverwriteChanges);
                 
                    FillResultsGrid(result);
                }
            }
            return null;
}

2 comments: