1

We have upgraded Sitecore 9.0.2 to Sitecore 9.3 and it is SXA based solution.

And media search is not working after the upgrade. The Media dialog box shows an empty result and returns a response as below in the OOTB API call.

{
    "statusCode": 500,
    "error": {
        "message": "Exception has been thrown by the target of an invocation."
    }
}

enter image description here

Error screen with the console: enter image description here

I already tried this solution but not working for me, also tried rebuilding the index with no luck there.

If you have any suggestions or solutions. Will be Happy to hear you.

Thanks in Advance!

5
  • If you open that query from console in a new tab (/-/item/v1/sitecore/shell?facetsRootItemId=xxxx), you should be abe to see a corresponding log in Search.log (targeting the sitecore_master_index). Does that show an error? If not, if you run the query directly in SOLR dashboard, do you get any results? Sep 26, 2023 at 9:34
  • Also, please check that the parameter from query 'searchConfig' matches '/sitecore/client/Applications/Dialogs/SelectMediaDialog/PageSettings/SearchConfigs' in core database. That item should have 'sitecore/Media Library' as root, and also versioned/unversioned image in base templates. Sep 26, 2023 at 9:49
  • Thanks @DeanOBrien for your input, It does not show any error in search.log file. I do get the images when I run the query in Solr using a dashboard. Also I did verify the configuration in core db, those looks fine too. Sep 26, 2023 at 13:07
  • @SumitHelaiya, as it's a 500 error, are there any error messages in Sitecore logs when you open the Media dialog?
    – Anna Gevel
    Sep 26, 2023 at 15:16
  • Thanks @AnnaGevel, it's strange but the response as per screenshot in statuscode 500 and the request shows 200 in console and no log in log file or search log. Sep 26, 2023 at 17:25

2 Answers 2

1

One of the results you will find if you search your sitecore solution for one of the params (i.e. facetsRootItemId) is App_Config\Sitecore\SPEAK\Sitecore.Speak.ItemWebApi.config

You can use this file to find which processor deals with the search request, which is Sitecore.ItemWebApi.Pipelines.Request.Search, Sitecore.Speak.ItemWebApi.

What I suggest is overriding that processor, using the code from the decompiled DLL. Then stick a break point in the code and see if and where any of the OTB code is failing, this should help you diagnose further the issue.

Example code below:

using Sitecore.ContentSearch;
using Sitecore.ContentSearch.Linq;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Globalization;
using Sitecore.ItemWebApi.Facets;
using Sitecore.ItemWebApi.Pipelines.Request;
using Sitecore.ItemWebApi.Pipelines.Search;
using Sitecore.Pipelines;
using Sitecore.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace YourNameSpace.Pipeline
{
    public class SearchRequestProcessor : RequestProcessor
    {
        private const string SearchParameterName = "search";
        private const string LanguageParameterName = "language";
        private const string ShowHiddenItemsParameterName = "showHiddenItems";

        public override void Process(RequestArgs args)
        {
            Assert.ArgumentNotNull((object)args, nameof(args));
            string searchText = WebUtil.GetQueryString("search", (string)null);
            if (searchText == null)
            {
                if (!args.Context.HttpContext.Request.HasQueryStringFlag("search"))
                    return;
                searchText = string.Empty;
            }
            string str = WebUtil.GetQueryString("showHiddenItems", (string)null);
            if (str == null)
                str = string.Empty;
            bool showHiddenItems = str == "true";
            string queryString = WebUtil.GetQueryString("language", string.Empty);
            if (!string.IsNullOrEmpty(queryString))
            {
                using (new LanguageSwitcher(queryString))
                    this.RunSearchPipeline(args, searchText, queryString, showHiddenItems);
            }
            else
                this.RunSearchPipeline(args, searchText, queryString, showHiddenItems);
        }

        private void RunSearchPipeline(
          RequestArgs args,
          string searchText,
          string languageName,
          bool showHiddenItems)
        {
            Item searchDefinition = this.GetSearchDefinition(Sitecore.Context.Database);
            Item rootItem = this.GetRootItem(args, searchDefinition);
            string searchText1 = this.GetSearchText(searchDefinition, searchText);
            using (IProviderSearchContext searchContext = ContentSearchManager.GetIndex((IIndexable)new SitecoreIndexableItem(rootItem)).CreateSearchContext())
            {
                SearchArgs args1 = new SearchArgs(searchContext, searchDefinition, rootItem, searchText1, languageName, showHiddenItems);
                CorePipeline.Run("itemWebApiSearch", (Sitecore.Pipelines.PipelineArgs)args1);
                SearchResults<ConvertedSearchResultItem> results = args1.Queryable.GetResults<ConvertedSearchResultItem>();
                args.Items = results.Hits.Select<SearchHit<ConvertedSearchResultItem>, Item>((Func<SearchHit<ConvertedSearchResultItem>, Item>)(s => s.Document.GetItem())).Where<Item>((Func<Item, bool>)(x => x != null)).ToArray<Item>();
                args.CustomData["totalCount"] = (object)results.TotalSearchResults;
                if (results.Facets == null)
                    return;
                args.CustomData["facets"] = (object)this.GetFacets(args1.FacetProviders, results.Facets);
            }
        }

        private IEnumerable<Facet> GetFacets(
          IList<IFacetProvider> facetProviders,
          FacetResults facets)
        {
            List<Facet> facets1 = new List<Facet>();
            foreach (FacetCategory category in facets.Categories)
            {
                FacetCategory facetCategory = category;
                IFacetProvider facetProvider = facetProviders.FirstOrDefault<IFacetProvider>((Func<IFacetProvider, bool>)(provider => provider.CanHandle(facetCategory.Name)));
                Facet facet = facetProvider == null ? new Facet(facetCategory.Name) : facetProvider.GetFacet(facetCategory.Name);
                foreach (Sitecore.ContentSearch.Linq.FacetValue facetValue in (IEnumerable<Sitecore.ContentSearch.Linq.FacetValue>)facetCategory.Values.Where<Sitecore.ContentSearch.Linq.FacetValue>((Func<Sitecore.ContentSearch.Linq.FacetValue, bool>)(v => v.AggregateCount != 0)).OrderBy<Sitecore.ContentSearch.Linq.FacetValue, string>((Func<Sitecore.ContentSearch.Linq.FacetValue, string>)(v => v.Name)).ThenByDescending<Sitecore.ContentSearch.Linq.FacetValue, int>((Func<Sitecore.ContentSearch.Linq.FacetValue, int>)(value => value.AggregateCount)))
                {
                    Sitecore.ItemWebApi.Facets.FacetValue value;
                    if (facetProvider != null)
                    {
                        string name = facetValue.Name;
                        value = facetProvider.GetValue(facet, name);
                    }
                    else
                        value = new Sitecore.ItemWebApi.Facets.FacetValue(facet.Name + ":" + facetValue.Name, facetValue.Name)
                        {
                            DisplayText = Capitalize(facetValue.Name)
                        };
                    value.Count = facetValue.AggregateCount;
                    if (facet.Values.Exists((Predicate<Sitecore.ItemWebApi.Facets.FacetValue>)(v => v.Text == value.Text)))
                        facet.Values.Find((Predicate<Sitecore.ItemWebApi.Facets.FacetValue>)(v => v.Text == value.Text)).Count += value.Count;
                    else
                        facet.Values.Add(value);
                }
                facets1.Add(facet);
            }
            return (IEnumerable<Facet>)facets1;
        }

        private Item GetRootItem(RequestArgs args, Item searchDescriptor)
        {
            string queryString = WebUtil.GetQueryString("root", searchDescriptor != null ? searchDescriptor["root"] : string.Empty);
            return string.IsNullOrEmpty(queryString) ? Sitecore.Context.ContentDatabase.GetRootItem() : Sitecore.Context.ContentDatabase.GetItem(queryString);
        }

        private Item GetSearchDefinition(Database database)
        {
            string str = WebUtil.GetQueryString("searchConfig", (string)null) ?? ParseSearchText.AllItemId.ToString();
            if (ID.IsID(str))
            {
                Item searchDefinition = database.GetItem(str);
                if (searchDefinition != null)
                    return searchDefinition;
            }
            return database.GetItem(ParseSearchText.SearchId)?.Children[str];
        }

        private string GetSearchText(Item searchDescriptor, string searchText)
        {
            if (searchDescriptor == null)
                return searchText;
            string empty = string.Empty;
            if (!string.IsNullOrEmpty(searchDescriptor[nameof(Search)]))
                empty = searchDescriptor[nameof(Search)];
            if (GetBool(searchDescriptor["AppendSearchText"], true) && !string.IsNullOrEmpty(searchText))
                empty += searchText;
            return empty;
        }
        public static bool GetBool(string value, bool defaultValue)
        {
            if (string.IsNullOrEmpty(value))
                return defaultValue;
            return value.Equals("true", StringComparison.OrdinalIgnoreCase) || value.Equals("yes", StringComparison.OrdinalIgnoreCase) || value.Equals("1", StringComparison.OrdinalIgnoreCase);
        }
        public static string Capitalize(string text)
        {
            string str = text.Trim();
            return str.Length == 0 ? string.Empty : str.Substring(0, 1).ToUpperInvariant() + str.Substring(1).ToLowerInvariant();
        }
    }
    internal static class RequestExtensions
    {
        private static readonly string[] Separator = new string[1]
        {
      ","
        };

        internal static bool HasQueryStringFlag(this HttpRequest request, string parameterName)
        {
            if (WebUtil.GetQueryString(parameterName, (string)null) != null)
                return true;
            string str = request.QueryString[(string)null];
            return !string.IsNullOrEmpty(str) && ((IEnumerable<string>)str.Split(RequestExtensions.Separator, StringSplitOptions.RemoveEmptyEntries)).Contains<string>(parameterName);
        }
    }
}

Sitecore patch:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
        <processor type="YourNameSpace.Pipeline.SearchRequestProcessor, YourNameSpace" patch:instead="processor[@type='Sitecore.ItemWebApi.Pipelines.Request.Search, Sitecore.Speak.ItemWebApi']" />
      </itemWebApiRequest>
    </pipelines>
  </sitecore>
</configuration>
0

This is a known issue with Sitecore 9.3 and If the default Home item is deleted, then recently uploaded files are not shown in the Select media dialog.

To resolve the issue, consider one of the following options:

  • Upgrade Sitecore XP instance to 10.0.0 or later.

  • Recover the default Home item as follows:

    1. Install a new Sitecore XP instance.
    2. Create a Sitecore package with a default "Home" item from a clear instance.
    3. Install this package in the affected instance.

Reference: Select media dialog does not show any images if the default "Home" item is deleted

Hope it helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.