I have a special requirement that I need to get all site collection URLs list from JQuery.Can any one please help on that
-
Should be asked on [sharepoint stackexchange](http://sharepoint.stackexchange.com/) – Mar 30 '12 at 10:29
2 Answers
I am afraid that you cannot get the list of all site collections in pure JavaScript. I assume that you meant all existing site collections in a web application or in all web applications.
If your page (aspx) runs on the SharePoint server you can put some server-side code on the page "rendering" the list as JavaScript and then just access it. This would be the easiest way; probably using Page.ClientScript.RegisterStartupScript.
If your page (and/or the web application) runs outside the SharePoint farm you would have to create and deploy your web service (asmx) or a handler (ashx) to the SharePoint farm. You would probably respond with the site collection list as JSON content and consume it by jQuery on your page (AJAX).
The functionality is available in the server-side SharePoint API only. You would use SPWebService.ContentService.WebApplications and application.Sites to get the list in any case:
var json = new StringBuilder();
foreach (var site in SPWebService.ContentService.WebApplications.SelectMany(
application => application.Sites))
using (site) {
if (json.Length > 0)
json.Append(',');
json.Append('"').Append(site.Url).Append('"');
}
json.Insert(0, "[").Append("]");
--- Ferda

- 5,281
- 34
- 35
-
I've just happened to find an [answer](http://stackoverflow.com/questions/1420128/accessing-all-sitecollections-on-a-sharepoint-server-with-webservice) that suggest the same for a scenario outside the SP farm but apparently outside the browser (no jQuery). – Ferdinand Prantl Mar 31 '12 at 14:00
Try below code.you can get all site collection using client object model
function loadWebs() {
var clientContext = new SP.ClientContext.get_current();
this.webs = clientContext.get_web().get_webs();
clientContext.load(this.webs);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onWebsLoaded), Function.createDelegate(this, this.onQueryFailed));
}
function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}
function onWebsLoaded(sender, args) {
for (var i = 0; i < this.webs.get_count(); i++) {
alert(this.webs.itemAt(i).get_title());
}
}
ExecuteOrDelayUntilScriptLoaded(loadWebs, "sp.js");

- 1,571
- 5
- 16
- 37
-
The above CSOM code that uses clientContext.get_web() gets a list of all SPWebs (subsites) under the current site collection - it does not return a list of all the site collections in the current web application. – Martin Cox Nov 11 '14 at 07:41