Indexing content that is attached to some page

Vote:
 

Hello,

I have this requirment to only index that data, for searching, which is being used in some page. For example if I upload a document it shouldn't be available for search until I use it in some page. I found out this code online

            ContentIndexer.Instance.Conventions.ForInstancesOf().ShouldIndex(x =>
            {
                var contentRepository =
                    EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance();
                var contentSoftLinkRepository =
                    EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance();
                var softLinks = contentSoftLinkRepository.Load(x.ContentLink, true);

                try
                {
                    foreach (var softLink in softLinks)
                    {

                        if (softLink.SoftLinkType == ReferenceType.ExternalReference ||
                            softLink.SoftLinkType == ReferenceType.ImageReference)
                        {
                            var content =
                                contentRepository.Get(softLink.OwnerContentLink);
                            if (!ContentIndexer.Instance.Conventions.ShouldIndexConvention.ShouldIndex(content).Value) // don't index referenced file if content is marked as not indexed
                            {
                                continue;
                            }

                            // only index if content is published
                            var publicationStatus =
                                content.PublishedInLanguage()[softLink.OwnerLanguage.Name];

                            if (publicationStatus != null &&
                                (publicationStatus.StartPublish == null ||
                                 publicationStatus.StartPublish < DateTime.Now) &&
                                (publicationStatus.StopPublish == null ||
                                 DateTime.Now < publicationStatus.StopPublish))
                            {
                                return true;
                            }
                        }
                    }
                }
                catch
                {
                    // ooops something went wrong. Better not index this one ;-)
                }

                return false;
            });

This works when I attach softlinks. But lets say a page has a property called Content type and when I add something in there, lets say a block which has softlinks to that document, it doesn't work. I am stuck in there. Any hints?

#195712
Edited, Aug 06, 2018 11:09
* 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.