Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Get related pages?

Vote:
 

While saving a page I want to check which pages are related to it similar to whats happening when you move a page to the recycle bin.

I found this tutorial on how to build a plugin in edit to do this https://gregwiechec.com/2015/06/displaying-related-content-in-edit-mode/ but I want to do this in code so this way doesn't really help me.

Any advice is much appreciated

#150709
Jun 28, 2016 11:33
Vote:
 

What do you mean by related?
Do you want to get all content that has a reference to your page?

The contentRepository has a GetReferencesToContent method you could have a look at.

You can have a look here of how to use it https://blog.i4code.nl/referencing-content-gadget/

Basically you would want to hook into the Publishevent with an InitializationModule and run your code there :)

Here's how you can hook into the event: http://world.episerver.com/blogs/Janaka-Fernando/Dates/2015/4/adding-custom-logic-to-your-publishing-step/

[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class InitializationModule : IInitializableModule {
	public void Initialize(InitializationEngine context) {
		var contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
		contentEvents.PublishingContent += contentEvents_PublishingContent;
	}

	public void Uninitialize(InitializationEngine context) {
		//Add uninitialization logic
	}

	void contentEvents_PublishingContent(object sender, EPiServer.ContentEventArgs e) {
		var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
		var related = contentRepository.GetReferencesToContent(e.ContentLink, includeDecendents: false);
	}
}



#150717
Edited, Jun 28, 2016 14:00
Vote:
 

Yes GetReferencesToContent worked. Strange i wasn't able to find that solution by googling. I instead reflected EPiServer.UI.dll on an episerver 6 site and checked how it was done there.

#150725
Jun 28, 2016 15:14
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.