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

  • Question

  •  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.

    I want to get this value by Jquery or JavaScript, can anyone help me please ?

    Tuesday, December 3, 2019 5:20 AM

Answers

  • Hi developer sp,

    Add the Jquery ajax code in Script Editor Web Part within 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>

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by developer sp Wednesday, December 4, 2019 10:49 AM
    Tuesday, December 3, 2019 7:52 AM

All replies

  • Hi developer sp,

    Add the Jquery ajax code in Script Editor Web Part within 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>

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by developer sp Wednesday, December 4, 2019 10:49 AM
    Tuesday, December 3, 2019 7:52 AM
  • Hi Jerry,

    Thanks for always helping me, this is working good :)

    Wednesday, December 4, 2019 10:49 AM