Importing Contacts Using the Spreadsheet Launcher Control

Applies to: SharePoint Foundation 2010

Using a SpreadsheetLauncher control, this code example opens the address book and imports the selected contacts into the Contacts list of a website that is based on Microsoft SharePoint Foundation. It assumes that there is a link or button command that calls the ImportFromAddressBook function.

<!--Create an AddrBookWrapper object to open the address book and return all information for each contact selected.--> 

<SCRIPT language="VBScript"> 
   Function OpenABW() 
      Dim objAddrBkWrap 
      Dim objContacts 
      Dim objABWOp 

      OpenABW = "" 

      On Error Resume Next 

      Set objAddrBkWrap = CreateObject("MsSvAbw.AddrBookWrapper") 

      If IsObject(objAddrBkWrap) Then 
         objABWOp = objAddrBkWrap.AddressBook(, 1, , , , objContacts, , , True) 

         If objABWOp <> 0 then 
            OpenABW = "" 
            OpenABW = ProcessABWCollection(objContacts) 
         End If 

      End If 

      On Error GoTo 0 
   End Function 

<!--Create a SpreadsheetLauncher object to use for import of the data and ensure the object's presence.--> 

   Function EnsureImport() 
      Dim objEnsureImport 

      EnsureImport = 0 

      On Error Resume Next 

      Set objEnsureImport = CreateObject("SharePoint.SpreadsheetLauncher.1")
      If IsObject(objEnsureImport) Then 
      End If 

      On Error GoTo 0 
   End Function 

<!--Return success or failure of importing contacts.--> 

<SCRIPT language="JavaScript"> 
   function DoImportFromAddressBook()         {
      return OpenABW();                       } 

<!--Iterate through the properties and fields arrays created 
per contact by the ProcessABWCollection function, and create 
the CAML for registering each field and value in the 
Office namespace.--> 
   function XMLSetVars(obj, rgstProps, rgstFields)         {
      var ist; 
      var st = ""; 

      for (ist = 0; ist < rgstProps.length; ist++)         {
         { st += "<SetVar Name=\"urn:schemas-microsoft-      
            com:office:office#" +
            rgstFields[ist] + "\">" + 
            STSHtmlEncode(obj[rgstProps[ist]]) + "</SetVar>";
      return st;                                           } 

<!--Using arrays, assign to each contact item the same properties and 
fields, and construct the CAML code for importing the items via the 
batch manager RPC.--> 
   function ProcessABWCollection(col)         { 

      var rgstProps = new Array("FirstName", "LastName", "SMTPAddress", 
         "CompanyName", "JobTitle", "HomeTelephoneNumber", 
         "BusinessTelephoneNumber", "MobileTelephoneNumber", 
         "BusinessFaxNumber", "BusinessAddressStreet", 
         "BusinessAddressCity", "BusinessAddressState", 
         "BusinessAddressPostalCode", "BusinessAddressCountry", 
      var rgstFields = new Array("FirstName", "Title", "Email", 
         "Company", "JobTitle", "HomePhone", "WorkPhone", "CellPhone", 
         "WorkFax", "WorkAddress", "WorkCity", "WorkState", "WorkZip", 
         "WorkCountry", "Comments"); 
      var st; 
      var e = new Enumerator(col); 

   if (e.atEnd()) 
      return ""; 

   st = "<" + "ows:Batch OnError=\"Return\">"; 

   for (; !e.atEnd(); e.moveNext())               { 
      st += "<Method ID=\"0\"><SetList Scope=\"Request\">
         </SetList><SetVar Name=\"Cmd\">Save</SetVar>
         <SetVar Name=\"ID\">New</SetVar>"; 
      st += XMLSetVars(e.item(), rgstProps, rgstFields); 
      st += "<SetVar Name=\"urn:schemas-microsoft-
         + "\">" + e.item()["WebPage"] + ", </SetVar>"; 
      st += "</Method>";
      st += "</ows:Batch>"; 

      return st;                                   } 

<!--Initiate import of address book contacts and submit items to 

<SCRIPT language="JavaScript"> 
   function ImportFromAddressBook()        { 
      if (0 == EnsureImport())             { 

         st = DoImportFromAddressBook(); 

            if (st.length > 0)                     { 
               FormABWImport.NextUsing.value = window.parent.location; 
               FormABWImport.PostBody.value = st; 
               FormABWImport.submit();             } 

<!--Define a form for posting the imported data to the server.--> 

<FORM id=FormABWImport method="POST" 
   <INPUT type=hidden name="NextUsing" value=""> 
   <INPUT type=hidden name="Cmd" value="DisplayPost"> 
   <INPUT type=hidden name="PostBody" value=""> 

Notice that the ImportFromAddressBook function first calls the EnsureImport function to create a SpreadsheetLauncher object to import the data into the contacts list.

In the OpenABW function, an AddrBookWrapper object is used. This is an interface for an ActiveX control that facilitates display of the browsing and selection user interface (UI) of the address book. This object implements a modified version of the Microsoft Collaboration Data Objects (CDO) version 1.2.1 AddressBook method for the MAPISession object. In this example, three of the method's nine optional parameters are specified. The nSelBoxes parameter specifies the number of list boxes to display, and in this example it specifies only one. The SelUsers1Contacts parameter specifies the collection of contact items that are retrieved for each selected contact. The fFullInfo parameter contains a Boolean value, true, which specifies that all information for each contact is returned. When Microsoft Office is installed on the client computer, this control is provided in the Msosvabw.dll file.