Try our conversational search powered by Generative AI!

Filter DataFactory.Instance.GetReferencesToContent on sites in EPiServer CMS 9

Vote:
 

Is there a way to filter DataFactory.Instance.GetReferencesToContent on sites? E.g. we want only to get references on the same site as the user visits.

We have a multisite solution and I've tried to fetch a SiteDefinition or a startpage or anything that I can compare to the original reference but haven't found any way of doing that. Haven't found a way of getting a SiteDefinition from either ContentReference, PageData or any other object. Feels weird that it should be so difficult so I have a feeling that I'm missing something.

#145121
Feb 25, 2016 13:21
Vote:
 

Here you have two extension methods. You could easily change them to use contentlink instead of IContent if you feel like:

public static int SiteIdAsInt(this IContent content)
        {
            var definition = ServiceLocator.Current.GetInstance<SiteDefinitionResolver>().GetDefinitionForContent(content.ContentLink, false, false);
            if (definition == null)
            {
                return -1;
            }
            return definition.StartPage.ID;
        }

        public static Guid SiteIdAsGuid(this IContent content)
        {
            var definition = ServiceLocator.Current.GetInstance<SiteDefinitionResolver>().GetDefinitionForContent(content.ContentLink, false, false);
            if (definition == null)
            {
                return Guid.Empty;
            }
            return definition.Id;
        }



#145124
Feb 25, 2016 13:40
Vote:
 
<p>Thanks! I used a version of this to get it to work.&nbsp;</p> <p>If someone is interested, this is the code I ended up using.</p> <pre class="brush:csharp;auto-links:false;toolbar:false" contenteditable="false">var references = DataFactory.Instance.GetReferencesToContent(authorBlockLink, false); var siteDefinitionResolver = ServiceLocator.Current.GetInstance&lt;SiteDefinitionResolver&gt;(); var referencesList = new List&lt;ReferenceInformation&gt;(); var siteGuid = SiteDefinition.Current.Id; foreach (var reference in references) { var referenceSiteDefinition = siteDefinitionResolver.GetDefinitionForContent(reference.OwnerID, false, false); if (referenceSiteDefinition == null) continue; if (referenceSiteDefinition.Id != siteGuid) continue; if (!reference.OwnerID.IsPublishedAndUserHasAccess()) continue; referencesList.Add(reference); }</pre> <p></p>
#145131
Feb 25, 2016 14:55
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.