Importing Contacts with the Spreadsheet Launcher Control

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

Using a SpreadsheetLauncher control, this code example opens the address book and imports the selected contacts into the Contacts list of a Web site that is based on Windows SharePoint Services. It assumes the existence of a link or button command that calls the ImportFromAddressBook function.

[Visual Basic]

<!--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 = "" 
         Else 
            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 
         objEnsureImport.EnsureImport() 
      End If 

      On Error GoTo 0 
   End Function 
</SCRIPT> 

<!--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", 
         "Body"); 
      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-
         com:office:office#WebPage" 
         + "\">" + e.item()["WebPage"] + ", </SetVar>"; 
      st += "</Method>";
                                                   } 
      st += "</ows:Batch>"; 

      return st;                                   } 
</SCRIPT> 

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

<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();             } 
                                           } 
                                           } 
</SCRIPT> 

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

<FORM id=FormABWImport method="POST" 
      action="http://STSServer1/Sub1/_vti_bin/owssvr.dll?CS=109"> 
   <INPUT type=hidden name="NextUsing" value=""> 
   <INPUT type=hidden name="Cmd" value="DisplayPost"> 
   <INPUT type=hidden name="PostBody" value=""> 
</FORM> 

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, which 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 retrieved for each selected contact. The fFullInfo parameter contains a Boolean value, true, specifying that all information for each contact be returned. When the Microsoft Office system is installed on the client, this control is provided in the Msosvabw.dll file.