2

I am working on EPiServer. I am creating a plugin to edit content of current page. I am trying to get the data from the code following:

 var pageRouteHelper = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IPageRouteHelper>();

 var currentPage = pageRouteHelper.Page;

but every time the currentPage returns me the content of the start page. What is missing here and I cannot get the page I have open on screen content?

Grigoris Loukidis
  • 383
  • 2
  • 7
  • 23
  • 2
    It will fallback to StartPage if it can't get the current page. Are you sure you are on an actual page when requesting this and not inside a web api controller or similar? – Thomas Schmidt Aug 21 '18 at 10:55
  • The page id changes on the address bar. It was 5 on the start page and now it is 139. So I think the page changes. – Grigoris Loukidis Aug 21 '18 at 11:10
  • Is this a plugin that's used within edit mode, seems like since "The page id changes on the address bar"? Then there is other ways to get the selected page in the page tree. In edit mode the selected page id is in the fragment part of the URL, which is never sent to the server, so IPageRouteHelper can't resolve it. – Johan Petersson Aug 21 '18 at 11:28
  • Yes it works on edit mode. Which ways can help me on getting current page's content? – Grigoris Loukidis Aug 21 '18 at 11:34
  • I think we need to take a step back and figure out exactly what you are trying to do, are you making a gadget just like the gadgets you have in the cms editor (versions, recent etc) or a tool or making some code in a page controller or what are you actually trying to do? – Thomas Schmidt Aug 21 '18 at 11:38
  • Something like a tool for get a page's text or content on a controller. – Grigoris Loukidis Aug 21 '18 at 11:39
  • Are you able to pass the page ID to your control? It will be the easiest way for you – Boris Sokolov Aug 21 '18 at 16:57
  • I would say that code isn't executed within the context of a page. That code _would_ work in a controller, or in a page template (i.e. Razor view), but it _will not_ work in a gadget as it won't be executed within the context of a page request. Can you share more of your code? – Ted Nyberg Aug 22 '18 at 07:00
  • @TedNyberg the two lines of code I have uploaded are located in the start of the controller. – Grigoris Loukidis Aug 22 '18 at 07:13
  • 1
    What kind of controller? If you have a content parameter called `currentPage`, it will automatically be populated if the request is part of a page request. – Ted Nyberg Aug 23 '18 at 13:37

1 Answers1

1

If we have the page id, for instance 139, with the following code we can retrieve the page's content.

var pageId = 139;

var pageRef = new PageReference(pageId);

var contentRepository = ServiceLocator.Current.GetInstance<IContentLoader>();

var page = contentRepository.Get<PageData>(pageRef);

A simply, but may not the most practical way, to get the page id from the browser is

var pageUrl = HttpContext.Current.Request.RawUrl;

From the pageUrl we can retrieve the pageId.

Grigoris Loukidis
  • 383
  • 2
  • 7
  • 23