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>
  • Its work when I edit page and don't work when I reload this – Grzegorz Z Jan 30 '15 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#"? – Rob Windsor Jan 31 '15 at 0:00
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>
  • It don't work too – Grzegorz Z Jan 30 '15 at 22:23
  • :(Can You try at Your Sharepoint please ? – Grzegorz Z Jan 30 '15 at 22:24
  • What version of SharePoint do you have (2010 or 2013)? Also, how are you adding the JavaScript to your page? – Thriggle Jan 30 '15 at 22:33
  • Yes i, m add ING to my page and I use Sharepoint 2013. – Grzegorz Z Jan 30 '15 at 22:36
  • Try using F12 developer tools and debugging your JavaScript to see what the error might be. – Thriggle Jan 30 '15 at 22:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.