Displaying a User’s Lync Status (Using nameCtrl)

posted in: Uncategorized | 14

Often times in SharePoint and other Microsoft integrated applications, you will see a user’s name with a little box infront of it indicating their Lync status. If you move your mouse over their name, often times it will show a Lync contact card with options to email them or send a Lync message. This is possible thanks to a Office plugin that is installed with Lync that you can access via JavaScript, which works in IE, Chrome, and Firefox.

Basic Setup

Before you do anything, you need to initialize the nameCtrl object. This is done using an ActiveXObject in IE and a object element in other browsers.

if(window.ActiveXObject) {
	nameCtrl = new ActiveXObject("Name.NameCtrl");
} else {
	try {
		nameCtrl = new ActiveXObject("Name.NameCtrl");
	} catch (e){
		nameCtrl = (function(b){
			var c = null;
			try {
				c = document.getElementById(b);
				if (!Boolean(c) && (Boolean(navigator.mimeTypes) && navigator.mimeTypes[b] && navigator.mimeTypes[b].enabledPlugin)) {
					var a = document.createElement("object");
					a.id = b;
					a.type = b;
					a.width = "0";
					a.height = "0";
					a.style.setProperty("visibility", "hidden", "");
					document.body.appendChild(a);
					c = document.getElementById(b)
				}
			} catch (d) {
				c = null
			}
			return c
		})("application/x-sharepoint-uc");
	}
}

This code works by trying to do IE10 and down supported code, failing back to IE11 code, and then failing back to other browsers.

Before you do anything, you need to check if it was initialized properly and if getting users’ statuses is enabled.

if(nameCtrl && nameCtrl.PresenceEnabled){
// code here
}

Showing a User’s Status

To show a user’s status, and update it any time it changes, we need to bind an event handler (OnStatusChange) to the nameCtrl.

nameCtrl.OnStatusChange = function(userName, status, id){
	document.getElementById(id).classList.remove("status-available","status-offline","status-away","status-inacall","status-outofoffice","status-busy","status-donotdisturb");
	switch (status) {
		case 0:
			//available
			document.getElementById(id).classList.add('status-available');
			break;
		case 1:
			// offline
			document.getElementById(id).classList.add('status-offline');
			break;
		case 2:
		case 4:
		case 16:
			//away
			document.getElementById(id).classList.add('status-away');
			break;
		case 3:
		case 5:
			//inacall
			document.getElementById(id).classList.add('status-inacall');
			break;
		case 6:
		case 7:
		case 8:
			document.getElementById(id).classList.add('status-outofoffice');
			break;
		case 10:
			//busy
			document.getElementById(id).classList.add('status-busy');
			break;
		case 9:
		case 15:
			//donotdisturb
			document.getElementById(id).classList.add('status-donotdisturb');
			break;
	}
};

NOTE: The classList API works in IE10 and above. For older versions of IE, you will need a shim.

The event handler passes in a userName (AD name of the user), status (a number which corresponds to a status), and an id (you provide this, usually the ID of the element where you are updating).

You can then use CSS to style accordingly.

Next, you need to register the usernames you want to show status for.

nameCtrl.GetStatus("username@domain.com", "myDiv");

When the page loads, the nameCtrl.OnStatusChange event will be called, and whenever the user’s status changes, it will be called, in real time.

blog1

Showing a Contact Card

To show a user’s contact card on mouse over (seems to be the only way to get it to show), you need to bind a onmouseover event and call the ShowOOUI method.

myDiv.onmouseover = function(){
	nameCtrl.ShowOOUI("username@domain.com", 0, 10, 10);
}

The second parameter is the iInputType parameter, which is apparently used to tell the control which event triggered the event, which can be either onmouseover or onfocusin. 0 is for onmouseover, so it is same to assume 1 would be onfocusin.

The third and forth parameters or the x and y coordinates, relative to the position of the cursor.

You will also need to tell the contact card to hide on onmouseout.

myDiv.onmouseout = function(){
	nameCtrl.HideOOUI();
}

blog2

Issues

IE11 not Working

If the plugin doesn’t work in IE11, but works if you change the document mode to IE10, it is because IE11 no longer recognizes ActiveXObject as a property of the window object. The code I have in the setup section of this post will fix this issue, but most blog posts don’t account for this. (see: http://msdn.microsoft.com/en-us/library/ie/dn423948(v=vs.85).aspx).

Not Showing up at all

No javascript errors but nothing seems to be working? For this to work, you may need to go into Internet Explorer’s Internet Options menu and add your domain (or localhost) as a trusted domain.

My name is Andrew McGivery. I currently work full time as an application developer at Manulife Financial in Canada. My current passion is building and leading highly engaged teams where employee happiness, learning, and growth is a priority.

14 Responses

  1. Hi.. This is really kewl and something i was looking out for… do u have a working copy of this code… i tried it.. but may be i am missing something. 🙁

    would be really kewl if you can share/upload a working file…

  2. I do not have sharepoint, It did not work. Do you have any other work around?

  3. Hi Andrew,

    I followed the same , was able to get presence updates in IE 10 and IE 11 , however in chrome and firefox its not at all working.When i looked further into it i figured out that the mime type (application/x-sharepoint-uc) was unavailable in these browsers.
    I would appreciate if you could guide me in the right direction here.

    Thanks,
    hari

  4. Ahmed Gewalt

    it works on IE 11, just use
    if (“ActiveXObject” in window){//IE11
    nameCtrl = new ActiveXObject(“Name.NameCtrl”);
    }

    the issue with me is in Chrome, it is not working at all, any Ideas ?!

    • Yep its working in IE 10 and IE 11 for me too , but sadly not in chrome/firefox . Is it working for you in Firefox?
      As i mentioned in the above comment i am unable to get that mimetype and build a object out of it.I dont know whether the problem is with the lync registration or it is something else.

      Explored UCWA , but couldnt proceed much with that , no solid guidance on that either.

  5. Ahmed Gewaly

    it is working fine with in FireFox, but Chrome not and I guess it is known issue in Chrome 🙁

  6. Maryanna

    read this on another site….As of April 2015, Chrome disabled NPAPI by default, which restricts the navigator.mimeTypes

  7. I have tried this and works. Only issue is the first time when the page loads the status is not correct. It comes as offline. Once I refresh it comes back correctly. I think the the nameCtrl.GetStatus method does not returns the correct status first time. Any thoughts?

  8. Hi,
    I need to get the ‘What’s Happening Today’ information for users.. I know that I can get this information from Mouse Overing the presence, but I want this from the NameCtrl property.. and I will use this field to populate a custom text/label field…
    Any thoughts?

Leave a Reply