ARTICLES

Home  > Articles  >  Integrating SharePoint and MS Outlook 2007 - increasing SharePoint solution adoption
Integrating SharePoint and MS Outlook 2007 - increasing SharePoint solution adoption
by Yaroslav Pentsarskyy


 If you’re rolling out SharePoint based intranet in your organization – you’re probably anxious to see how your users start using this new solution for all of their document management needs and more.
 
In many environments users have relied on email applications to store and collaborate on their documents for years. In some organizations I’ve seen members of the team training new employees on naming conventions for document revisions when sending them over email. This is not some old time story – this happened few months ago. If you’ve deployed SharePoint in an organization using Microsoft Outlook as their email client – and chances are you have – you can take advantage of integration features available in Microsoft Office framework. 
 
In this article we’ll cover how to build your own custom MS Outlook add-in that allows users to select message attachments and save them to a shared document library on the SharePoint portal. This real world scenario will show you how easy it is to integrate not only MS Outlook but other MS Office applications to fit your specific needs.
 
I assume you’re using Visual Studio 2008 as your development environment. During VS2008 installation – you have an option to install Visual Studio Tools for Office (VSTO). If you opted out to install this component – you need to run VS 2008 installation wizard and install VSTO.
 
We’ll start by creating new Outlook add-In project by clicking File->New -> Project -> C# -> Outlook Add-In. It’s a matter of preference whether you choose VB or C#, so we’ll use C# in this example.
 
If you’re used to developing SharePoint components, you might think you can use SharePoint object model to access various site objects like sub-sites and document libraries. Since our add-in will be running on the client environment we’ll have to use web services to access SharePoint objects on your server. This approach will still imply the same steps:
 
1. Get a hold of the attachment object that MS Outlook user has specified
2. Access SharePoint portal and enumerate document libraries available on the site or sub-site
3. Present MS Outlook user with the few destination locations available as menu options
4. Copy the file to the chosen destination location on the SharePoint portal.
 
First, in solution explorer, open Outlook->ThisAddIn.cs. In the Add-In startup section we’ll create an instance of Attachment Content Menu event handler:
 
private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            this.Application.AttachmentContextMenuDisplay += new ApplicationEvents_11_AttachmentContextMenuDisplayEventHandler(Application_AttachmentContextMenuDisplay);
        }
 
 
This event is going to be fired every time a user clicks on the attachment. The function that will execute on this event will allow us to perform any custom action before the menu is displayed to a user. In our case, we’ll populate a list of available document libraries on our site and create menu items representing those document libraries. As a result – our end user will have an option of selecting the document library where they would like to upload their file.
 
To be able to list various document libraries from your site, we’ll add a Web Reference to SharePoint list service using Solution explorer. The path to the web service definition file is http://[servername]/_vti_bin/lists.asmx
 
Our handler function, token above, will look like this:
 
void Application_AttachmentContextMenuDisplay(Microsoft.Office.Core.CommandBar CommandBar, AttachmentSelection Attachments)
        {
            Office.CommandBarButton button;
            ServiceReference1.Lists listService = new ServiceReference1.Lists();
            listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
 
            XmlNode ndLists = listService.GetListCollection();
 
            foreach (XmlNode list in ndLists.ChildNodes)
            {
                string title = list.Attributes["Title"].Value;
                listUrl = list.Attributes["DefaultViewUrl"].Value.Replace("/Forms/AllItems.aspx", string.Empty);
                button = (Office.CommandBarButton)CommandBar.Controls.Add(Type.Missing, Type.Missing, "stuff", Type.Missing, Type.Missing);
                button.Caption = title;
                button.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(button_Click);
            }
        }
 
 
In the above snippet we got a handle of the SharePoint list web service and extracted the list of document library URLs and Lists and their names; we created menu items for each document library and assigned a menu item handler. When our user clicks on one of the document library names, we’ll move the selected attachment to that library.
 
We’ll use a global variable to store our attachments and save them temporarily to a client’s disk before uploading them to a server. This step is required because attachment object does not have methods allowing us to retrieve its stream or byte array.
 
Also, you’ll notice we’re using DocLibHelper in the snippet below. This is a neat library I put together to upload files to a SharePoint document library using web services. DocLibHelper is freely available from my blog: www.sharemuch.com/wp-content/uploads/2009/03/spuploadtoremoteserver.zip . Once downloaded you need to reference this DLL in your solution explorer and use it within your add-in class. This DLL will need to be shipped along with your custom add-in to the client as it serves a communication layer between your add-in and SharePoint.
 
The snippet below will give you a starting point which you can develop to match your own requirements:
 
void button_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            DocLibHelper docLibHelper = new DocLibHelper();
            Dictionary<string, object> properties = new Dictionary<string, object>();
            if (att != null)
            {
                att.SaveAsFile(@"C:\" + att.FileName);
                properties.Add("Title", att.FileName);
                byte[] data = new byte[att.Size];
                File.Open(@"C:\" + att.FileName, FileMode.Open).Read(data,0,att.Size);
                string destinationUrl = "http://[server]"+listUrl;
                docLibHelper.Upload(destinationUrl + att.FileName, data, properties);
            }
        }
 
Finally, to test our add-in we’ll execute the solution; VS 2008 will launch MS Outlook instance locally where we can test the execution steps described.
 
If you found information in this article useful – please be sure to vote and comment! I’d love to hear your feedback.
 
Yaroslav Pentsarskyy