0

If I have a page down in a few nested subsites how can I get the title of the parent subsite via JS? Will I have to use the ClientContext object or is there something easier?

Parent --
  Subsite 1 --
     Subsite 2 -- //<I'd like to get the title of Subsite 1

2 Answers 2

0

Try the following code :

var context = new SP.ClientContext.get_current();        
var web = context.get_web();                       
var parentInfo = web.get_parentWeb();  
var parentWeb = context.get_site().openWebById(parentInfo.get_id());

context.load(parentInfo);
context.load(parentWeb);

context.executeQueryAsync(Function.createDelegate(this,onListDataSucceeded),Function.createDelegate(this, onListDataFailed)); 

function onListDataSucceeded(sender, args)
{
    alert(this.parentWeb.get_title());
}

function onListDataFailed(sender, args)
{
    alert('error');
}
0

You can use javascript object model or rest api to get parent web properties. If using REST, call this url _api/web/parentweb

 var currWebAbsUrl = _spPageContextInfo.webAbsoluteUrl;
        $.ajax({
            url: currWebAbsUrl + "/_api/web/parentweb",      
            method: "GET",
            headers: {
                "Accept": "application/json; odata=verbose"
            },
            success: function (data) {
                var parentWebTitle = data.d.Title;
                window.console && console.log(parentWebTitle);
            },
            error: function (err) {
                window.console && console.log('Error: ' + JSON.stringify(err));
            }
        });      

See MSDN article: https://msdn.microsoft.com/en-us/library/office/jj246539.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.