2

I am updating index dependencies as mentioned in Sitecore Full Content Search where we are using GetDatasourceDependencies.

Now, the issue is that this works fine for the English language, but, whenever I publish content for a non-English version, it's still taking dependency in the English version, but the dependency for published item is actually in a non-English version. where as Item object is in fr-FR version as returned by this piece of code Item item = (Item)(context.IndexedItem as SitecoreIndexableItem); it's dependency is still returned in English version i think last two lines of processor need to pass language code some where but not sure where

My Pipeline code is like this:

public class GetDatasourceDependencies : BaseProcessor
{
    public override void Process(GetDependenciesArgs context)
    {
        Func<ItemUri, bool> func = null;
        Assert.IsNotNull(context.IndexedItem, "indexed item");
        Assert.IsNotNull(context.Dependencies, "dependencies");
        Item item = (Item)(context.IndexedItem as SitecoreIndexableItem);

        if (item != null)
        {
            if (func == null)
            {
                func = uri => (bool)((uri != null) && ((bool)(uri != item.Uri)));
            }
            System.Collections.Generic.IEnumerable<ItemUri> source = Enumerable.Where<ItemUri>(from l in Globals.LinkDatabase.GetReferrers(item, FieldIDs.LayoutField) select l.GetSourceItem().Uri, func).Distinct<ItemUri>();
            context.Dependencies.AddRange(source.Select(x => (SitecoreItemUniqueId)x));
        }
    }
}

How to correctly process index dependencies for correct language?

  • What is your question, exactly? – Dmytro Shevchenko Feb 23 '18 at 11:54
  • Even though my Item in this code Item item = (Item)(context.IndexedItem as SitecoreIndexableItem); is of fr-FR version it's still returning dependency in English version – user2332873 Feb 23 '18 at 11:56
  • I think there we need to do some modification in these two lines of code . But not sure where System.Collections.Generic.IEnumerable<ItemUri> source = Enumerable.Where<ItemUri>(from l in Globals.LinkDatabase.GetReferrers(item, FieldIDs.LayoutField) select l.GetSourceItem().Uri, func).Distinct<ItemUri>(); context.Dependencies.AddRange(source.Select(x => (SitecoreItemUniqueId)x)); – user2332873 Feb 23 '18 at 12:01
  • Note that you can edit your question to add more details if you want. – Dmytro Shevchenko Feb 23 '18 at 12:02
2

It looks like you need to include the Final Layout field as well. The Final Layout field will differ per version of your item, i.e. a different language. The primary Layout field is set to [shared], which means it's shared across languages.

public override void Process(GetDependenciesArgs context)
{
    Assert.IsNotNull(context.IndexedItem, "indexed item");
    Assert.IsNotNull(context.Dependencies, "dependencies");

    var item = (Item)(context.IndexedItem as SitecoreIndexableItem);

    if (item == null)
        return;

    bool Func(ItemUri uri) => (uri != null) && uri != item.Uri;

    var source = (
        from l 
            in Globals.LinkDatabase.GetReferrers(item, FieldIDs.LayoutField)
        select l.GetSourceItem().Uri)
            .Where(Func)
            .ToList();

    // this is what you're missing
    source.AddRange(
    (
        from l
            in Globals.LinkDatabase.GetReferrers(item, FieldIDs.FinalLayoutField)
        select l.GetSourceItem().Uri)
            .Where(Func)
    );

    source = source.DistinctBy(i => i.ItemID).ToList();

    context.Dependencies.AddRange(source.Select(x => (SitecoreItemUniqueId)x));
}

The extension DistinctBy() is listed below:

public static class IEnumerableExtensions
{
    /// <summary>
    /// Filters items out of a collection that have distinct key values. Key values are determined by <paramref name="keySelector"/>.
    /// </summary>
    /// <typeparam name="TSource">Type of the enumerable object</typeparam>
    /// <typeparam name="TKey">Type of the keys</typeparam>
    /// <param name="source">The items to select from</param>
    /// <param name="keySelector">A function to select key values</param>
    /// <returns>The items that have distinct key values</returns>
    public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
    {
        var seenKeys = new HashSet<TKey>();
        return source.Where(element => seenKeys.Add(keySelector(element)));
    }
}
| improve this answer | |
  • For consideration of search we are only taking renderings added on Shared layout final layout is not considered at all . If i remove the final layout code snippet from your solution Will i be able to get right dependency – user2332873 Feb 23 '18 at 14:40
  • Why aren't you considering the Final Layout? What exactly are you trying to index? – jrap Feb 23 '18 at 14:48
  • We are indexing everything on shared layout since all the content Authoring is taking place on shared layout only. – user2332873 Feb 23 '18 at 14:52
  • 1
    For that to be the case, your authors would need to work exclusively in Content Editor, is this the case? Also, how are you versioning EN content from FR? Again, the Shared Layout is shared so if an item has a version in EN and FR and you make a change in either language version, both language versions will get the change. Do you have separate tree structures for EN vs FR? – jrap Feb 23 '18 at 14:57
  • if an item has a version in EN and FR since it's shared layout we are fine for both language version to get change since content and look and feel will not vary across languages – user2332873 Feb 23 '18 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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