Tuesday, July 9, 2013

Integrate Lync presence indicator with sharepoint 2013 custom web parts

As we know, we can easily intergrate lync features with sharepoint. in this post i am going to show how to integrate lync presence indicator with custom sharepoint web parts.

before that you have to make sure that Lync is already enabled in your sharepoint environment.if not, first installed it.

now follow these steps :

1. Create web part (In this post i am going to create normal web aprt not a visual web part) in visual studio and name it as "UserIndicator" then copy and paste this script into CreateChildControls() method

// get the current user

string[] CurrentUser = SPContext.Current.Web.CurrentUser.LoginName.ToString().Split(new string[] { "\\", "%5C" }, StringSplitOptions.RemoveEmptyEntries);
string UrlUserWithDomain = HttpContext.Current.Request.QueryString["accountname"];
string[] UrlUser;
string userName = string.Empty;
if (UrlUserWithDomain != null)
{
  UrlUser = UrlUserWithDomain.Split(new string[] { "\\", "%5C" }, StringSplitOptions.RemoveEmptyEntries);
  userName = UrlUser[UrlUser.Length - 1];
}
else
{
  userName = CurrentUser[CurrentUser.Length - 1];
}

// format the page

string pageFormat = string.Empty;
pageFormat += string.Format(@"
<div id = ""indicator"" class=""myProfile-user-presence""  onmouseover=""ShowOOUI()"" onmouseout=""HideOOUI()"">
    <div id = ""indicatorImg""></div>                       
</div>
                    
<script type=""text/javascript"" src='http://code.jquery.com/jquery-latest.js'></script>
<object id=""application/x-sharepoint-uc"" type=""application/x-sharepoint-uc"" width=""0"" height=""0"" style=""visibility: hidden;""></object>
                
<script type=""text/javascript"">
    jQuery.support.cors = true;
    var nameCtrl = null;
    var sipUri = ""{0}"".concat(""@virtusa.com""); 

    $(document).ready(function () {{
       try {{
         if (window.ActiveXObject) {{
           nameCtrl = new ActiveXObject(""Name.NameCtrl"");
         }} else {{
           nameCtrl = CreateNPApiOnWindowsPlugin(""application/x-sharepoint-uc"");
         }}

         if (nameCtrl.PresenceEnabled) {{
            nameCtrl.OnStatusChange = onStatusChange;
            nameCtrl.GetStatus(sipUri, ""1"");
         }}
                                
         }}
         catch (ex) {{ }}
         }}); 

function getLyncPresenceString(status) {{
  switch (status) {{
    case 0:
      return ""{1}"".concat('_layouts/images/IMNON.PNG');
      break;
    case 1:
      return ""{1}"".concat('_layouts/images/IMNOFF.PNG');
      break;
    case 2:                                
      return ""{1}"".concat('_layouts/images/IMNAWAY.PNG');
      break;
    case 3:
      return ""{1}"".concat('_layouts/images/IMNBUSY.PNG');
      break;
    case 4: 
      return ""{1}"".concat('_layouts/images/IMNAWAY.PNG');
      break;
    case 9:
      return ""{1}"".concat('_layouts/images/IMNDND.PNG');
      break;
    default:
      return ""{1}"".concat('_layouts/images/IMNAWAY.PNG');
  }}
}}
  
function onStatusChange(name, status, id) {{
 alert(name + "", "" + status + "", "" + id);
 $('#indicatorImg').replaceWith(
  '<img id=""StatusImg""  alt=""' + getLyncPresenceString(status) + '""src=""' + getLyncPresenceString(status) + '""/>'                                   
                                );
}}

function ShowOOUI() {{
   nameCtrl.ShowOOUI(sipUri, 0, 15, 15);
}}

function HideOOUI() {{
   nameCtrl.HideOOUI();
}}

</script>                 
", userName, GetSPServerUrl());

//Add the controls to page

Controls.Add(new LiteralControl(pageFormat));

// insert other support methods to return required URLs .

private string GetImageUrl()
{
     // read it from config file
     return "../_layouts/15/images/ProfileContactCard/";
}

private string GetSPServerUrl()
{
    // read it from config file
    return "Sharepoint MySiteUrl";
}

Note :
Replace GetImageUrl() , GetSPServerUrl() with actual values

finally deploy to sharepoint site using visual studio and insert that webpart to your page. 
If you want to deploy using wsp file manually , check this post

6 comments:

Anonymous said...

I am using your post as a reference yet in firefox/chrome browser is unable to identify the mime type "application/x-sharepoint-uc" as it is not getting availble.Though the above solution is wrking great for ie10 and ie11.

Note: Lync is installed in my local machine and i was currently signed in while checking the status.

Lync is not installed in my sever where sharepoint is hosted though.

cheers,
Hari

Anonymous said...

north face, hogan, coach outlet, air force, hollister pas cher, michael kors, coach outlet, lululemon, burberry, sac guess, nike roshe, nike free, true religion jeans, air jordan pas cher, lacoste pas cher, ralph lauren pas cher, michael kors, kate spade outlet, ralph lauren uk, michael kors, new balance pas cher, true religion jeans, ray ban uk, nike roshe run, converse pas cher, michael kors, hermes, mulberry, vans pas cher, louboutin pas cher, true religion outlet, ray ban pas cher, nike air max, oakley pas cher, air max, timberland, nike free run uk, kate spade handbags, nike air max, hollister, tn pas cher, replica handbags, nike air max, vanessa bruno, abercrombie and fitch, sac longchamp, true religion jeans, nike blazer, coach purses, longchamp pas cher, north face

Anonymous said...

ugg,uggs,uggs canada, canada goose, ugg,ugg australia,ugg italia, canada goose, hollister, wedding dresses, moncler, louis vuitton, canada goose outlet, swarovski, pandora charms, pandora jewelry, moncler outlet, moncler, sac louis vuitton pas cher, replica watches, louis vuitton, marc jacobs, louis vuitton, vans, moncler, swarovski crystal, pandora jewelry, toms shoes, moncler, juicy couture outlet, doudoune canada goose, karen millen, converse, lancel, canada goose outlet, coach outlet, gucci, ray ban, thomas sabo, moncler, supra shoes, canada goose uk, juicy couture outlet, links of london, barbour jackets, louis vuitton, canada goose, barbour, ugg boots uk, pandora charms, bottes ugg, converse outlet, moncler, montre pas cher, canada goose, ugg pas cher, moncler
ninest123 16.03

5689 said...

zzzzz2018.8.27
basket nike femme
pandora jewelry
cheap snapbacks
pandora outlet
salomon
basket nike
true religion outlet
canada goose jackets
ugg boots clearance
michael kors outlet online

5689 said...

zzzzz2018.8.27
ralph lauren uk
christian louboutin shoes
ferragamo outlet
canada goose jackets
polo ralph lauren
pandora
adidas superstar
christian louboutin outlet
skechers outlet
lacoste outlet

dythesh said...

t7z67w5k80 m9j30p0i79 m0q37e6f88 e3g36v5y08 t9b62k7p85 p1w09r6b60