Bookmark and Share BitCoin Donate: 13t8gAWVpHP2ddxMp88d1NFpZjnFJC6UwK

How to control lync programmatically

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

using Microsoft.Lync.Model;

namespace lyncStatus
{
    class Program
    {
        static Microsoft.Lync.Model.LyncClient _lyncClient;
        static Timer t;

        static void Main(string args)
        {
            initialiseLync();

            t = new Timer();
            t.Interval = 5000;
            t.AutoReset = true;
            t.Elapsed += new ElapsedEventHandler(t_Elapsed);
            t.Start();

            
            Console.Write("enter command:");
            string command = Console.ReadLine();

            while (command != "quit")
            {
                handleCommand(command);
                showStatus();
                Console.Write("enter command:");
                command = Console.ReadLine();
            }
        }

        static void t_Elapsed(object sender, ElapsedEventArgs e)
        {
            showStatus();
        }

        private static void showStatus()
        {
            string songName = getSongName();

            List<KeyValuePair<PublishableContactInformationType, object>> data = new List<KeyValuePair<PublishableContactInformationType,object>>();
            data.Add(new KeyValuePair<PublishableContactInformationType,object>(PublishableContactInformationType.PersonalNote,songName));

            _lyncClient.Self.BeginPublishContactInformation(
                data
                , new AsyncCallback(delegate(IAsyncResult result)
                {
                }),
                null);
        }

        private static string getSongName()
        {
            System.IO.StreamReader reader = new System.IO.StreamReader(@"c:#customShitQuoteGenerator
owplaying.txt");
            return reader.ReadToEnd();
        }

        private static void handleCommand(string command)
        {
            switch (command)
            {
                case "update":
                    showStatus();
                    break;
                default:
                    break;
            }
        }

        private static void initialiseLync()
        {
            _lyncClient = Microsoft.Lync.Model.LyncClient.GetClient();

            if (_lyncClient == null) throw new Exception("Cannot create a lync client");

            _lyncClient.StateChanged += new EventHandler<Microsoft.Lync.Model.ClientStateChangedEventArgs>(_lyncClient_StateChanged);

            // Check if Lync is signed in
            if (_lyncClient.State != ClientState.SignedIn)
            {
                _lyncClient.BeginSignIn(
                    null,
                    null,
                    null,
                    result =>
                    {
                        if (result.IsCompleted)
                        {
                            _lyncClient.EndSignIn(result);
                            InitializeClient(); // Setup application logic
                        }
                        else
                        {
                            // could not sign in 
                        }
                    },
                    "Local user signing in" as object);
            }
            else
            {
                InitializeClient();
            }
        }


        static void InitializeClient()
        {
            if (_lyncClient == null)_lyncClient = Microsoft.Lync.Model.LyncClient.GetClient();
            _conversationManager = _lyncClient.ConversationManager;
            _contactManager = _lyncClient.ContactManager;
            _self = _lyncClient.Self;
            _automation = Microsoft.Lync.Model.LyncClient.GetAutomation();
        }

        static void  _lyncClient_StateChanged(object sender, Microsoft.Lync.Model.ClientStateChangedEventArgs e)
        {
            switch (e.NewState)
            {
            case ClientState.SigningOut:
                _conversationManager = null;
                _contactManager = null;
                _self = null;
                _lyncClient = null;
                break;
            case ClientState.SignedIn:
                InitializeClient();
                break;
            }
        }

        public static Microsoft.Lync.Model.Conversation.ConversationManager _conversationManager { get; set; }
        public static Microsoft.Lync.Model.ContactManager _contactManager { get; set; }
        public static Microsoft.Lync.Model.Self _self { get; set; }
        public static Microsoft.Lync.Model.Extensibility.Automation _automation { get; set; }
    }
}