I have the URL of a web (http://intranet/sites/site/web) and I would like to get it's title. I know in PowerShell or C# you could create a Web object from the URL and get the title. Can you do something similar with JavaScript and if so how?
3 Answers
Yes, you can use (SP.js) to Get Web Site Properties like
- Title (
Web.get_title();
). - Description (
Web.get_description();
). - Language (
Web.get_language();
). - UI Version (
Web.get_uiVersion();
). - Created (
Web.get_created();
).
To get Page Title
, Just add the below code in Script Editor Web Part
to your page
[Code]
<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("http://epm/workflow");
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>
Just replace this line
var clientContext = new SP.ClientContext("http://epm/workflow");
with your site URL
clientContext = new SP.ClientContext("http://intranet/sites/site/web");
Note: you can get the current site URL as the following:
var clientContext = new SP.ClientContext(_spPageContextInfo.siteServerRelativeUrl);
Check also Ref: Get Web Site Properties
You can use REST API to get web Title. Assuming your web is at url http://intranet/sites/site/myweb put below link in browser you get be able to see json object.
http://intranet/sites/site/myweb/_api/Web
Json Response.
{
......//all other properties.
"Title": "test",
"TreeViewEnabled": false,
"UIVersion": 15,
"UIVersionConfigurationEnabled": false,
"Url": "https://fcatcs.sharepoint.com/sites/dev/test",
"WebTemplate": "STS"
}
SP 2016 OnPrem:
var siteTitle = _spPageContextInfo.webTitle; // returns name of site