I was looking at the JSOM documentation, but couldn't find a nice way to spin through all site collection and sites to show a user what they have read access to. Is this possible using JSOM and a SharePoint hosted app?
Here is the code:
I tried to pass a client context with the URL but it doesn't seem to return anything.
'use strict';
var context = SP.ClientContext.get_current();
var user = context.get_web().get_currentUser();
var web = context.get_web().get_webs();
(function () {
// This code runs when the DOM is ready and creates a context object which is
// needed to use the SharePoint object model
$(document).ready(function () {
getUserName();
getWebSite();
});
// This function prepares, loads, and then executes a SharePoint query to get
// the current users information
function getUserName() {
context.load(user);
context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
}
function getWebSite() {
context.load(web);
context.executeQueryAsync(onGetWebSuccess,onGetWebFail);
}
// This function is executed if the above call is successful
// It replaces the contents of the 'message' element with the user name
function onGetUserNameSuccess() {
$('#message').text('Hello ' + user.get_title().split(',')[1] + '!');
}
// This function is executed if the above call fails
function onGetUserNameFail(sender, args) {
alert('Failed to get user name. Error:' + args.get_message());
}
function onGetWebSuccess(){
//for (var i = 0; i < web.get_count(); i++) {
// alert(web.itemAt(i).get_title());
//}
var div = $('#listofsites');
$('#listofsites').html('<p>Webs loaded!</p>');
$('#listofsites').append('<p>Count: ' + web.get_count() + '</p>');
div.append('<p>' + web.get_areItemsAvailable() + '</p>');
for (var i = 0; i < web.get_count(); i++){
div.append('<p>Site Title: ' + web.itemAt(i).get_title() + '</p>');
}
}
function onGetWebFail(sender, args) {
alert('Failed to get web. Error:' + args.get_message());
}
})();