Blog Hero Image

Coveo Search API from within Sitecore – Part 2/2

Jun 25, 2012
Juan Arias
Juan Arias

Now that we have learned how to query Coveo Enterprise Search by using Coveo DLLs from a Sitecore CMS page in part 1, I will show how to do the same query via web services; this code might even be useful for a desktop or mobile component of your Sitecore solution. Open your Visual Studio project and add a Service Reference to http://mycoveoserver:8080/CoveoSearch.asmx

We will use the same web form from the previous post:

With the same code:

<asp:TextBox ID=“txtTitle” runat=“server”></asp:TextBox>
<asp:Button ID=“SearchButton” runat=“server” Text=“Search” onclick=“SearchButton_Click” />
<br />
<br />
<asp:GridView ID=“resultsGrid” runat=“server” AutoGenerateColumns=“False”>
  <Columns>
    <asp:BoundField DataField=“SitecoreId” DataFormatString=“{{{0}}}” HeaderText=“Sitecore Id” />
    <asp:BoundField DataField=“Title” HeaderText=“Title” />
    <asp:BoundField DataField=“Author” HeaderText=“Author” />
  </Columns>
  <EmptyDataTemplate>
    The query returned no results.
  </EmptyDataTemplate>
</asp:GridView>
<asp:Label ID=“lblMessage” runat=“server” Text=“” EnableViewState=“false”></asp:Label>

And this code behind:

private void PerformSearch()
{
    SearchServiceSoapClient searchService = new SearchServiceSoapClient();
 
    //Create the query parameters object
    QueryParameters queryParams = new QueryParameters();
 
    //It is necessary to specify all the fields that are needed as results
    ArrayOfString neededFields = new ArrayOfString();
 
    //@SCItemID is the Sitecore Item ID
    neededFields.Add(“@SCItemID”);
 
    //We will need a custom field too
    neededFields.Add(“@MyAuthor”);
 
    //Assign the needed fields array
    queryParams.NeededFields = neededFields;
 
    //We will need 10 results
    queryParams.NumberOfResults = 10;
 
    //Start with the first item, this is used when paging the results
    queryParams.FirstResult = 0;
 
    //Sort the results by a field, ascending
    queryParams.SortCriteria = SortByEnum.FieldAscending;
 
    //Sort by the custom field @MyAuthor
    queryParams.SortByField = “@MyAuthor”;
   
    //Search by the Title (@systitle)
    queryParams.AdvancedQuery = string.Format(“@systitle={0}”, this.txtTitle.Text);
 
    //Perform the search
    QueryResults searchResults = searchService.PerformQuery(queryParams);
 
    //Check if there are results
    if (searchResults != null &&
        searchResults.Results != null &&
        searchResults.Results.Length > 0)
    {
        //The results will be stored in the array ‘searchResults.Results’
        //For this example, we will create an Anonymous Type (just to avoid the creation of an object entity)
        //and assign the list to the GridView
        var resultList = from result in searchResults.Results
                         select new
                            {
                               Title = result.Title,
                               SitecoreId = result.Fields[0].Value.ToString(),
                               Author = result.Fields[1].Value.ToString()
                            };
 
        this.resultsGrid.DataSource = resultList;
        this.resultsGrid.DataBind();
 
        this.lblMessage.Text = string.Format(“Showing {0} of {1} results”, resultList.Count(), searchResults.TotalCount);
    }
    else
    {
        this.resultsGrid.DataSource = null;
        this.resultsGrid.DataBind();
    }
}

And we get the exact same results as we got in the previous post:

Latest insights