Share via


Calling the Outlook Address Book from InfoPath

Ok, so I'm no code slinger, but once in a while I get my hands dirty and write (more like customize existing) code. Many times I have to break the bad news to customers that Outlook as a forms tool linking to Public Folders is going the way of the dodo. Therefore I get requests on how to convert Outlook forms to InfoPath. Most stuff is easy, but the stuff that involves e-mail functions and integration with Exchange are not as developed, and often require code. I asked internally at Microsoft on how to call into the Global Address Book in Exchange and pull back info into InfoPath, and got only one response. Here is an excerpt from the mail I sent back to the internal alias after I got it working. 

            Wanted to add a little back to the alias here with some follow-on work I did on automating the Outlook Address Book from InfoPath. Specifically, the code snippet below invokes the Outlook Address Book, allows a user to pick a single person from the GAL, and then puts that user’s alias into a field in the form. Sounds easy, but isn’t. Zoltan Pekic deserves most of the credit for his starter code, but I found it much more complex than what I actually needed, and thus wanted to drop this simpler snippet onto the alias for future posterity. The button on the form invokes the Access_GAL function. I used a SQL backend to my form, so the structure of the node path will likely be much simpler if not using a SQL backend. It’s all in jscript, everyone favorite dev environment (not!). Enjoy.

var g_oMapiSession = null;

     

function XDocument::OnLoad(eventObj)

{

g_oMapiSession = new ActiveXObject("MAPI.Session");

}

//=======

// The following function handler is created by Microsoft Office InfoPath.

// Do not modify the name of the function, or the name and number of arguments.

//=======

function Access_GAL::OnClick(eventObj)

{

      AddPerson();

}

 

function AddPerson()

{

      var oRecipients = null;

      g_oMapiSession.Logon("Outlook","",true);

      //var oTestMessage=EnsureValue(g_oMapiSession.Outbox.Messages.Add(),'Cannot create test message');

      var oTestMessage=g_oMapiSession.Outbox.Messages.Add();

      oRecipients=g_oMapiSession.AddressBook(oTestMessage.Recipients, "Managers", true, true, 1, "Add");

      //XDocument.UI.Alert(oRecipients.item(1).AddressEntry.address);

      var nameApprovingManager = XDocument.DOM.selectSingleNode("dfs:myFields/dfs:dataFields/d:Detail/@Approving_Mgr");

      nameApprovingManager.text=oRecipients.item(1).AddressEntry.address;

      var nameLength = String(nameApprovingManager.text).length;

      var equalPosition = FindTheEqual(nameApprovingManager.text);

      //XDocument.UI.Alert(equalPosition);

      nameApprovingManager.text = String(nameApprovingManager.text).substring(equalPosition, nameLength);

      g_oMapiSession.Logoff();

}

function FindTheEqual(strSearch)

{

      for (i=String(strSearch).length; i >0; i--)

      {

          if (strSearch.substring(i-1,i)== "=")

          {

                  return i;

          }

      }

      return -1;

}

Comments

  • Anonymous
    April 19, 2006
    Hello,

    I tried your code snippet above to create an address book button from within infopath and recieve the "[Collaboration Data Objects - [E_ACCESSDENIED(8007005)]]" error.  Can you give me some direction on this?

    Thanks.