Pages Menu
TwitterRssFacebook

Posted by on May 15, 2015 in Learn Skype Web SDK

Learn Skype Web SDK Day 12 : Getting Users in Groups

Learn Skype Web SDK Day 12 : Getting Users in Groups

[contemplate-1]
View Demo

In the last few lessons we’ve looked at how to get lists of contacts and groups. However, sometimes it’s the structuring of contacts within groups which is important, and which you want to display in your application.

Once you have retrieved a particular group or groups, you can then query that group for its members, using the property persons. This collection isn’t immediately filled however (for performance reasons) so you can subscribe items being added to it:


group.persons.added(function (person) {
//do something with the person object
});

It’s worth nothing that if you do this for all groups you may get the same person object returned multiple times – one for each group that the user is in.

In the code sample below, all groups and their users are collated and displayed on the screen. Notice how relatively slow this action is compared to some of the other functions in the Skype Web SDK. This is why the SDK is structured to only load the data you need when you ask for it.


 <div class="form-horizontal">
      <div class="form-group">
        <label for="username" class="col-sm-2 control-label">Username</label>
        <div class="col-sm-10">
          <input type="email" class="form-control" id="username" placeholder="Email">
        </div>
      </div>
      <div class="form-group">
        <label for="password" class="col-sm-2 control-label">Password</label>
        <div class="col-sm-10">
          <input type="password" class="form-control" id="password" placeholder="Password">
        </div>
      </div>
    </div>

    <div class="form-horizontal">
      <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
          <button class="btn btn-default" id="btnLogIn">Log in</button>
        </div>
      </div>

      <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
          <button class="btn btn-default" id="btnLogOut">Log out</button>
        </div>
      </div>
    </div>

    <div>
      <span id="loginStatus"></span><hr/>

    
    
   
  </div>

  Group List:
  <ul id="lstGroupList"></ul>


  <div id="footer"></div>
  
  <!-- This is not needed for the samples to run, but adds standard headers and footers to the samples, to display title, instructions, about etc.
       If you're taking this code and using it yourself, you can remove this.-->
   <script type="text/javascript" src="../../assets/layoutcodesample-min.js"></script>
  
  <script type="text/javascript">

  <!-- These variables are only needed for laying out the code sample, they are not part of the sample code. -->
  var pageTitle = "Getting Users in Groups";
  var blogPostLocation = "http://thoughtstuff.co.uk";
  var githubLocation = "http://github.com";



 var client;
  $(function () {
    'use strict';

	Skype.initialize({
            apiKey: 'SWX-BUILD-SDK',
        }, function (api) {
            client = new api.application();           
			
		// whenever client.state changes, display its value
		client.signInManager.state.changed(function (state) {
		$('#loginStatus').text("Login State: " + state);
		});
	
        }, function (err) {
            alert('Error loading Skype Web SDK: ' + err);
        }); 

    $('#btnLogIn').click(function () {

        // start signing in
        client.signInManager.signIn({
          username: $('#username').val(),
          password: $('#password').val()
        }).then(function () {
            //log in worked!
            alert('Logged in!');
            GetGroupsWithUsers();   
            
          }, function (error) {
            //Something went wrong.
            alert(error);
          });
      });

    $('#btnLogOut').click(function () {
        // start signing out
        client.signInManager.signOut()
        .then(function () {
               //log out worked!
               alert('Logged out!');
               $('#btnSearch').prop('disabled', true);
             }, function (error) {
                //Something went wrong.
                alert(error);
              });
      });

    

    function GetGroupsWithUsers()
    {
            client.personsAndGroupsManager.all.groups.added(function (group) {
        group.name.get().then(function (theGroupName) {
		
			group.persons.added(function (person) {	
			person.displayName.get().then(function (theDisplayName) {
				$('#lstGroupList').append('<li><b>Group: ' + theGroupName + '</b> : ' + theDisplayName + '</li');
				});
			
			});	  
			
		  
        });

      });

       client.personsAndGroupsManager.all.groups.subscribe();          
       
        }

      });

        </script>

Demo Online

You can try this code out against your own Skype for Business environment by going to the demo page. From here you can also download the code from GitHub if you want to host it locally, or take it and use it in your next project.
[contemplate-2]

Written by Tom Morgan

Tom is a Microsoft Teams Platform developer and Microsoft MVP who has been blogging for over a decade. Find out more.
Buy the book: Building and Developing Apps & Bots for Microsoft Teams. Now available to purchase online with free updates.

Post a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.