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