2

I am working on a custom field which is based on :https://marketplace.sitecore.net/en/Modules/A/Advance_Image_Field.aspx

I am trying to make it suitable for SXA by changing the way to find thumbnails. So instead of looking for an ID I would like to use query with SXA tokens.

Sadly it throws an error :

Field control has failed to render: End of string expected at position 5.Details Sitecore.Data.Query.ParseException: End of string expected at position 5. at Sitecore.Data.Query.QueryParser.Raise(String error) at Sitecore.Data.Query.QueryParser.DoParse(String query) at Sitecore.Data.Query.Query..ctor(String query) at Sitecore.Data.Query.Query.SelectSingleItem(String query, QueryContext contextNode, Database database) at Sitecore.Data.DefaultDatabase.SelectSingleItem(String query) at HagerDeep.Foundation.Media.Fields.ImageWithFocusCrop.GetThumbnails() in D:\BitBucket\DEEP2\src\Foundation\Media\code\Fields\ImageWithFocusCrop.cs:line 639 at HagerDeep.Foundation.Media.Fields.ImageWithFocusCrop.DoRender(HtmlTextWriter output) in D:\BitBucket\DEEP2\src\Foundation\Media\code\Fields\ImageWithFocusCrop.cs:line 202 at Sitecore.Web.UI.WebControl.Render(HtmlTextWriter output) at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) at Sitecore.Shell.Applications.ContentEditor.EditorFieldContainer.RenderChildren(HtmlTextWriter writer)

Here's my code that throw the error:

var thumbnailFolderItem = Client.ContentDatabase.SelectSingleItem(ThumbnailsFolderQuery);

With the following query using SXA token :

query:$site/*[@@name='Presentation']/*[@@name='Thumbnails Ratio']/*[@@name='Fallback Thumbnail Folder']

Do I use the right function ? Or does the SXA tokens are not supported in code ?

2 Answers 2

3

Inject ITokenResolver and call Resolve() method.

ITokenResolver tokenResolver = ServiceLocator.ServiceProvider.GetService<ITokenResolver>()
string query = tokenResolver.Resolve(MY_QUERY, CONTEXT_ITEM);

and then for example:

CONTEXT_ITEM.Axes.SelectItems(query)

You can, of course, use construction injection instead of using ServiceLocator for getting ITokenResolver.

3
  • Hi Dawid, thanks for your response. Unfortunately I can't get the context item since I'm in a Field class which inherit from LinkBase. So the context item is the field itself in the core database. Moreover the method GetService<ITokenResolver>() ask for a type and I don't know which one to give it. Mar 7, 2019 at 15:59
  • 1
    Add the following usage: using Microsoft.Extensions.DependencyInjection;. There is almost always a way to get the context item. It is just not obvious in some cases. Check my answer here: sitecore.stackexchange.com/questions/17263/…. If URL contains "hdl" query string than it means that you can get object form session using this "hdl" and inside it you will find context item id. Mar 8, 2019 at 8:41
  • Thanks for the hint ! I am not a dependency injection expert but I finally found how to do it thanks to you. I'll add an answer to explain what I actually did. Mar 8, 2019 at 11:18
3

Here's my solution:

  1. I am using 3.7.1 version of the Sitecore.XA.Foundation.TokenResolution.dll so there is no service for token resolution but a static class.
  2. Then for getting current item it was really easy actually... Just add a property in my custom field.

Use those if Sitecore.XA.Foundation.TokenResolution is 3.8.0 or above

  • using Sitecore.DependencyInjection;
  • using Microsoft.Extensions.DependencyInjection;

In my case only that one was useful since it's not a service in 3.7.1

  • using Sitecore.XA.Foundation.TokenResolution;

    public class ImageWithFocusCrop : LinkBase
    {
        //Will get the target item ID
        public string ItemID { get; set; }
    
        private string GetThumbnails()
        {
             var targetItemID = ItemID;
             var targetItem = Client.ContentDatabase.GetItem(new Sitecore.Data.ID(targetItemID));
             //For 3.8.0 version and above
             ITokenResolver tokenResolver = ServiceLocator.ServiceProvider.GetService<ITokenResolver>();
             string query = tokenResolver.Resolve(ThumbnailsFolderQuery, targetItem );
             //For 3.7.1 version
             string query = TokenResolver.Resolve(ThumbnailsFolderQuery, targetItem);
    
             var thumbnailFolderItem = targetItem.Axes.SelectItems(query);
        }
    }
    

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.