How to get current page title?

Posted by Community Admin on 03-Aug-2018 21:49

How to get current page title?

All Replies

Posted by Community Admin on 15-Mar-2011 00:00

How can I get the current pageNode and access its properties?

Posted by Community Admin on 15-Mar-2011 00:00

Hi Bruno,

Here's one way of doing it. I have this code in a UserControl that I drop onto my Master Template so that each page gets a Page title. Just so my clients (content editors) don't get confused when creating content and try to add a title themselves I have it setup so that when the page is in design/edit mode it displays a place holder message.

PageTitle.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PageTitle.ascx.cs" Inherits="theme_controls_content_PageTitle" %>
<asp:Literal ID="lblPageTitle" runat="server" />

PageTitle.ascx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
using Telerik.Sitefinity.Web;
using Telerik.Sitefinity.Modules.Pages;
 
public partial class theme_controls_content_PageTitle : System.Web.UI.UserControl
    protected void Page_Load(object sender, EventArgs e)
    
        if (!Page.IsPostBack)
        
            if (!this.IsDesignMode())
            
                string title = new PageManager().GetPageNode(new Guid(SiteMapBase.GetCurrentProvider().CurrentNode.Key)).Title;
                if (title != "")
                    lblPageTitle.Text = title;
            
            else
            
                lblPageTitle.Text = "Page Title automatically inserted.";
            
        
    

Hope that helps.

Cheers,
Phill

Posted by Community Admin on 16-Mar-2011 00:00

Hello ,

You should better use SiteMapBase.GetActualCurrentNode() and from there get a reference to the PageNode object.

Best wishes,
Ivan Dimitrov
the Telerik team

Posted by Community Admin on 16-Mar-2011 00:00

Good point Ivan, I had created that code during Beta/RC, before GetActualCurrentNode() existed. I also only ever use custom menus that fix the menu item Group bug by assigning a group item the same url as the first node so the code i provided always works as expected.

But yes, I would recommend changing the retrieval of the title to:

string title = SiteMapBase.GetActualCurrentNode().Title;

Regards,
Phill

Posted by Community Admin on 05-Apr-2011 00:00

Hi,

Is there a way for me to get the page Revision number from code?

Thanks,
Andrei

Posted by Community Admin on 05-Apr-2011 00:00

Hi Andrei,

Do you mean the page Version? If so there is a public property of the content object - Version that you can use.

Best wishes,
Ivan Dimitrov
the Telerik team


Posted by Community Admin on 05-Apr-2011 00:00

Ivan,

Yes, I meant the page version, sorry. I am just trying to find in the forums the line of code that
I need. I am still learning the API and so am unsure how to get to it.

I am looking for something like "string version = Page.CurrentPage.Version;" or something like that.

Many thanks,
Andrei

Posted by Community Admin on 05-Apr-2011 00:00

Hello Andrei,

The GetActualCurrentNode returns PageSiteNode. You can call GetPageNode of the page manager and get the version


            var m= PageManager.GetManager();
            var s = m.GetPageNode(PageSiteNode.Id);
            var version = s.Page.Version;

Best wishes,
Ivan Dimitrov
the Telerik team


Posted by Community Admin on 05-Apr-2011 00:00

Ivan,

That is wonderful, thanks. I will give it a go.

Many thanks,
Andrei

Posted by Community Admin on 06-Apr-2011 00:00

So, I have a user widget with public property with default value described in constructor.
This default value must be current page name.
Both

pageTitle = new PageManager().GetPageNode(new Guid(SiteMapBase.GetCurrentProvider().CurrentNode.Key)).Title
and
pageTitle = SiteMapBase.GetActualCurrentNode().Title
is working until I click "Edit" link for this widget.
Seems that Edit window asks my widget and pageTitle is being calculated again. But I see that SiteMapBase.GetActualCurrentNode() and SiteMapBase.GetCurrentProvider().CurrentNode are null. Probably because SiteMapBase.GetCurrentProvider() is of type  BackendSiteMap. How can I get SitefinitySiteMap in this case and will it help me to get current page title?

Posted by Community Admin on 10-Apr-2011 00:00

Hello Anatoliy Gerasin,

The actual page during page edit mode is not the page that you are editing. The SiteMap provider used in the front end is returning null because the page is not part of this provider. You can try using ths.Page.Title to get the title.

Best wishes,
Radoslav Georgiev
the Telerik team


Posted by Community Admin on 11-Apr-2011 00:00

And there is no way to get content page title in Edit modal window in any context, is there?

Posted by Community Admin on 14-Apr-2011 00:00

Hello Anatoliy Gerasin,

Can you please elaborate a little bit on the scenario. Are you editing the page in the page editor or you are using browse and edit?

Best wishes,
Radoslav Georgiev
the Telerik team


Posted by Community Admin on 14-Apr-2011 00:00

There is a case I tried just now:
- Create new custom widget "MyWidget".
- Add property "PageUrl" with default value calculated as described above (with node calculation).
- Add label "MyLabel" and fill it with PageUrl value.
- Create new page "test-page" with URL "/test-page" and go to editing this page.
- Drag and drop "MyWidget" in the edit window.
- I see that Label shows "test-page"
- Then I click "Edit" link on dropped "MyWidget"
- I see Edit window and see "MyLabel" equals empty string. But I would like to get "test-page" string in this case.

However I discovered that HttpContext.Current.Request.UrlReferrer = "/test-page/Action/Edit" in Edit window. Therefore I could check if I can get page url from CurrentNode - and if not - then parse UrlReferrer with regular expression.
This is a workaround but I confused to use it - is there more "civilized" solution?

Posted by Community Admin on 14-Apr-2011 00:00

Hello Anatoliy,

When you edit your widget you can call Context.Request.QueryString["PageId"]. This should give you the page ID,so you can pass it to the PageManager. You can add the code inside your control designer - InitializeControls

Regards,
Ivan Dimitrov
the Telerik team


Posted by Community Admin on 29-Apr-2011 00:00

hi Ivan, i'm doing as you suggested but i always get the wrong pageId, and always get this error "You are trying to access item that no longer exists. The most probable reason is that it has been deleted by another user."

string pageId = Context.Request.QueryString["PageId"];
 
            if (!String.IsNullOrEmpty(pageId))
            
                var pManager = Telerik.Sitefinity.Modules.Pages.PageManager.GetManager();
                PageNode page = pManager.GetPageNode(new Guid(pageId)); //exception happen here
            

i'm guessing it returning the edit mode page not the actual page. any input how to solve this?

Posted by Community Admin on 29-Apr-2011 00:00

Hi Anatoliy,

Use GetPageDraft because this is the draft version of a page. Then you can get the PageData from ParentPage property.

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

Posted by Community Admin on 09-May-2012 00:00

Hi Ivan,

I have a question regarding GetActualCurrentNode(). I have a blog on my site which uses a specific detail page to show the individual entries.  If I were to use this method on that template page, would it return a PageSiteNode that describes the post page (this is what I assume will happen), or will it describe the blog post as though it were its own page. What I mean is if I check the Id of the returned PageSiteNode, will it be the id of the blog post page or the id of the individual blog post?

I am currently migrating to Sitefinity 5 and am updating old 3.X code. I need a way to, from the blog post detail page, get the id of the specific blog post the user has navigated to.

Thank you!

Posted by Community Admin on 09-May-2012 00:00

Gah, I got a server error then pressed back and resubmitted and it double posted. Apologies, you can delete this one.

Thanks

Posted by Community Admin on 15-May-2012 00:00

For anyone who might also be wondering how to get the current BlogPost object from a dedicated blog post detail page, here is the code straight from Telerik:

string urlParams = this.GetUrlParameterString(true);
if (urlParams != null)
    BlogsManager blogsManager = BlogsManager.GetManager();
    string redirectUrl;
    var item = blogsManager.GetItemFromUrl(typeof(BlogPost), urlParams, true, out redirectUrl);

Thanks

This thread is closed