QUERCUS BLOG
Industry Insights from Our Experts

Updating your SharePoint Newsfeed from Lync 2013

Filed under .NET, Lync, Productivity, SharePoint

If you are not familiar with the SharePoint 2013 Newsfeed, it allows your SharePoint users to keep their team up to date on what is going on and what they are working on. It is a great tool for increasing social collaboration in the workplace.

image

Other team members can see a quick feed of their colleagues updates through out the day.

image

Another Microsoft Office product, Lync, provides a “What’s happening today?” field that also lets you update your colleagues on what you are up to. Pretty similar to what the SharePoint Newsfeed does.

image

So two Microsoft Office products doing pretty much the same thing but in totally separate places. I would love it if I could update my “What’s happening today?” status on Lync and have it also add to my SharePoint Newsfeed. In fact I am really surprised this isn’t a feature out of the box with Office 2013. (Okay, maybe I’m not really that surprised, but I do think it would be a great feature)

To get around this limitation I decided to create my own application that can create a newsfeed post in SharePoint via Lync.

I called it “Lync Newsfeed Sync” (very original, I know), and here are a few screenshots of it:

image

As you can see it is a Windows system tray application with a pretty small footprint.

image

Start Sync tells the app to synchronize any “What’s happening today” changes from Lync to SharePoint, like so:

image

Within a few seconds, it appears on my team site SharePoint newsfeed.

image

So, lets see how we go about creating such an application.


Creating the Lync Newsfeed Sync application

To begin creating the application I needed to download a couple of SDKs

  1. Lync 2013 SDK available here
  2. Sharepoint Server 2013 Client Components SDK available here

The beauty of the SharePoint Server 2013 client components means I can develop a solution like this on my desktop machine without needing SharePoint to be installed. Love it!

Next I create a Windows Forms Application in Visual Studio 2012:

image

Add the following SharePoint References:

  • Microsoft.SharePoint.Client
  • Microsoft.SharePoint.Runtime
  • Microsoft.SharePoint.Client.UserProfiles

image

Add the following Lync reference:

  • Microsoft.Lync.Model

(I had to browse for it, it didn’t appear under Assemblies in Reference Manager)

image

So now your project references should look like this:

image


The SharePoint stuff

So now I want to create a class to handle the SharePoint piece of the work. Basically I want to have a utility method that I can pass some text into. This will then create a post in my team site Newsfeed with that text.

So Add a new class, lets call it SharePointUtility.cs

image

Add the following using statements to the top of the class:

using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Social;

 

Next change the class modifier to static and create a static function called updateNewsFeed with a parameter of type string. It should look something like this:

namespace LyncNewsFeedSync
{
    static class SharePointUtility
    {
        static void updateNewsFeed(string note)
        {
           

        }
    }
}

 

So now it’s time to add the code to create a newsfeed post in SharePoint.

namespace LyncNewsFeedSync
{
    static class SharePointUtility
    {
        static void updateNewsFeed(string note)
        {

            string serverUrl = RegistryActions.Read("NewsfeedSiteURL");

            // Define properties for the post.
            SocialPostCreationData postCreationData = new SocialPostCreationData();
            postCreationData.ContentText = note;

            // Get the client context.
            ClientContext clientContext = new ClientContext(serverUrl);

            // Get the SocialFeedManager instance.
            SocialFeedManager feedManager = new SocialFeedManager(clientContext);

            // Publish the post. 
            feedManager.CreatePost(serverUrl + "/newsfeed.aspx", postCreationData);
            clientContext.ExecuteQuery();
           
        }
    }
}

 

Lets go through it bit by bit:

string serverUrl = RegistryActions.Read("NewsfeedSiteURL");

First get the Url of the SharePoint server that hosts the newsfeed. I am storing it in the registry but really this could come from application config, constant etc.

SocialPostCreationData postCreationData = new SocialPostCreationData();
postCreationData.ContentText = note;

Next we create a SocialPostCreationData object. This represents the content for a microblog (or newsfeed) post.  Set the ContentText property to the note parameter we passed into our function.

// Get the client context.
ClientContext clientContext = new ClientContext(serverUrl);

Now we create the ClientContext object. This is what creates our connection to the SharePoint site and allows us to interact with SharePoint objects like sites, lists etc.

// Get the SocialFeedManager instance.
SocialFeedManager feedManager = new SocialFeedManager(clientContext);

// Publish the post. 
feedManager.CreatePost(serverUrl + "/newsfeed.aspx", postCreationData);
clientContext.ExecuteQuery();

Finally we create the SocialFeedManager object which allows us to act against the Newsfeed by creating posts and modifying threads. We create a post by calling the CreatePost method and passing in the SharePoint server Url as the TargetId and the postCreationData object as our SocialPostCreationData object.

That completes the SharePoint piece. Anytime SharePointUtility.updateNewsFeed is called we will create a new newsfeed post for the current user.


The Lync Stuff

Now that we have our SharePoint method ready for creating posts, how are we going to call it. Well, what we want to do is basically fire our SharePoint updateNewsFeed method anytime the user makes a change to their Lync client “What’s happening today” area.

Luckily the Lync API makes this pretty straightforward.

Create a new class called LyncUtility. Add the following using statement:

using Microsoft.Lync.Model;

 

Next we will create a LyncClient object:

private LyncClient lyncClient;

The LyncClient class is our main entry point to the Lync API. This class will be our representation of the running Lync client on the users machine.

In the class constructor lets grab an instance of the running client using the GetClient method.

Note: I really should have some error handling here, to handle the client not running for example, but for the purposes of the blog (and laziness) I’ve left that out.

namespace LyncNewsFeedSync
{
    public class LyncUtility
    {

        private LyncClient lyncClient;

        public LyncUtility()
        {
            lyncClient = LyncClient.GetClient();
        }
    }
}

 

Next we want to create a method that allows us to start synchronizing Lync changes to the SharePoint newsfeed.

The Lync API makes this really easy by surfacing any changes to a Lync Contact via the Contact.ContactInformationChanged event.

public void startSync()
{
   if (lyncClient.State == ClientState.SignedIn)
   {
      Contact lyncContact = lyncClient.Self.Contact;
      lyncContact.ContactInformationChanged += new 
           EventHandler<ContactInformationChangedEventArgs>(_Contact_ContactInformationChanged);
   }
}

First we check that the user is signed into the Lync client. If they are we grab the current user Contact instance using lyncClient.Self.Contact. Then we attach an event handler called _Contact_ContactInformationChanged to the ContactInformationChanged event.

To allow for our application to stop synchronizing we add another method that is practically identical except it removes our event handler from the ContactInformationChanged.

public void stopSync()
{
   if (lyncClient.State == ClientState.SignedIn)
   {
      Contact lyncContact = lyncClient.Self.Contact;
      lyncContact.ContactInformationChanged –= new 
           EventHandler<ContactInformationChangedEventArgs>(_Contact_ContactInformationChanged);
   }
}

 

Finally we need to create our event handler. It should check what type of change has occurred to our current users Contact object. If it is a change to “What’s happening today” this gets raised as a ContactInformationType.DefaultNote event. Then we want to grab the text and call our SharePoint utility to create the post.

protected void _Contact_ContactInformationChanged(object sender, ContactInformationChangedEventArgs e)
{
   if (e.ChangedContactInformation.Contains(ContactInformationType.DefaultNote))
   {
      object newnote = ((Contact)sender).GetContactInformation(ContactInformationType.DefaultNote);

      if (newnote != null)
      {
          SharePointUtility.updateNewsFeed(newnote.ToString());
      }
   }
}

That’s all that’s required for us to achieve our goal of monitoring the changes to the Lync client and acting on them.


Bringing it all together

Now the next steps involve setting up the Main form to act like a system tray application should. I’m not going to go into the details of how to do this because I’m only really interested in showing the Lync and SharePoint pieces. In a nutshell you add NotiyIcon and ContextMenuStrip from the toolbox to your main form and configure about 5 properties to get it to act as an application should in the System Tray. For more information on creating a basic system tray application click here.

So now I have a windows form with two context menu items called “Start Sync” and Exit:

image

If we look at the MainForm class we see the following code:

namespace LyncNewsFeedSync
{
    public partial class MainForm : Form
    {

        public LyncUtility lyncUtility = new LyncUtility();

        public MainForm()
        {
            InitializeComponent();
        }

        private void syncControl_Click(object sender, EventArgs e)
        {
            if (this.contextMenuStrip1.Items[0].Text == "Start Sync")
            {
                lyncUtility.startSync();
                this.contextMenuStrip1.Items[0].Text = "Stop Sync";
            }
            else
            {
                lyncUtility.stopSync();
                this.contextMenuStrip1.Items[0].Text = "Start Sync";
            }

        }

        private void exitApp_Click(object sender, EventArgs e)
        {
            this.Close();
        }
   }
}

Notice our LyncUtility object is instantiated as soon as the application is launched. This object needs to persist throughout the lifecycle of the application so we can keep synchronizing with Lync and SharePoint.

The method syncControl_Click corresponds to the “Start Sync” menu click event. It is pretty straight forward, it will either Start or Stop synchronizing using the LyncUtility.startSync() and LyncUtility.stopSync() methods we created earlier.

Finally the method exitApp_Click allows us to close the application if a user clicks Exit from the menu.


Summary

So that wraps up how I went about creating a system tray application to create newsfeeds in SharePoint from Lync. There is room for improvement obviously such as making it more “production ready” with error handling etc.

I hope to do a follow up post at some stage showing how we can also use this to create real #tags in SharePoint from Lync.

If anyone is interested in the source code, please let me know and I can add a link.

Copyright 2017 by Quercus Solutions
Login