Skip to content

Instantly share code, notes, and snippets.

@michaellwest
Last active December 6, 2020 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaellwest/d064ad48faaa0e2d60124ee00365067d to your computer and use it in GitHub Desktop.
Save michaellwest/d064ad48faaa0e2d60124ee00365067d to your computer and use it in GitHub Desktop.
Convert ScopeQuery to SearchStringModels using Sitecore PowerShell Extensions.
#$provider = [Sitecore.DependencyInjection.ServiceLocator]::ServiceProvider.GetService([Sitecore.XA.Foundation.Abstractions.Configuration.IConfiguration[Sitecore.XA.Foundation.Search.SearchConfiguration]]).GetConfiguration().QueryMaxItems
$query = "+template:{51ed5851-1a61-4dae-b803-6c7fae6b43d8};+sxa:SameFirstTagAsCurrentPage|SxaTags;custom:EventStartDate|[NOW TO *];custom:EventEndDate|[NOW TO *];sort:eventstartdate;sort:isfeatured[desc]"
$modelList = [Sitecore.ContentSearch.Utilities.SearchStringModel]::ParseDatasourceString($query)
$contextItem = Get-Item -Path "master:" -ID "{31318AEE-310B-4EE4-84DA-4B84471E0FCA}"
[Sitecore.DependencyInjection.ServiceLocator]::ServiceProvider.GetService([Sitecore.XA.Foundation.Search.Services.ISearchQueryTokenResolver]).Resolve($modelList, $contextItem)
class CustomSearchResultItem : SearchResultItem
{
[Sitecore.ContentSearch.IndexField("eventstartdate_tdt")]
[DateTime]$EventStartDate
[Sitecore.ContentSearch.IndexField("isfeatured_b")]
[bool]$IsFeatured
}
$props = @{
Index = "sitecore_sxa_concentra_master_index"
QueryType = [CustomSearchResultItem]
ScopeQuery = "+template:{51ed5851-1a61-4dae-b803-6c7fae6b43d8};+custom:sxatags|Operations;+custom:sxatags|Accounting and Finance;custom:EventStartDate|[NOW TO *];custom:EventEndDate|[NOW TO *];sort:eventstartdate;sort:isfeatured[desc]"
}
Find-Item @props
using System.ComponentModel;
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.Converters;
using Sitecore.ContentSearch.SearchTypes;
using Sitecore.Data;
namespace Spe.Commands.Data.Search
{
public class SimpleSearchResultItem : ISearchResult
{
public string this[string index] { get => null; set => _ = value; }
public object this[ObjectIndexerKey key] { get => null; set => _ = value; }
[IndexField("_group")]
[TypeConverter(typeof(IndexFieldIDValueConverter))]
public virtual ID ItemId { get; set; }
[IndexField("_name")]
public virtual string Name { get; set; }
[IndexField("_content")]
public virtual string Content { get; set; }
[IndexField("_datasource")]
public virtual string Datasource { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment