Blog posts by Naresh Azhagarselvam2016-09-30T14:02:28.3570000Z/blogs/naresh-azhagarselvam/Optimizely WorldIPageCriteriaQueryService - GET THE RELATIVE URL OF A PAGE /blogs/naresh-azhagarselvam/dates/2016/9/first-blog/2016-09-30T14:02:28.3570000Z<h2>GET THE RELATIVE URL OF A PAGE IN EPISERVER USING IPageCriteriaQueryService</h2>
<h2>Problem Statement</h2>
<p>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</p>
<p>window.location.href = pageUrl</p>
<p>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</p>
<h2>Solution</h2>
<p> <img src="/link/95f28fc2bb934720a6c0a8a2c1fe66c3.aspx" alt="Image Functional_Sequence.png" /></p>
<p><strong>IPageCriteriaQueryService</strong> 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 <strong>PropertyCriteria class </strong>and add those objects to a collection named <strong>PropertyCriteriaCollection. </strong>The API exposes a method <strong>FindPagesWithCriteria</strong> which searches for page instances using this <strong>PropertyCriteriaCollection </strong>object as parameter and the other parameter will be the content reference (page reference) where to start the search. The return type is a <strong>PageDataCollection</strong> object. We can retrieve the first page object from this collection and retrieve its virtual path using <strong>UrlResolver</strong> object as shown below.</p>
<p><strong>Code snippet:</strong></p>
<p><strong> </strong>var myPageUrl = GetPageRelativeURL<MyCMSPage>();</p>
<p><strong> </strong>public static string GetPageRelativeURL<PageToSearch>() where PageToSearch : BasePageClass</p>
<p> {</p>
<p> string returnurl = "/404";</p>
<p> try</p>
<p> {</p>
<p> var pages = FindPagesWithCriteria<PageToSearch>();</p>
<p> if (pages != null && pages.Any())</p>
<p> {</p>
<p> returnurl = GetPageUrl(pages.FirstOrDefault());</p>
<p> }</p>
<p> </p>
<p> }</p>
<p> catch (Exception ex)</p>
<p> {</p>
<p> //logging mechanism</p>
<p> }</p>
<p> return returnurl;</p>
<p> }</p>
<p> private static PageDataCollection FindPagesWithCriteria<PageSearch>() where PageSearch : BasePageClass</p>
<p> {</p>
<p> PageDataCollection pages = new PageDataCollection();</p>
<p> try</p>
<p> {</p>
<p> PropertyCriteriaCollection criterias = new PropertyCriteriaCollection();</p>
<p> PropertyCriteria criteria = new PropertyCriteria();</p>
<p> criteria.Condition = CompareCondition.Equal;</p>
<p> criteria.Name = "PageTypeID";</p>
<p> criteria.Type = PropertyDataType.PageType;</p>
<p> var contentRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();</p>
<p> criteria.Value = contentRepository.Load<PageSearch>().ID.ToString();</p>
<p> criteria.Required = true;</p>
<p> criterias.Add(criteria);</p>
<p> </p>
<p> IPageCriteriaQueryService queryService = ServiceLocator.Current.GetInstance<IPageCriteriaQueryService>();</p>
<p> pages = queryService.FindPagesWithCriteria(ContentReference.StartPage, criterias);</p>
<p> }</p>
<p> catch (Exception ex)</p>
<p> {</p>
<p> //Add your logging mechanism</p>
<p> }</p>
<p> return pages;</p>
<p> }</p>
<p> </p>
<p> public static string GetPageUrl(IContent page)</p>
<p> {</p>
<p> if(page != null)</p>
<p> {</p>
<p> UrlResolver _urlResolver = ServiceLocator.Current.GetInstance<UrlResolver>();</p>
<p> </p>
<p> return _urlResolver.GetVirtualPath(page).GetUrl() ?? "/";</p>
<p> }</p>
<p> else</p>
<p> {</p>
<p> return "/";</p>
<p> }</p>
<p> }</p>