7

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());
    }

})();
1
  • 1
    Not possible. If you need to use JS I would suggest using the Search Rest API Commented Oct 15, 2014 at 1:56

3 Answers 3

1

Not sure what you are trying to do here, since you have not actually opened a web. Maybe you have that code elsewhere.

Try this out for loading a context:

var oWeb;
var clientContext;
var oList;

.
.
.
clientContext = new SP.ClientContext();
oWeb = clientcontext.get_web();
clientContext.load(oWeb);
//leverage the oWeb to call your onGetWebSuccess()...

//rest of this is for others who need to do an actual query
oList = oWeb.get_lists().getByTitle("Items");
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml("<View></View>");  //add your CAML here...you don't need it tho
var collItems = oList.getItems(camlQuery);
clientContext.load(collItems)
1

If I understand your question correctly, you could have a look at SP.Web.getSubwebsForCurrentUser Method (sp.js). It will return a collection of child sites of the current site based on the specified query.

For more detail, have a look on this MSDN reference.

2
  • 1
    This only shows subwebs for a given site collection. The question involves getting all site collections and sites Commented Aug 6, 2015 at 8:42
  • True. I missed the all site collections part and I probably should have mentioned that it would take some futher development, like an recursive method to retrieve all the subwebs of the entire site collection. Commented Aug 6, 2015 at 9:00
0

If I got your requirement correctly, then following solution should accomplish your goal.

Spin throw site: Just make a GET request to the following URL. It will provide you all sub-sites under it.

/_api/web/webs?$select=Url

Response will be

{
  "d": {
    "results": [
      {
        "__metadata": {
          "id": "https://yoursite.sharepoint.com/Actionspace/_api/Web",
          "uri": "https://yoursite.sharepoint.com/Actionspace/_api/Web",
          "type": "SP.Web"
        },
        "Url": "https://yoursite.sharepoint.com/Actionspace"
      },
      {
        "__metadata": {
          "id": "https://yoursite.sharepoint.com/blogTest/_api/Web",
          "uri": "https://yoursite.sharepoint.com/blogTest/_api/Web",
          "type": "SP.Web"
        },
        "Url": "https://yoursite.sharepoint.com/blogTest"
      }
    ]
  }
}

Now loop through the d.results and make a GET request to following URL. It will provide you the currents user's Groups. From Groups, you will be able to see which permission this user has in this site.

/_api/web/CurrentUser?$expand=groups

Response will be

{
  "d": {
    "__metadata": {
      "id": "https://yoursite.sharepoint.com/_api/Web/GetUserById(11)",
      "uri": "https://yoursite.sharepoint.com/_api/Web/GetUserById(11)",
      "type": "SP.User"
    },
    "Groups": {
      "results": [
        {
          "__metadata": {
            "id": "https://yoursite.sharepoint.com/_api/Web/SiteGroups/GetById(16)",
            "uri": "https://yoursite.sharepoint.com/_api/Web/SiteGroups/GetById(16)",
            "type": "SP.Group"
          },
          "Title": "Approvers",
          "Description": "Members of this group can edit and approve pages, list items, and documents."
        },
        {
          "__metadata": {
            "id": "https://yoursite.sharepoint.com/_api/Web/SiteGroups/GetById(14)",
            "uri": "https://yoursite.sharepoint.com/_api/Web/SiteGroups/GetById(14)",
            "type": "SP.Group"
          },
          "Title": "Designers",
          "Description": "Members of this group can edit lists, document libraries, and pages in the site. Designers can create Master Pages and Page Layouts in the Master Page Gallery and can change the behavior and appearance of each site in the site collection by using master pages and CSS files.  "
        },
        {
          "__metadata": {
            "id": "https://yoursite.sharepoint.com/_api/Web/SiteGroups/GetById(15)",
            "uri": "https://yoursite.sharepoint.com/_api/Web/SiteGroups/GetById(15)",
            "type": "SP.Group"
          },
          "Title": "Hierarchy Managers",
          "Description": "Members of this group can create sites, lists, list items, and documents. "
        },
        {
          "__metadata": {
            "id": "https://yoursite.sharepoint.com/_api/Web/SiteGroups/GetById(18)",
            "uri": "https://yoursite.sharepoint.com/_api/Web/SiteGroups/GetById(18)",
            "type": "SP.Group"
          },
          "Title": "Quick Deploy Users",
          "Description": "Members of this group can schedule Quick Deploy jobs."
        },
        {
          "__metadata": {
            "id": "https://yoursite.sharepoint.com/_api/Web/SiteGroups/GetById(17)",
            "uri": "https://yoursite.sharepoint.com/_api/Web/SiteGroups/GetById(17)",
            "type": "SP.Group"
          },
          "Title": "Restricted Readers",
          "Description": "Members of this group can view pages and documents, but cannot view historical versions or review user rights information."
        },
        {
          "__metadata": {
            "id": "https://yoursite.sharepoint.com/_api/Web/SiteGroups/GetById(13)",
            "uri": "https://yoursite.sharepoint.com/_api/Web/SiteGroups/GetById(13)",
            "type": "SP.Group"
          },
          "Title": "Style Resource Readers",
          "Description": "Use this group to give read permissions to the Master Page gallery and Style Library, which are required to browse this site. Do not delete this group."
        }
      ]
    }
  }
}

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.