C# Example: Perform an Ad-hoc Search with Filter
The following example shows how to perform an ad-hoc search for Incidents using a filter.
Run Swagger Code Generation before attempting to modify the code so that you do not receive reference errors.
Copy
using System.Collections.Generic;
using System.Linq;
using IO.Swagger.Api;
using IO.Swagger.Model;
namespace Trebuchet.WebApi.IntegrationTests.ExamplesForCustomers.Searches
{
public class AdHocSearchWithFilter
{
public void SearchResultsWithFilter()
{
//Get an access token using CSM credentials
var serviceApi = new ServiceApi("http://your server/CherwellApi/");
var tokenResponse = serviceApi.ServiceToken("password","your client id", null, "CSDAdmin", "CSDAdmin", null,"Internal");
//Create a new Business Object api object and add the default header
var businessObjectApi = new BusinessObjectApi("http://your server/CherwellApi/");
businessObjectApi.Configuration.AddDefaultHeader("Authorization","Bearer " + tokenResponse.AccessToken);
//Create a new Searches api object and add the default header
var searchesApi = new SearchesApi("http://your server/CherwellApi/");
searchesApi.Configuration.AddDefaultHeader("Authorization","Bearer " + tokenResponse.AccessToken);
//Get the Business Object summary by name. This returns the Business Object ID
var businessObjectSummaryByName = businessObjectApi.BusinessObjectGetBusinessObjectSummaryByNameV1("Incident");
//Get the Incident Business Object template
var templateRequest = new ApiTrebuchetWebApiDataContractsBusinessObjectTemplateRequest
{
BusObId = businessObjectSummaryByName[0].BusObId
};
var templateRespsonse = businessObjectApi.BusinessObjectGetBusinessObjectTemplateV1(templateRequest);
//Create Search results request
var searchResults = new ApiTrebuchetWebApiDataContractsSearchesSearchResultsRequest();
searchResults.BusObId = businessObjectSummaryByName[0].BusObId;
//Create a filter on the Incident ID field
searchResults.Filters = new List<ApiTrebuchetWebApiDataContractsSearchesFilterInfo>();
var filter = new ApiTrebuchetWebApiDataContractsSearchesFilterInfo();
filter.FieldId = templateRespsonse.Fields.First(f => f.Name == "ShortDescription").FieldId;
filter.Operator = "eq";
filter.Value = "Down System";
searchResults.Filters.Add(filter);
//Query for data
var response = searchesApi.SearchesGetSearchResultsAdHocV1(searchResults);
}
}
}