naresh Azhagarselvam
Sep 30, 2016
  2861
(2 votes)

IPageCriteriaQueryService - GET THE RELATIVE URL OF A PAGE

GET THE RELATIVE URL OF A PAGE IN EPISERVER USING IPageCriteriaQueryService

Problem Statement

Imagine that we have a scenario where we will need to navigate between EPiServer pages in screens of wizards and one simple way to achieve the navigation is by using the below JavaScript

window.location.href = pageUrl

In this case, we will need to retrieve the relative URL of the EPiServer page so that it can be used in the above JavaScript statement

Solution

 Image Functional_Sequence.png

IPageCriteriaQueryService is an API in EPiServer using which we can search for pages programmatically. We can create Page criteria to search by page name , page id etc. We can create page level criteria using PropertyCriteria class and add those objects to a collection named PropertyCriteriaCollection. The API exposes a method FindPagesWithCriteria which searches for page instances using this PropertyCriteriaCollection object as parameter and the other parameter will be the content reference (page reference) where to start the search. The return type is a PageDataCollection object. We can retrieve the first page object from this collection and retrieve its virtual path using UrlResolver object as shown below.

Code snippet:

            var myPageUrl = GetPageRelativeURL<MyCMSPage>();

            public static string GetPageRelativeURL<PageToSearch>() where PageToSearch : BasePageClass

            {

               string returnurl = "/404";

               try

              {

                var pages = FindPagesWithCriteria<PageToSearch>();

                if (pages != null && pages.Any())

                {

                    returnurl = GetPageUrl(pages.FirstOrDefault());

                }

 

            }

            catch (Exception ex)

            {

                //logging mechanism

            }

            return returnurl;

        }

        private static PageDataCollection FindPagesWithCriteria<PageSearch>() where PageSearch : BasePageClass

        {

            PageDataCollection pages = new PageDataCollection();

            try

            {

                PropertyCriteriaCollection criterias = new PropertyCriteriaCollection();

                PropertyCriteria criteria = new PropertyCriteria();

                criteria.Condition = CompareCondition.Equal;

                criteria.Name = "PageTypeID";

                criteria.Type = PropertyDataType.PageType;

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

                criteria.Value = contentRepository.Load<PageSearch>().ID.ToString();

                criteria.Required = true;

                criterias.Add(criteria);

 

                IPageCriteriaQueryService queryService = ServiceLocator.Current.GetInstance<IPageCriteriaQueryService>();

                pages = queryService.FindPagesWithCriteria(ContentReference.StartPage, criterias);

             }

            catch (Exception ex)

            {

                 //Add your logging mechanism

            }

            return pages;

        }

 

       public static string GetPageUrl(IContent page)

        {

            if(page != null)

            {

               UrlResolver _urlResolver = ServiceLocator.Current.GetInstance<UrlResolver>();

 

                return _urlResolver.GetVirtualPath(page).GetUrl() ?? "/";

            }

            else

            {

                return "/";

            }

        }

Sep 30, 2016

Comments

Sep 30, 2016 04:16 PM

Hi,

Instead of using FindPagesWithCriteria, you can use IContentRepository please see http://world.episerver.com/documentation/Class-library/?documentId=cms/9/9018F8D5.

Sep 30, 2016 04:28 PM

It's also possible to add a few properties to the page/block of type content reference and let editor configure the function. Pro is that you don't need to scan the entire page tree which can affect performance. Con is that you need to set up a few properties the first time you use the function. 

Also noted that the above function will take the first randomly found item of a specific type. That works well if you only have one wizard function but will be problematic if you need to add a second. Your wizard might work well one day and fail the next if editor moves a page or two in the tree structure...

K Khan
K Khan Sep 30, 2016 04:39 PM

Which CMS Versions are you using?

arivoli.elamparithi
arivoli.elamparithi Oct 3, 2016 05:00 PM

Hi Johan,

Any reason why IContentRepository should be used over FindPagesWithCriteria ??

Thanks in advance.

arivoli.elamparithi
arivoli.elamparithi Oct 3, 2016 05:03 PM

Hi Khan,

We are using Episerver version 9.X.

Replying on behalf of Naresh.

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024