InfiniTec - Henning Krauses Blog

Don't adjust your mind - it's reality that is malfunctioning

Setting the Homepage of an Exchange folder using the EWS Managed API

A while ago, Glen posted an article on his blog on how to set homepage of a folder using ADO and later he posted a version of that script which uses the EWS managed API to do this in this MSDN forum thread. However, he wrote it for the first version of the API, and the EWS Managed API 1.1 has a slightly different object model. Since someone on the MSDN forums had difficulties to update the script to work with the EWS Managed API 1.1, I thought I just post an updated version here:

private static void SetFolderHomePage(IEnumerable<string> pathFragments, string url, ExchangeService service)
{
    var folderWebviewinfoProperty = new ExtendedPropertyDefinition(14047, MapiPropertyType.Binary);
    var root = Folder.Bind(service, WellKnownFolderName.MsgFolderRoot);
    var targetFolder = root;
    foreach (var fragment in pathFragments)
    {
        var result = service.FindFolders(targetFolder.Id, new SearchFilter.IsEqualTo(FolderSchema.DisplayName, fragment), new FolderView(1));
        if (result.TotalCount == 0)
        {
            throw new InvalidOperationException(string.Format("Folder fragment {0} was not found.", fragment));
        }
        targetFolder = result.Folders[0];
    }

    targetFolder.SetExtendedProperty(folderWebviewinfoProperty, EncodeUrl(url));
    targetFolder.Update();
}

private static byte[] EncodeUrl(string url)
{
    var writer = new StringWriter();
    var dataSize = ((ConvertToHex(url).Length / 2) + 2).ToString("X2");

    writer.Write("02"); // Version
    writer.Write("00000001"); // Type
    writer.Write("00000001"); // Flags
    writer.Write("00000000000000000000000000000000000000000000000000000000"); // unused
    writer.Write("000000");
    writer.Write(dataSize);
    writer.Write("000000");
    writer.Write(ConvertToHex(url));
    writer.Write("0000");
    
    var buffer = HexStringToByteArray(writer.ToString());
    return buffer;
}

private static string ConvertToHex(string input)
{
    return string.Join(string.Empty, input.Select(c => ((int) c).ToString("x2") + "00").ToArray());
}

private static byte[] HexStringToByteArray(string input)
{
    return Enumerable
        .Range(0, input.Length/2)
        .Select(index => byte.Parse(input.Substring(index*2, 2), NumberStyles.AllowHexSpecifier)).ToArray();
}

You can set the homepage of a folder by calling the SetFolderHomepage method:

SetFolderHomePage(service, new[] {"InfiniTec blog"}, http://www.infinitec.de);

Posted by Henning Krause on Wednesday, October 05, 2011 7:30 PM, last modified on Wednesday, October 05, 2011 7:34 PM
Permalink | Post RSSRSS comment feed

Comments (1) -

On 10/6/2011 9:39:22 PM JEmlay wrote:

JEmlay

* SetFolderHomePage(new[] {&amp;amp;amp;amp;amp;quot;FOLDERNAME&amp;amp;amp;amp;amp;quot;}, &amp;amp;amp;amp;amp;quot;http://URL.COM&amp;amp;amp;amp;amp;quot;, service);

Reply

 +Pingbacks and trackbacks (1)

Add comment

biuquote
  • Comment
  • Preview
Loading