This documentation is archived and is not being maintained.

Code Example for Importing Contacts

SharePoint Team Services 1.1

Code Example for Importing Contacts

This code example opens up the address book and imports the contacts selected into the Contacts list of a Web site based on SharePoint Team Services from Microsoft. It assumes the existence of 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 = ""
         Else
            OpenABW = ProcessABWCollection(objContacts)
         End If
         
      End If
   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 StXMLSetVars(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] + "\">" + 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", "Body");
      var rgstFields = new Array("FirstName", "Title", "Email", "Company", "JobTitle", "HomePhone", "WorkPhone",
         "CellPhone", "WorkFax", "WorkAddress", "WorkCity", "WorkState", "WorkZip", "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\">Contacts</SetList><SetVar Name=\"Cmd\">Save</SetVar><SetVar Name=\"ID\">New</SetVar>";
      st += StXMLSetVars(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 for importing the data into the contacts list.

In the OpenABW function, the AddrBookWrapper object is an interface for a safe Microsoft ActiveX control (implemented in msosvabw.dll, which is installed with Microsoft Office XP) that facilitates display of the Address Book's browsing and selection user interface. This object implements a modified version of the Microsoft Collaboration Data Objects (CDO 1.21) AddressBook method for the MAPISession object. In this example, three of the method's nine optional parameters have been specified. The second parameter, nSelBoxes, specifies the number of list boxes to display, in this case only one. The sixth parameter, SelUsers1Contacts, specifies the collection of contact items retrieved for each selected contact. The ninth parameter, fFullInfo, contains a Boolean value, True meaning that all information for each contact be returned.


Show: