C# Example: Perform an Ad-hoc Search with Sorting
The following example shows how to perform an ad-hoc search for
Incidents sorted in ascending order.
Important: Run Swagger Code Generation before attempting to
modify the code so that you do not receive reference errors. Refer to
Using Swagger Code Generation for the
procedure.
using System;
using System.Collections.Generic;
using System.Linq;
using IO.Swagger.Api;
using IO.Swagger.Model;
namespace Trebuchet.WebApi.IntegrationTests.ExamplesForCustomers.Searches
{
public class AdHocSearchAscending
{
public void SearchResultsSortingAscendingByIncidentStatus()
{
//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/");
searchesApi.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/");
businessObjectApi.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 schemasresponse for Incident to get the field ID for Incident ID
var schemaResponse =
businessObjectApi.BusinessObjectGetBusinessObjectSchemaV1(
businessObjectSummaryByName[0].BusObId);
//Put together the request
var searchResultsRequest = new SearchResultsRequest
{
BusObId = businessObjectSummaryByName[0].BusObId,
PageSize = 200,
Sorting = new List<SortInfo>
{
new SortInfo
{
FieldId =
schemaResponse.FieldDefinitions.First(n => n.Name == "Status").FieldId,
SortDirection = 1
}
}
};
//Run the Search
searchesApi.SearchesGetSearchResultsAdHocV1(searchResultsRequest);
}
}
}
