+1-888-365-2779
Try Now
More in this section

Forums / Bugs & Issues / GetPage By URL

GetPage By URL

9 posts, 0 answered
  1. Nadya
    Nadya avatar
    52 posts
    Registered:
    17 Dec 2008
    16 Sep 2009
    Link to this post
    Hello,
    I am using Sitefinity 3.7 to develop a sitefinity website and I cannot find how to get a CMSPage by only having it's primary url.

    Right now I am doing it like this :

     public static CmsPage GetPageByUrl(string pageUrl) 
            { 
                CmsManager cms = new CmsManager(); 
                IList pages = cms.GetPages(); 
                string language = Thread.CurrentThread.CurrentUICulture.Name; 
                string url =  pageUrl.StartsWith("~")  ? pageUrl : "~" + pageUrl; 
                 
                foreach(CmsPageContent page in pages) 
                { 
                    if (page.StaticUrl.Equals(url)) 
                    {                     
                                return ((CmsPage)(p)); 
                    } 
                } 
     
                return null
            } 


    May be there's some better way ?

    Thanks.
    Have a great day,
    Nadya
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    16 Sep 2009
    Link to this post
    Hi Nadya,

    Below is  sample code that illustrates another approach.

     private ICmsPage GetPageByUrl(string url) 
        { 
            ICmsUrlContext context = UrlHelper.GetUrl(url, true); 
            CmsSiteMapNode node = context.GetSiteMapNode() as CmsSiteMapNode; 
            ICmsPage page = node.CmsPage; 
            return page; 
        } 
     
     
        void button1_Click(object sender, EventArgs e) 
         { 
                // here you need to pass an url 
                // for instance passing the URL from a TextBox 
                ICmsPage page = GetPageByUrl(tb.Text); 
                // do something with this page 
         } 


    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. antoniodlp
    antoniodlp avatar
    13 posts
    Registered:
    28 Mar 2007
    22 Apr 2010
    Link to this post
    Hi,

    I'm trying to use this approach but whenever I call UrlHelper.GetUrl(string, bool) the page never finishes loading. It just keeps processing for hours and then I have to restart IIS.

    Does UrlHelper.GetUrl really work?

    Has this happened to anyone else?

  4. Nadya
    Nadya avatar
    52 posts
    Registered:
    17 Dec 2008
    22 Apr 2010
    Link to this post
    Hi,
    I also couldn't make it work. I am still using my method with the cycle.

    Best, Nadya
  5. antoniodlp
    antoniodlp avatar
    13 posts
    Registered:
    28 Mar 2007
    22 Apr 2010
    Link to this post
    Thank you Nadya!

    Well I found that it does work but only not in the place where I'm trying to use it which is the SearchResult.ascx template. I don't know why it crashes the site; I'll have to find a workaround.

    I'll try your method first as it seems very straightforward.
  6. Andreas
    Andreas avatar
    49 posts
    Registered:
    29 Dec 2007
    20 Oct 2010
    Link to this post
    Not working for me as well in 3.7 SP4. Never finish looping and crashes.
  7. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    20 Oct 2010
    Link to this post
    Hi Andreas,

    Most probably the value you pass to GetPageByUrl method is not correct the the method cannot evaluate it.

    Please consider the sample below

        private ICmsPage GetPageByUrl(string url)
        {
            ICmsUrlContext context = UrlHelper.GetUrl(UrlPath.ResolveUrl(url), true);
            CmsSiteMapNode node = context.GetSiteMapNode() as CmsSiteMapNode;
            ICmsPage page = node.CmsPage;
            return page;
        }
     
     
    void test_Click(object sender, EventArgs e)
    {
        ICmsPage page = GetPageByUrl("~/sss.aspx");
    }


    Greetings,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  8. Edward
    Edward avatar
    9 posts
    Registered:
    12 Oct 2012
    29 Sep 2011
    Link to this post
    Ivan,

    This code snippet does not work for me (3.7 SP1 CE):

    ICmsUrlContext context = UrlHelper.GetUrl(UrlPath.ResolveUrl(url), true);

    I get an error stating, "The name 'UrlPath' does not exist in the current context."

    Here is my code:

    protected void NewApplicationsGridView_SelectedIndexChanged(object sender, EventArgs e)
       {
           String ApplicantID = (String)NewApplicationsGridView.SelectedDataKey.Value.ToString();
           ICmsPage page = GetPageByUrl("~/HRDashboard/Applicants/Applications.aspx");
           Response.Write(page + "?ApplicantID=" + ApplicantID);
       }
       private ICmsPage GetPageByUrl(string url)
       {
           ICmsUrlContext context = UrlHelper.GetUrl(UrlPath.ResolveUrl(url), true);
           CmsSiteMapNode node = context.GetSiteMapNode() as CmsSiteMapNode;
           ICmsPage page = node.GetCmsPage();
           return page;
       }
  9. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    30 Sep 2011
    Link to this post
    Hello Edward,

    Check if it is available under Telerik.Web namespace

    http://www.sitefinity.com/help/developer-manual/telerik.framework-telerik.web.urlpath.html

    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Register for webinar
9 posts, 0 answered