How do I show a user with presence in code?

For a first posting of sample code, I thought I would cover what has been a question I see asked a lot, and that is “How do I show a user with presence with code?” It’s actually quite easy!

First, I have a file that I store in the SiteAssets library in the root of our site collection called JSUtils.js and that file along with jquery.jsĀ is loaded in the master page, thus all of my most useful functions are always loaded and ready to be called.

The createPresence function is my code, but createGuid and the supporting S4 functions were lifted and I cannot remember the source.

When the enterprise version of Office is installed on a user’s machine, it installs an Active-X control in the browser and is triggered by the presence of the ms-imnSpan class. All you need to supply is a Title and Email from a user object.


"use strict";
// Creates the standard person with presence display
function createPresence(title, email) {
    var presence = "";
    try {
        presence = '<nobr>' +
                        '<span>' +
                            '<span class="ms-imnSpan">' +
                                '<a class="ms-imnlink" href="#">' +
                                    '<span class="ms-spimn-presenceWrapper ms-imnImg ms-spimn-imgSize-10x10">' +
                                        '<img id="imn{' + createGuid() + '}, type=sip" class="ms-spimn-img ms-spimn-presence-online-10x10x32" title="" src="/sites/yourcollection/_layouts/15/images/spimn.png" alt="Away" name="imnmark" />' +
                                    '</span>' +
                                    title +
                                '</a>' +
                            '</span>' +
                        '</span>' +
                  '</nobr>';
    } catch(err) {}
    return presence;
}

// Useful for many things, but primarily for creating Presence bubbles.
function createGuid() {
    return (S4() + S4() + "-" + S4() + "-4" + S4().substr(0,3) + "-" + S4() + "-" + S4() + S4() + S4()).toLowerCase();
}
function S4() {
    return (((1+Math.random())*0x10000)|0).toString(16).substring(1); 
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s