Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

Get the PageReference Object from the friendly URL(string).

Vote:
 

How can we get the PageReference Object from the friendly URL(string). I tried PermanentLinkMapStore, but failed... Can any body please guide.

Thanks

#41533
Jul 09, 2010 17:59
Vote:
 

I believe you need to play around with the URL rewriter to break a URL down into its internal representation - this contains an "id" parameter that you can grab a PageReference from.

Something like the code below should sort you out:

//* Convert an external URL to an internal URL           
UrlBuilder newURL = new UrlBuilder("http://EPiServer6TestSite/Home-page/");
Global.UrlRewriteProvider.ConvertToInternal(newURL);

//* Now fetch the ID and get a page reference
PageReference result;
string pageIDslug = newURL.QueryCollection["id"];
if (!string.IsNullOrEmpty(pageIDslug))
{
    string[] splitSlug = newURL.QueryCollection["id"].Split(new[] { '_' }, StringSplitOptions.RemoveEmptyEntries);

    int pageId;
    if (splitSlug.Length > 0 && int.TryParse(splitSlug[0], out pageId))
    {
        //* Do we have a work page ID?
        int workPageId;
        if (splitSlug.Length > 1 && int.TryParse(splitSlug[1], out workPageId))
        {
            result = new PageReference(pageId, workPageId);
        }
        else
        {
            result = new PageReference(pageId);
        }
    }
}

#41535
Jul 09, 2010 18:42
Vote:
 

PageReference.ParseURL() could be another choice. Need to convert it to an internal URL first though.

#41538
Jul 09, 2010 19:24
Vote:
 

Thanks, combination of both suggestions solved my problem

#41557
Jul 13, 2010 11:52
Vote:
 

Tahir

Another solution - very similar to above too . It has same concept to covert the Url to internal state.

 UrlBuilder urlBuilder = new UrlBuilder(Url);
                    object pageReference;

                    Global.UrlRewriteProvider.ConvertToInternal(urlBuilder, out pageReference);

                   //or use PageReference.ParseUrl() here


                      if (pageReference != null && pageReference is PageReference)
                    {
                       //ur page reference logic here
                    }

#41573
Edited, Jul 13, 2010 23:14
Vote:
 

"/mysite/en-gb/child/grandchild""/mysite/en-gb/child/grandchild"On a similar note, a complete newbie question by I can't seem to find the answer to this...

How do I get a reference to a page by absolute URL, e.g. "/mysite/child/grand-child"

PageReference.ParseURL looks like the correct method, but what am I missing?

            var relUrl = "/CommittedQuitters/Today/Blow-Temptation/";
            
            //var pageRef = PageReference.ParseUrl(relUrl);
            //var pageByLoc = DataFactory.Instance.GetPage(pageRef);

var url = "/mysite/child/grand-child";
var pageRef = PageReference.ParseUrl(url); <-- returns PageReference.EmptyReference
var page = DataFactory.Instance.GetPage(pageRef);

 

Eventually I will end up with several sites and languages, so the tree structure would be like so for example, so ideally i'd like to be able to retrieve the reference relatively...

"/mysite1/en-gb/child/grandchild"
"/mysite1/fr-fr/child/grandchild"

"/mysite2/en-gb/child/grandchild"
"/mysite2/it-it/child/grandchild"

"/mysite3/en-gb/child/grandchild"
"/mysite3/es-es/child/grandchild"

 

Let me know if you need info.

thanks

#44888
Oct 19, 2010 17:30
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.