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

2.5k views Asked by At

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.

2

There are 2 answers

0
Chandan Kumar On BEST ANSWER

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; }
    }
}
4
Imran Ali Khan On

you want to build basic a web-based front-end for communicator. The links from Peter will help show presence information, but only for a list of people that are part of the content of SharePoint page you are browsing.

In order to show your online contacts as defined in your Communicator contacts lists, as well as be able to send messages to them, you will need to write a whole lot of client-side script which connects to your locally running instance of Communicator.

The best place to start looking would be the Office Communicator 2007 SDK

http://www.microsoft.com/downloads/details.aspx?FamilyID=ed1cce45-cc22-46e1-bd50-660fe6d2c98c&displaylang=en

If you do a web search on "office communicator apis" you will get a whole lot of links that might be useful as well.

The Unified Communications forums might also help

http://social.msdn.microsoft.com/Forums/en-US/category/uc