0

I tried almost everything. A script count how many users are in group. I must find out how open this script on page load and upgrade reload. here is this script:

 <script>
var userCollection;

var clientContext = new SP.ClientContext();
//Get all groups in site
var groupCollection = clientContext.get_web().get_siteGroups();
// Get the group by name
group = groupCollection.getByName('Sharepoint Group');
//Get all SP Users in SP Group
 userCollection = group.get_users();
clientContext.load(userCollection);  
//Execute Query
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded1), Function.createDelegate(this, this.onQueryFailed)); 


function onQuerySucceeded1() {
    alert(userCollection.get_count());
    document.getElementById("count").innerHTML=userCollection.get_count();
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}


</script>
2
  • Its work when I edit page and don't work when I reload this Commented Jan 30, 2015 at 21:22
  • Are you using the ScriptEditor Web Part? If yes, when the script does not work, does the URL contain "_layouts/15/start.aspx#"? Commented Jan 31, 2015 at 0:00

1 Answer 1

1

It may be trying to execute before the SharePoint client object model JavaScript library (aka "SP.js") has finished loading. This would cause your code to fail when it reaches "SP.ClientContext()" because "SP" would be undefined.

Try wrapping it up in a call to ExecuteOrDelayUntilScriptLoaded.

Example:

<script>
ExecuteOrDelayUntilScriptLoaded(function(){
    var userCollection;
    var clientContext = new SP.ClientContext();
    //Get all groups in site
    var groupCollection = clientContext.get_web().get_siteGroups();
    // Get the group by name
    group = groupCollection.getByName('Sharepoint Group');
    //Get all SP Users in SP Group
     userCollection = group.get_users();
    clientContext.load(userCollection);  
    //Execute Query
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded1), Function.createDelegate(this, this.onQueryFailed)); 

    function onQuerySucceeded1() {
        alert(userCollection.get_count());
        document.getElementById("count").innerHTML=userCollection.get_count();
    }

    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
},"SP.JS");
</script>
5
  • It don't work too Commented Jan 30, 2015 at 22:23
  • :(Can You try at Your Sharepoint please ? Commented Jan 30, 2015 at 22:24
  • What version of SharePoint do you have (2010 or 2013)? Also, how are you adding the JavaScript to your page? Commented Jan 30, 2015 at 22:33
  • Yes i, m add ING to my page and I use Sharepoint 2013. Commented Jan 30, 2015 at 22:36
  • Try using F12 developer tools and debugging your JavaScript to see what the error might be. Commented Jan 30, 2015 at 22:43

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.