Does this work for you:
var pageRouteHelper = ServiceLocator.Current.GetInstance<PageRouteHelper>();
var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
var page = contentRepository.Get<IContent>(pageRouteHelper.PageLink);
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.
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]);
}
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
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.
Thanks Dejan, it looks like I'll have to stick with my hack :)
Cheers
Jonathan.
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.
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
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.