Accessing CurrentPage from with a class file

Vote:
 

Hi

I'm trying to get a reference to the current page from within a class. All I can find to accomplish this is the line below. However pageBase is always null.

var pageBase = HttpContext.Current.Handler as EPiServer.PageBase;

I read on this post that the method doesn't work on an MVC site.

http://world.episerver.com/Forum/Developer-forum/EPiServer-7-CMS/Thread-Container/2013/6/Get-current-page-from-block/

Can anyone help?

Thanks

Jonathan.

#86889
Jun 04, 2014 5:21
Vote:
 

Does this work for you:

var pageRouteHelper = ServiceLocator.Current.GetInstance<PageRouteHelper>();
var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();

var page = contentRepository.Get<IContent>(pageRouteHelper.PageLink);
#86897
Jun 04, 2014 10:25
Vote:
 

Depending on exactly what you're trying to do, sometimes it's easier to just send in the pageId of the page as a parameter to the class and then fetch it using IContentRepository.

#86922
Jun 04, 2014 12:18
Vote:
 

Hi

Dejan: Unfortunately that doesn't work. I have similar code which I use all the time. In this instance it always returns the home page for some reason.

Anders: In this case I'm a little unsure how to pass the pageId as a parameter. In edit mode I'm rendering a string property as a dropdown using the SelectOne attribute.

[SelectOne(SelectionFactoryType = typeof(ComparisonTypeSelectionFactory))]

Within my ComparisonTypeSelectionFactory class is where I'm trying to get a reference to the current page. My code is below

public class ComparisonTypeSelectionFactory : ISelectionFactory
{
public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
{
var selectItems = new List<SelectItem>();

PageData currentPage = HtmlHelpers.GetCurrentPage;
PageData pageData = HtmlHelpers.GetParentPageOfType(currentPage,
typeof (CategoryDetailPage).Name);

if (pageData != null && ((CategoryDetailPage)pageData).ComparisonNames.Length > 0)
{
string[] items = ((CategoryDetailPage) pageData).ComparisonNames;

foreach (string item in items)
{
string[] splitItem = item.Split(new string[] {"|"}, StringSplitOptions.None);
if (splitItem[1] == "1")
{
selectItems.Add(new SelectItem()
{
Value = (string.IsNullOrEmpty(splitItem[0]) ? " " : splitItem[0]),
Text = splitItem[0]
});
}
}
}

return selectItems;
}

}

The HtmlHelpers.GetCurrentPage basically does what Dejan's code does but always returns the home page.

As a hack I've resorted to using the URL and extracting the pageId from there.

string url = HttpContext.Current.Request.Url.Query.ToLower();
Int32 pageId = 0;
if (url.Contains("contentlink")) // ### Updating a page
{
pageId = Convert.ToInt32(url.Substring(url.IndexOf("%3a%22") + 6)
.Split(new string[] { "_" }, StringSplitOptions.None)[0]);
}
else if (url.Contains("parentlink")) // ### Adding a page
{
pageId = Convert.ToInt32(url.Substring(url.IndexOf("%3a%22") + 6)
.Split(new string[] { "%22" }, StringSplitOptions.None)[0]);
}

#86945
Jun 05, 2014 1:12
Vote:
 

Jonathan I haven't tried this myself but can you use the ContentRouteHelper to get the current page?

ServiceLocator.Current.GetInstance<ContentRouteHelper>();
http://world.episerver.com/Documentation/Class-library/?documentId=cms/7.5/671ae85e-93e4-abbb-0a51-aa4a61d52bb1

#86948
Jun 05, 2014 1:29
Vote:
 

Hi Toni, unfortunately that returns null values.

#86949
Jun 05, 2014 2:08
Vote:
 

Hi Jonathan,

The code I sent you can be used in Page/Block controllers, external classes, etc. but it cannot be used in Selection factories.

I tried to extract the current page / container from ExtendedMetadata, but no luck :( The URL hack you posted seems like the only way.

#86955
Jun 05, 2014 10:40
Vote:
 

Thanks Dejan, it looks like I'll have to stick with my hack :)

Cheers

Jonathan.

#86992
Jun 06, 2014 1:04
Vote:
 

ContentRouteHelper (and PageRouteHelper) gives the content that the routing routed to. So for "normal" request (that is request in view, preview mode) it will work, however for some request from the UI interface (that has it own routing) it will not work since those request is not routed through the same routes.

#87030
Jun 07, 2014 22:30
Vote:
 

Hi I think below code will return you the requrie current page object.

  public class ProductFeatureSelectionFactory : ISelectionFactory
    {
public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
        {

ProductPage productPage = ((EPiServer.Cms.Shell.UI.ObjectEditing.ContentDataMetadata) (metadata)).OwnerContent as ProductPage;

....
....

}
}

Thanks

Ali, Murtaza

#118946
Edited, Mar 18, 2015 5:13
Ted
Vote:
 

Actually, there's no need to cast the ExtendedMetadata object.

Instead you can use the FindOwnerContent extension method like so:

var ownerContent = metadata.FindOwnerContent();

The extension method is in the EPiServer.Cms.Shell.Extensions namespace.

#143344
Jan 20, 2016 22:40