How to get current page title?
How can I get the current pageNode and access its properties?
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"
/>
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.";
Hello ,
You should better use SiteMapBase.GetActualCurrentNode() and from there get a reference to the PageNode object.
Best wishes,
Ivan Dimitrov
the Telerik team
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;
Hi,
Is there a way for me to get the page Revision number from code?
Thanks,
Andrei
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
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
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
Ivan,
That is wonderful, thanks. I will give it a go.
Many thanks,
Andrei
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
pageTitle =
SiteMapBase.GetActualCurrentNode().Title
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?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
And there is no way to get content page title in Edit modal window in any context, is there?
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
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?
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
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
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
Hi Ivan,
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);