Search code examples
javascriptsharepoint-2013

Is there a way of getting the current page's title on a SharePoint Page


I am using SharePoint 2013 on-prem. I am attempting to use the Title attribute (column) from Site Pages (list) as the pageTitle for the corresponding page. I know that I could hard-code going through the Site Pages list for the title, but I would like to use some property on the page or a direct call to get the page's title and replace the pageTitle tag.


Solution

  • I understand you want to display the Site Pages Title column in the corresponding page, then you can use Rest API to get it and display in the page:

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    <script type="text/javascript">
            $.ajax({
                       url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Site Pages')/items?$filter=Id eq '"+_spPageContextInfo.pageItemId+"' ",
                       method: "GET",
                       headers: { "Accept": "application/json; odata=verbose" },
                       success: function (data) {
                                $("#PageTitle").html(data.d.results[0].Title);
    
                      },
                      error: function (data) {
                          alert("Error: "+ data);
                     }
              });
    </script>
    <div id="PageTitle"></div>
    

    enter image description here

    This is the return Json format for the page item: enter image description here