Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Get all users from Office Communicator to asp.net web page?

I am facing a serious problem. I want to display all users and their status in a HTML Table .Is it possible to do using javascript ?

I have gone through the below link where it is showing only one user and it's status and user mail id is hard coded.

Integrate Microsoft Office Communicator 2007 in ASP.NET Page

Javascript

<script type="Javascript">

     var sipUri = "[email protected]";

     var nameCtrl = new ActiveXObject('Name.NameCtrl.1');
     if (nameCtrl.PresenceEnabled)
     {
        nameCtrl.OnStatusChange = onStatusChange;
        nameCtrl.GetStatus(sipUri, "1");
     }


     function onStatusChange(name, status, id)
     {
        // This function is fired when the contacts presence status changes.
        // In a real world solution, you would want to update an image to reflect the users presence
           alert(name + ", " + status + ", " + id);
     }

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

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

 </script>

HTML

 <span onmouseover="ShowOOUI()" onmouseout="HideOOUI()" style="border-style:solid">Your Contact</span>
 <table id="tblContacts" runat="server">
    <tr>
       <td> email id </td>
       <td> status </td>
    </tr>
    <tr>
       <td> --- </td>
       <td> --- </td>
    </tr>
    <tr>
       <td> --- </td>
       <td> --- </td>
    </tr>
    <tr>
       <td> --- </td>
       <td> --- </td>
    </tr>
 </table>

Here in this example it is showing for a single user and user mail id is defined as a string(hard coded). i want to show all users. Is it possible through javascript ?

Any solution/Demo will be very much helpful for me.

Thanks.

like image 652
Chandan Kumar Avatar asked Nov 10 '22 15:11

Chandan Kumar


1 Answers

After a long try I am able to achieve the functionality according to my need. Here I am explaining the steps to be followed.

I used "Communicator API " with "NameCtrl" to achieve that

1) Refer this link http://msdn.microsoft.com/en-us/library/bb787231%28v=office.12%29.aspx

why I am telling to refer this, because To develop applications using the Microsoft Office Communicator Automation API, the following requirements must be met:

Microsoft Office Communicator 2007 is installed on your development machine.

Microsoft Office Communicator 2007 SDK is installed on the development machine. The SDK is available for download from MSDN.

2) Install Lync2010 on the server and login to that.

3) add reference to CommunicatorAPI.dll and CommunicatorPrivate.dll from your Web application

I am using communicator images here which I have downloaded from "http://www.microsoft.com/en-us/download/details.aspx?id=10503" . it's a msi file . download it and execute. in the demo, you can copy these images and add to your application

Here is the complete solution.

HTML

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MARSWebCommunicator._Default" %>

<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
    <section class="featured">
        <div class="content-wrapper">
        </div>
    </section>
    <script src="Scripts/jquery-latest.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        var sipUri = "[email protected]";
        var nameCtrl = new ActiveXObject('Name.NameCtrl.1');

        $(document).ready(function () {
            sipUri = $("#<%=HiddenField1.ClientID %>").val();
            if (sipUri != "") {
                if (nameCtrl.PresenceEnabled) {
                    nameCtrl.OnStatusChange = onStatusChange;
                    nameCtrl.GetStatus(sipUri, "1");
                }
            }
        });

        function onStatusChange(name, status, id) {
            // This function is fired when the contacts presence status changes.
            // In a real world solution, you would want to update an image to reflect the users presence
            //alert(name + ", " + status + ", " + id);
        }

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

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

    </script>
</asp:Content>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <div>
        <asp:HiddenField ID="HiddenField1" runat="server" />
    </div>
    <div id="dvContactdetails">
        <table border="1">
            <tr>
                <td>Email Id</td>
                <td>Name</td>
                <td>Status</td>
            </tr>
            <tr>
                <td>
                    <asp:DropDownList ID="drpEmails" AutoPostBack="true" runat="server" OnSelectedIndexChanged="drpEmails_SelectedIndexChanged"></asp:DropDownList></td>
                <td>
                    <asp:Image ID="Image1" onmouseover="ShowOOUI()" onmouseout="HideOOUI()" ImageUrl="presence_images/presence_16-unknown.bmp" runat="server" />
                    <asp:Label ID="lblName" runat="server" Text="Contact Name"></asp:Label>
                </td>
                <td>
                    <asp:Label ID="lblStatus" runat="server" Text="Contact Status"></asp:Label></td>
            </tr>
        </table>
    </div>   
</asp:Content>

Codebehind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunicatorAPI;

namespace MARSWebCommunicator
{
    public partial class _Default : Page
    {
        CommunicatorAPI.Messenger communicator = null;
        List<CustomContacts> lstContactDetails;


        protected void Page_Load(object sender, EventArgs e)
        {
            lstContactDetails = new List<CustomContacts>();
            communicator = new CommunicatorAPI.Messenger();
            string mymailid = "[email protected]";

            var contact = GetContact(mymailid);
            int s = (int) contact.Status;
            lblStatus.Text = GetStatus(s);

            if (!Page.IsPostBack)
            {
                drpEmails.Items.Add(mymailid);
                lblName.Text = contact.SigninName.ToString();
                HiddenField1.Value = contact.SigninName.ToString();

                lstContactDetails = GetAllEmailsWithFiendlyName();
                foreach (CustomContacts aContact in lstContactDetails)
                {
                    drpEmails.Items.Add(aContact.ContactEmailId);
                }
            }
        }

        protected void drpEmails_SelectedIndexChanged(object sender, EventArgs e)
        {
            string currentmailid = drpEmails.SelectedItem.Text;
            var contact = GetContact(currentmailid);
            lblName.Text = contact.FriendlyName.ToString();
            int s = (int)contact.Status;
            lblStatus.Text = GetStatus(s);

            if (HiddenField1.Value != "")
            {
                HiddenField1.Value = "";
                HiddenField1.Value = contact.SigninName.ToString();
            }
        }

        protected string GetStatus(int s)
        {
            string status = string.Empty;
            string src = string.Empty;
            int tempstatusno = s;
            switch (s)
            {
                case 0 :
                    status = "UNKNOWN";
                    src = "presence_images/presence_16-unknown.bmp";
                    break;
                case 1:
                    status = "OFFLINE";
                    src = "presence_images/presence_16-off.bmp";
                    break;
                case 2:
                    status = "ONLINE";
                    src = "presence_images/presence_16-online.bmp";
                    break;
                case 6:
                    status = "INVISIBLE";
                    src = "presence_images/presence_16-unknown.bmp";
                    break;
                case 10:
                    status = "BUSY";
                    src = "presence_images/presence_16-busy.bmp";
                    break;
                case 14:
                    status = "BE_RIGHT_BACK";
                    src = "presence_images/presence_16-idle-busy.bmp";
                    break;
                case 18:
                    status = "IDLE";
                    src = "presence_images/presence_16-idle-online.bmp";
                    break;
                case 34:
                    status = "AWAY";
                    src = "presence_images/presence_16-away.bmp";
                    break;
                case 50:
                    status = "ON_THE_PHONE";
                    break;
                case 66:
                    status = "OUT_TO_LUNCH";
                    break;
                case 82:
                    status = "IN_A_MEETING";
                    break;
                case 98:
                    status = "OUT_OF_OFFICE";
                    break;
                case 114:
                    status = "DO_NOT_DISTURB";
                    src = "presence_images/presence_16-dnd.bmp";
                    break;
                case 130:
                    status = "IN_A_CONFERENCE";
                    break;
                case 146:
                    status = "ALLOW_URGENT_INTERRUPTIONS";
                    break;
                case 162:
                    status = "MAY_BE_AVAILABLE";
                    break;
                case 178:
                    status = "CUSTOM";
                    break;
                default:
                    status = "OFFLINE";
                    src = "presence_images/presence_16-unknown.bmp";
                    Image1.ImageUrl = src;
                    break;
            }
            Image1.ImageUrl = src;
            return status;
        }

        public IMessengerContact GetContact(string signinName)
        {
            return communicator.GetContact(signinName, communicator.MyServiceId) as IMessengerContact;
        }

        public List<CustomContacts> GetAllEmailsWithFiendlyName()
        {
            List<CustomContacts> lstContacts = new List<CustomContacts>();

            IMessengerContacts messengerContacts = (IMessengerContacts)communicator.MyContacts;
            foreach (IMessengerContact acontact in messengerContacts)
            {
                CustomContacts aContact = new CustomContacts();
                aContact.ContactName = acontact.FriendlyName.ToString();
                aContact.ContactEmailId = acontact.SigninName.ToString();

                lstContacts.Add(aContact);
            }
            return lstContacts;
        }

    }

    public class CustomContacts
    {
        public string ContactEmailId { get; set; }
        public string ContactName { get; set; }
        public string ContactStatus { get; set; }
    }
}
like image 135
Chandan Kumar Avatar answered Nov 14 '22 21:11

Chandan Kumar