Kentico 11 to 12 upgrade CMS.Search.SearchResult

David Pearson asked on March 8, 2022 19:50

The code is in a method in webpart code behind.
The Method returns a dataset from the search.

DataSet results = SearchResult.Search(parameters);

Return results;

The API Change guide states CMS.Search.SearchHelper.Search(CMS.Search.SearchParameters) was change to CMS.Search.SearchResult

How do I get a dataset from CMS.Search.SearchResult? Code example?

Correct Answer

David Pearson answered on March 18, 2022 16:30

If you have older code from Kentico 11 and below that depends on the search results to be dataset you can use the following below to convert CMS.Search.SearchResult to a DataSet. This will convert a List of objects to a dataset for you.

public DataSet ConvertToDataSet<T>(IList<T> list)
{
    DataSet dsFromDtStru = new DataSet();
    DataTable table = new DataTable();
    PropertyInfo[] properties = typeof(T).GetProperties();
    foreach (PropertyInfo prop in properties)
    {
        table.Columns.Add(prop.Name, prop.PropertyType);
    }
    foreach (T item in list)
    {
        DataRow row = table.NewRow();
        foreach (PropertyInfo prop in properties)
        {
            row[prop.Name] = prop.GetValue(item);
        }
        table.Rows.Add(row);
    }
    dsFromDtStru.Tables.Add(table);
    return dsFromDtStru;
}
0 votesVote for this answer Unmark Correct answer

   Please, sign in to be able to submit a new answer.