Use harmon.ie SDK to customize upload to SharePoint behavior

harmon.ie SDK enables you to tailor harmon.ie and develop a module that manipulates the items uploaded to SharePoint.

What can you do using the SDK?

You can add code that will be called before and after uploading item(s) to SharePoint. This allows you to do many things, for example:

  • Add metadata to uploaded items.
  • Change the name of the uploaded item.
  • Convert the document format before upload.

Note: Adding Managed Metadata, Lookup, Person or Group and custom types are currently not supported by the SDK.

Getting started with the SDK:

Implementing a plugin for Outlook:

Set the default value to the full path of your module
  1. Implement your own plugin:
    1. Create a .NET Framework 3.5 Class Library project.
    2. Add a reference to Harmonie.SDK.dll (located in <harmon.ie installation dir>\bin) – read full class library documentation.
    3. Extend the UploadOperationHandler class:
      1
      public class uploadOperationTest : UploadOperationHandler
      
    4. Apply the [Harmonie.SDK.UploadOperation] attribute on the plugin class.
  2. Deploy the plugin into the harmon.ie runtime platform, using the registry:
    1. Add a sub-key under {HKCU|HKLM}\Software\Mainsoft\Prefs\Plugins with your module name.
      For example, {HKCU|HKLM}\Software\Mainsoft\Prefs\Plugins\MailRename
    2. Set the default value to the full path of your module.

Samples

The first sample below takes the name of an uploaded email item, and concatenates the date of the email to the name.
The date is taken from the email header mapping to SharePoint metadata.

The second sample sets a random number in a property called Random.

The third sample checks if the email contains a PDF attachment, and if so, uploads the attachment instead of the original email message.

Sample 1: Changing the name of an uploaded item

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
using System.IO;
using Harmonie.SDK;

namespace MailRenameHarmoniePlugin
{
    [Harmonie.SDK.UploadOperation]
    public class MailRenamePlugin : Harmonie.SDK.UploadOperationHandler
    {
        public override bool onBeforeUpload(IWrappedList items)
        {
            foreach (IUploadItemData itemData in items)
                itemData.DestinationName = 
                    Path.GetFileNameWithoutExtension(itemData.DestinationName) + 
                    FormatMailShortDate(itemData) + 
                    Path.GetExtension(itemData.DestinationName);
            return true;
        }

        
        private string FormatMailShortDate(IUploadItemData itemData)
        {
            //take the email date from the email header mapping to SharePoint columns
            if (itemData.Fields[Harmonie.SDK.MailHeader.DATE] != null)
            {
                string mailDate = 
                  ((MetaDataValue)itemData.Fields[Harmonie.SDK.MailHeader.DATE]).Value;
                if (!string.IsNullOrEmpty(mailDate))
                    return "_" + 
                      DateTime.Parse(mailDate).ToString("dd.MMM.yy-HH.mm.ss");
            }

            return string.Empty;
        }
    }
}

You can download a zip file containing this sample project from here.

Sample 2: Adding metadata to an uploaded item

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
using System;
using Harmonie.SDK;

namespace AddMetadataHarmoniePlugin
{
    [Harmonie.SDK.UploadOperation]
    public class AddMetadataPlugin : Harmonie.SDK.UploadOperationHandler
    {
        public override UploadAction onBeforeUpload(IUploadItemData single)
        {
            //add a random number between 0 to 1000 in the 'Random' number field
            single.Fields.Add("Random", new MetaDataValue( (new Random()).Next(0, 1000).ToString() ));
            return UploadAction.Continue;
        }
    }
}

Sample 3: Upload a PDF attachment instead of the original email message

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Harmonie.SDK;
using System.IO;

namespace UploadPDF
{
    [UploadOperation]
    public class UploadPDF : UploadOperationHandler
    {
        public override void init(ISidebar sidebar, bool isMulti)
        {
            base.init(sidebar, isMulti);
        }

        public override bool onBeforeUpload(IWrappedList items)
        {
        //In this callback, we check that a message is being uploaded. 
        //If it is a message, and it contains one PDF, we add the PDF to the uploaded items       
            var attachments = new List<IUploadItemData>();
            foreach (IUploadItemData item in items)
            {
                if (item is IUploadMailData)
                {
                    IList<IUploadItemData> itemAttachments = new List<IUploadItemData>();
                    IWrappedList innerAttachments = ((IUploadMailData)item).Attachments;

                    if (ContainsOnePDF((IUploadMailData)item))
                        itemAttachments.Add(innerAttachments[0] as IUploadItemData);
                   
                    attachments.AddRange(itemAttachments);
                }
            }

            foreach (IUploadItemData item in attachments)
                items.Add(item);

            return true;
        }
        public override UploadAction onBeforeUpload(IUploadItemData single)
        {
            //In this callback, we check that a message is being uploaded. 
            //If it is a message, and it contains one PDF, we skip it. 
            if (single is IUploadMailData)
            {
                if (ContainsOnePDF((IUploadMailData)single))
                    return UploadAction.Skip;
            }

            return UploadAction.Continue;
        }


        private bool ContainsOnePDF(IUploadMailData mail)
        {
            try
            {
                if (mail.Attachments != null && mail.Attachments.Count == 1)
                {
                    if (Path.GetExtension(((IUploadItemData)mail.Attachments[0])
                        .DestinationName).ToLower().TrimStart('.') == "pdf")
                    {
                        return true;
                    }
                }
            }
            catch
            {
                return false;
            }

            return false;
        }
    }

}

You can download a zip file containing this sample project from here.