2

I need to get a subweb with a particular title using JSOM. So far, the only way I can see to do this would be to use code similar to the following:

function getStuff() {
    var oWebsite = clientContext.get_web();
    collWebs = oWebsite.get_webs();
    clientContext.load(collWebs, 'Include(Title, Url)');

    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded), 
        Function.createDelegate(this, this.onQueryFailed)
    );
}

function onQuerySucceeded() {
    var webInfo = '';
    var websEnumerator = collWebs.getEnumerator();

    while (websEnumerator.moveNext()) {
        var oWeb = websEnumerator.get_current();
        webInfo += 'Title: ' + oWeb.get_title() + ' Url: ' + 
            oWeb.get_url() + '\n';
    }
}

where I'm forced to go through the entire collection of webs using the enumerator. Is there any way to get a particular web, similar to the way you can do the following on a List Collection?

get_lists().getByTitle("MyCustomList");
1
  • 1
    The search answer is a good one since there is no getByTitle equivalent for get_webs(), but I would imagine you probably aren't taking that much of a performance hit by just enumerating the webs to find the right one since you're already only retrieving the title and url... that is unless you have TONS of them. In fact I would think making a call to the search service will actually be slower than 'manually' enumerating the webs -- but it would be worth performing some benchmarking on
    – John-M
    Commented Jan 22, 2015 at 20:21

2 Answers 2

5

You can leverage SP 2013 Search API s for this if you have a search configured and continous crawling enabled and there is huge number of sub sites to be iterated otherwise.

//Raw code not tested , just to give idea of what it will look like
function getSite(){
var clientContext = new SP.ClientContext("<serverRelativeUrl>");
var contextSite = clientContext.get_site();
var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(clientContext); 
keywordQuery.set_queryText("contentClass:STS_Web AND TitleofWeb"); //refine the query to your need
var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(clientContext); 
results = searchExecutor.executeQuery(keywordQuery); 
context.executeQueryAsync(onQuerySuccess, onQueryError);
}


function onQuerySuccess(sender, args) {
$.each(results.m_value.ResultTables[0].ResultRows, function () {
console.log(this.Path);//URL of the site
});

See this blog on how to use Search APIs in JSOM.

5
  • I am getting Uncaught TypeError: Cannot read property 'Search' of undefined. in sharepoint online. Commented Sep 2, 2016 at 7:13
  • Did you load Sp.Search.js in the page. Make sure all relevant js files are loaded
    – Unnie
    Commented Sep 2, 2016 at 7:15
  • Ok. Now i added Sp.Search.js reference but now getting Uncaught TypeError: this.get_objectData is not a function. Commented Sep 2, 2016 at 7:22
  • You should post a new question with your code.
    – Unnie
    Commented Sep 2, 2016 at 7:26
  • Thank you so much Unnie....Its working now....I forgot to give SP.RequestExecutor.js ref. Thanks again. Commented Sep 2, 2016 at 7:28
1

If you aren't restricted from using the REST API, you could just use the $filter command to get the particular web URL you're looking for... this would result in one of the lightest weight network requests possible given the scenario.

Try pasting this into your browser dev tools on your SharePoint site, it will log the Url into the console (you'll need jQuery of course and remember to replace yourWebTitle as appropriate):

$.getJSON("/_api/web/webs?$select=Title,Url&$filter=Title eq 'yourWebTitle'",
            function(data) { console.log(data.value[0].Url) });

Noting the caveat that you asked for JSOM and I'm supplying a REST answer, this is a plausible alternative for most use cases.

Actually, you don't even need to retrieve the Title to filter by it...

$.getJSON("/_api/web/webs?$select=Url&$filter=Title eq 'yourWebTitle'",
            function(data) { console.log(data.value[0].Url) });

Should be about as good as you can do!

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.