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

Forums / Developing with Sitefinity / Retrieve Page Title and Page URL from a widget

Retrieve Page Title and Page URL from a widget

8 posts, 0 answered
  1. kele
    kele avatar
    10 posts
    Registered:
    05 Nov 2013
    05 Dec 2013
    Link to this post
    We need to create social sharing link urls in a widget:

    For instance something like this:

    http://twitter.com/share?url=URLGOESHERE&text=PAGETITLEGOESHERE

    How do we get this widget to pull in the current pages URL and PAGETITLE of the page its been dropped onto? Id rather do this automatically vs having a content editor enter in the page URL and Page Title each time they drop the widget on a page.
  2. Pavel Benov
    Pavel Benov avatar
    341 posts
    Registered:
    14 Mar 2016
    10 Dec 2013
    Link to this post
    Hi,

    You can try with the following code:
    var manager = PageManager.GetManager();
               var node = SiteMapBase.GetActualCurrentNode();           
    var title = node.Title;
    var url = node.Url

    I hope this helps.

    Regards,
    Pavel Benov
    Telerik
    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
  3. kele
    kele avatar
    10 posts
    Registered:
    05 Nov 2013
    10 Dec 2013
    Link to this post
    Excellent thanks Pavel,

    Quick question: what is manager used for? i don't see it referenced anywhere else in the code you posted.
  4. Pavel Benov
    Pavel Benov avatar
    341 posts
    Registered:
    14 Mar 2016
    11 Dec 2013
    Link to this post
    Hi,

    For the specifics of the PageManager class you can take a look at the following documentation article:

    http://www.sitefinity.com/documentation/documentationarticles/developers-guide/sitefinity-essentials/pages

    As a whole managers in Sitefinity are the connection between your custom implementation and our Providers that associate with different data in the database. The managers in Sitefinity (there are different for each content type - NewsManager, EventsManager etc.) are basically classes that expose to the developers means to do CRUD operations with the system. You can take a look also at the Provider model that Sitefinity uses.

    Regards,
    Pavel Benov
    Telerik
    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
  5. Stephen2
    Stephen2 avatar
    94 posts
    Registered:
    05 Feb 2012
    14 Dec 2013
    Link to this post
    Couple of notes:
    1) In Pavel's code, he didn't need the PageManager, so you can drop that if you prefer
    2) I would prefer to use e.g., Page.Request.Url.AbsoluteUri instead of node.Url - handles querystring etc

  6. Stephen2
    Stephen2 avatar
    94 posts
    Registered:
    05 Feb 2012
    14 Dec 2013 in reply to Stephen2
    Link to this post
    Thinking about it you should use Page.Title instead of node.Title because:
    Some widgets in Sitefinity replace the Page.Title (e.g., on a News Item page)

    Be careful though, because this Title replacement is done after the Page_Load event for your UserControl

    I would suggest interrogating Page.Title during the Page_PreRender event, so your control would be something like (I just wrote this from my head, but be aware of the URL encoding I guess is important)...

    Page_PreRender(object sender, EventArgs e) {
      myLink.NavigateUrl = HttpServerUtility.UrlEncode(string.Format("http://twitter.com/share?url={0}&text={1}", Page.Request.Url.AbsoluteUri, Page.Title));
    }
  7. kele
    kele avatar
    10 posts
    Registered:
    05 Nov 2013
    14 Dec 2013
    Link to this post
    Thanks Stephen,

    Can this be put right into a master template as well? For instance to populate Facebook open graph meta tags?

    Kele.
  8. Stephen2
    Stephen2 avatar
    94 posts
    Registered:
    05 Feb 2012
    14 Dec 2013 in reply to kele
    Link to this post
    Yep no problem with that, either as a Widget/UserControl on a Template, or on a Master Page that is imported as a Template.
8 posts, 0 answered