Saturday 3 September 2011

Get all subsites Ecmascript\Javascript client object model sharepoint 2010


An example of how to get all the subsites in a site collection

<script type="text/javascript">

var currentcontext = null;
var currentweb = null;

ExecuteOrDelayUntilScriptLoaded(RetriveSites, "sp.js");

function RetriveSites()
{
currentcontext = new SP.ClientContext.get_current();

currentweb = currentcontext.get_web();

this.subsites = currentweb.get_webs();

currentcontext.load(this.subsites);

currentcontext.executeQueryAsync(Function.createDelegate(this, this.ExecuteOnSuccess),
Function.createDelegate(this, this.ExecuteOnFailure));

}

function ExecuteOnSuccess(sender, args) {
var subsites = '';

var enum = this.subsites.getEnumerator();

while (enum.moveNext())
{
var Site = enum.get_current();
subsites += '\nID: ' + Site.get_id() + '\nTitle: ' + Site.get_title();

}
alert(subsites);
}

function ExecuteOnFailure(sender, args) {
alert("error");
}
</script>

Ads by Google

1 comment:

  1. Hi!,

    I tested and works, but only for people that have atleast permissions of Contribute…why?, the visitors have the follow message:

    Access denied. You do not have permission to perform this action or access this resource.

    Could you helpme?

    ReplyDelete