Pages Menu
TwitterRssFacebook

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

Learn Skype Web SDK Day 12 : Getting Users in Groups

Learn Skype Web SDK Day 12 : Getting Users in Groups

This is one post in a series of more than 30, devoted to helping you get up to speed quickly with the new Skype Web SDK. Each lesson comes with source code (in GitHub) and a working demo page so you can see what it does. Check out the full list of posts on the Skype Web SDK page.


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.

Disclaimer: This is sample code, intended to inform and educate. It is not production-ready and is lacking key components such as error handling. You use it entirely at your own risk. You should fully understand the effects, limitations and risks of the code before executing, and understand the implications of any set-up steps. By using these code examples you are using the Skype Web SDK, so you should read the Skype Software License Terms to which you are agreeing.

Good to Know

Post a Reply

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

This site uses cookies to help make this website better. By continuing to use this site we’ll assume you’re OK with that (implied consent).