5

In alert I am getting [object object], I need to get names of all sub sites.

<script>
ExecuteOrDelayUntilScriptLoaded(RetriveSites,"sp.js");

function RetriveSites()
{
$.ajax({
        url: "https://hp.sharepoint.com/teams/VeraxRep/_api/web/webs",
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
        },
        success: function (data) {
alert(data.d.results);    // This gets alert of [object object]
 var jsonObject = JSON.parse(data.body);
    var results = jsonObject.d.results;

    alert(results);
        },
        error: function (error) {
            alert(JSON.stringify(error));
        }
    });
}
</script>
  • Better to use Console.log instead of alert. Then in the console you can see the Json details. – Unnie Nov 10 '15 at 8:39
7

data.d.results is an Array of object (Sub-Site). alert can print message/string only in the right format.

If you need to see them as string, then use

JSON.stringify(data.d.results)

You do not need to use following code

var jsonObject = JSON.parse(data.body);
var results = jsonObject.d.results;

Try some thing like following

$.ajax({
    url: "https://hp.sharepoint.com/teams/VeraxRep/_api/web/webs?$select=Title,Url,ServerRelativeUrl",
    type: "GET",
    headers: {
        "accept": "application/json;odata=verbose",
    },
    success: function(data) {
        var subSiteNames = [];
        $.each(data.d.results, function(index, item) {
            subSiteNames.push(item.Title);
        });
        console.log(subSiteNames);
    },
    error: function(error) {
        alert(JSON.stringify(error));
    }
});

subSiteNames is the Array that contains all sub-site's name. If you need them in a single string, the use

alert(subSiteNames.join(", "));

Based on the comment. You need the sub-site URL.

For Server Relative Url

item.ServerRelativeUrl

For absolute Url

item.Url
  • Thanks alot...what should I do to get the subsite names as links.. – KumarV Nov 10 '15 at 6:40
  • use item.ServerRelativeUrl instead of item.Title – Atish Dipongkor - MVP Nov 10 '15 at 6:46
  • This requires user to have Browse Directories permission. And in that case it returns all subwebs regardless of user permissions to that subweb. Must use effectivebasepermissions filter as in answer below. – Jussi Palo May 10 '17 at 6:25
3

Use the code given below, it will return the sub site name and url and save it in an arraylist named SubsiteDetails.

 $.ajax({
    url: siteUrl + "/_api/web/webs/?$select=title,ServerRelativeUrl,Created,effectivebasepermissions&$filter=(effectivebasepermissions/high%20gt%2032)&$orderby=Created desc",
    method: "GET",
    headers: {
        "Accept": "application/json; odata=verbose"
    },
    success: function(data) {
        var tempArray = data.d.results;
        $.each(data.d.results, function(index, item) {
            var tempArray = new Array(2);
            tempArray[0] = item.Title;
            tempArray[1] = item.ServerRelativeUrl;
            SubsiteDetails.push(tempArray);
        });
    },
    error: function(data) {
        //alert(JSON.stringify(data));
    }
});
1
$.ajax({
url: "http://<site url>/_api/web/webs",
type: "GET",
headers: { "accept": "application/json;odata=verbose" },
success: function (data) {
        console.log(data.d.results);
    },
error: function (error) {
        alert(JSON.stringify(error));
    }
});

Above code will return array of objects. Count of objects will be count of subSite. Each object will contain properties of each SubSite.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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