How to get SharePoint Page Title and URL Programmatically

In this hint, I will show How to get SharePoint Page Title and URL Programmatically?


Get SharePoint Page Title and URL Programmatically

Get the SharePoint Page Title in C#

using Microsoft.SharePoint;
void getSPTitle()
{
// to get the sharepoint page title
String Title= SPContext.Current.Item["Title"].ToString(); 
}

Get the SharePoint Page URL in C#

using Microsoft.SharePoint;
void getSPURL()
{
// to get the sharepoint page URL
String URL=System.Web.HttpContext.Current.Request.Path;
}

Get the SharePoint Page URL using “File.Url” in C#

using Microsoft.SharePoint;
void getSPURL()
{
// to get the sharepoint page URL
SPContext.Current.Web.Url + "/" + SPContext.Current.File.Url;
}

Get SharePoint Page Title and Description using JavaScript Object Model

How to get SharePoint Page Title and URL Programmatically
<script src ="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"> </script>
<script type = "text/javascript">

    $(document).ready(function() {
        // Wait until SP.JS has loaded before calling getWebUserData 
        ExecuteOrDelayUntilScriptLoaded(retrieveWebSite, "sp.js");
    });

function retrieveWebSite() {
    var clientContext = new SP.ClientContext(_spPageContextInfo.siteServerRelativeUrl);
    this.oWebsite = clientContext.get_web();
    clientContext.load(this.oWebsite);
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded),
        Function.createDelegate(this, this.onQueryFailed)
    );
}

function onQuerySucceeded(sender, args) {
    alert('Title: ' + this.oWebsite.get_title() +
        ' Description: ' + this.oWebsite.get_description());
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() +
        '\n' + args.get_stackTrace());
} 
</script>

You might also like to check SharePoint 2016: Get Current User Using JavaScript


Conclusion

In conclusion, we have provided code samples to get SharePoint Page Title and URL Programmatically using JS and C#

Applies To
  • SharePoint 2016.
  • SharePoint 2013 / 2010.
  • C# SSOM.
  • JSOM.
You might also like to read
Have a Question?

If you have any related questions, please don’t hesitate to Ask it at deBUG.to Community.

8 thoughts on “How to get SharePoint Page Title and URL Programmatically”

  1. Pingback: Get SharePoint Choice Field Value In C# | SPGeeks

Leave a Reply