locked
How do i get current page (aspx) title dynamically? RRS feed

  • Question

  • I have an aspx page /Lists/projectlist/College%20Education.aspx. I want to display the page name in this example it should display "College Education".

    how do I go about doing that? I've seen examples about doing this using SPContext.Current.List.Title .... do i have to build a customeWebPart or is there an easier way?


    Thanks


    nothing for members to see

    Wednesday, February 22, 2012 5:10 PM

Answers

  • Do you want the name of the list or the name of the page?  The URL you gave appears to be an ASPX page that resides within a list (either a view or a custom page).  If you want the name of the list, and you're using C#, then the code you seems correct.  If it's the name of the page, then you need to access the URL.

    If you're using javascript, and you just want the page title to be set dynamically to the name of the page, here is the code:

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
    <script type="text/javascript">
    
    $(function(){
    	var pageName = $("form").attr("action").replace(".aspx", "");
    	$("td.ms-pagetitle").text(pageName);
    });
    
    </script>

    • Marked as answer by Wayne Fan Friday, March 2, 2012 9:39 AM
    Wednesday, February 22, 2012 6:59 PM
  • hello

    if you have publishing page then the regular way to display page title is to use FieldValue control. You need to add it to the page layout of your publishing page:

    <SharePointWebControls:FieldValue id="PageTitle" FieldName="Title" runat="server"/>
    However in your case seems like you are not talking about publishing page. Publishing pages are located in "Pages" document library in most cases. But in your example aspx page is located in the list. I can assume that this is custom list form. If it is correct you may edit it in Sharepoint Designer and add title manually.

    Blog - http://sadomovalex.blogspot.com
    CAML via C# - http://camlex.codeplex.com

    • Proposed as answer by Muawiyah Shannak Thursday, February 23, 2012 9:49 AM
    • Marked as answer by Wayne Fan Friday, March 2, 2012 9:39 AM
    Wednesday, February 22, 2012 7:12 PM
  • You can add it to a CEWP or use SharePoint Designer.  Since there is a dependency on the location of the CEWP in the page, I usually place the code in a separate script file and reference it from within the page or master page using Designer.


    • Edited by buckenn Wednesday, February 22, 2012 8:05 PM clarify
    • Marked as answer by Wayne Fan Friday, March 2, 2012 9:39 AM
    Wednesday, February 22, 2012 8:04 PM

All replies

  • Hi,

    As per my understanding you have a list named, projectlist.. and the aspx page you are saying about is not clear to me.Is this a custom page and how you stored it in a list, as an attachment or what? please elaborate in detail.

    Thanks

    Wednesday, February 22, 2012 6:05 PM
  • Do you want the name of the list or the name of the page?  The URL you gave appears to be an ASPX page that resides within a list (either a view or a custom page).  If you want the name of the list, and you're using C#, then the code you seems correct.  If it's the name of the page, then you need to access the URL.

    If you're using javascript, and you just want the page title to be set dynamically to the name of the page, here is the code:

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
    <script type="text/javascript">
    
    $(function(){
    	var pageName = $("form").attr("action").replace(".aspx", "");
    	$("td.ms-pagetitle").text(pageName);
    });
    
    </script>

    • Marked as answer by Wayne Fan Friday, March 2, 2012 9:39 AM
    Wednesday, February 22, 2012 6:59 PM
  • hello

    if you have publishing page then the regular way to display page title is to use FieldValue control. You need to add it to the page layout of your publishing page:

    <SharePointWebControls:FieldValue id="PageTitle" FieldName="Title" runat="server"/>
    However in your case seems like you are not talking about publishing page. Publishing pages are located in "Pages" document library in most cases. But in your example aspx page is located in the list. I can assume that this is custom list form. If it is correct you may edit it in Sharepoint Designer and add title manually.

    Blog - http://sadomovalex.blogspot.com
    CAML via C# - http://camlex.codeplex.com

    • Proposed as answer by Muawiyah Shannak Thursday, February 23, 2012 9:49 AM
    • Marked as answer by Wayne Fan Friday, March 2, 2012 9:39 AM
    Wednesday, February 22, 2012 7:12 PM
  • buckenn...thanks for the reply. yes, the ASPX page is a view that resides within a list and I want to grab the name of the page. Sorry I wasn't very clear but you got the idea correctly.

    Since the code above is a Javascript, could I just add this code into a Content Editor WebPart?

    Thanks


    nothing for members to see

    Wednesday, February 22, 2012 7:31 PM
  • You can add it to a CEWP or use SharePoint Designer.  Since there is a dependency on the location of the CEWP in the page, I usually place the code in a separate script file and reference it from within the page or master page using Designer.


    • Edited by buckenn Wednesday, February 22, 2012 8:05 PM clarify
    • Marked as answer by Wayne Fan Friday, March 2, 2012 9:39 AM
    Wednesday, February 22, 2012 8:04 PM