none
Sync Active Directory to a Contacts List (WSS 3.0)

    Question

  • Hello everyone,

    Can anybody let me know about how to,

    Sync Active Directory to a Contacts List (WSS 3.0) without using thirdparty tools ?

    Is there any programmatic solution?

     

    Please help me out 

    Tuesday, June 24, 2008 4:43 PM

All replies

  • I havent received any response ..Guys please help me out about this.
     
    Tuesday, July 08, 2008 9:22 PM
  •  

    I would be interested in finding a solution to this too. I have searched online and found a couple of third party apps that claim to do it, but I feel as though there is another way to do this. If someone has any ideas, please let me know. Much appreciation!!!!
    Wednesday, July 09, 2008 8:39 PM
  • Create Active Directory Service using WCF – Accusing AD Functions using WCF Services – AD Services
    http://ledomoon.blogspot.com/2009/12/create-active-directory-service-using.html
    Waleed Mohamed
    • Proposed as answer by Ola500 Friday, January 22, 2010 1:25 AM
    Wednesday, December 09, 2009 10:32 AM
  • Old post I know! -

    I have accomplished this using a Powershell script based on Sharepoint User Management scripts on CodePlex = Totally Free!!

    I have modified the script 'Set-WSS-Contact-List-From-ActiveDirectory.ps1' to add a few extra properties from AD but out the box it updates most of the properties you'd need. We then just run the script via task scheduler every day to pick up any changes to AD user properties.

    NOTE: This is a one way sync from AD to Sharepoint, if you make a change to the SP list then it just gets overwritten the next time the script is run. This is OK for us as we only want AD admins changing properties directly in AD.

    Happy to share a copy of our script to anyone who wants it.

    Cheers,

    Alex

    Thursday, May 31, 2012 7:21 AM
  • Alex,

    Is there anyway that I could get that script from you?

    Best regards,

    chad S.

    Tuesday, August 21, 2012 7:39 PM
  • Hi Chad,

    Here's the script, its probably a bit rough round the edges as I'm not a PS expert by any means but it seems to serve our purposes OK.

    Notes:

      • You can add the comment "SHAREPOINT=FALSE" in the AD User Comments field to prevent the user being added to the contacts list (we use this to prevent service accounts appearing in our SharePoint list).
      • Here we use the 'Pager' field in AD to store the DDI number for the user.
      • The extension number is split out into a separate field in the SP list although it is represented in AD as part of the telephone number field (e.g. +44(0)1234 567890 X123).
    [bool]$Verbose = $true
    [int]$GLOBAL:TotalUsersAdded = 0
    [int]$GLOBAL:TotalUsersEdited = 0
    [bool]$IsChanged = $false
    
    function QuickCleanAllListItems([string]$SiteURL, [string]$ListName)
    {
       [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null
       $site = new-object Microsoft.SharePoint.SPSite($SiteURL)
       Write-Host "SiteURL", $SiteURL
    
       $sw = New-Object System.Diagnostics.StopWatch
       $sw.Start()
    
       $web = $site.OpenWeb()   
       $myList = $web.Lists[$ListName]
       Write-Host "Items Number before delete: ", $myList.Items.Count
    
        $guid = $myList.ID
        $strGuid = $guid.ToString()
    
       $strPost = "<?xml version=""1.0"" encoding=""UTF-8""?><ows:Batch OnError='Return'>"
       foreach($item in $myList.Items)
       {
          $strPost += "<Method><SetList Scope=""Request"">"+ $strGuid +"</SetList>"
          $strPost += "<SetVar Name=""ID"">"+ $item.ID +"</SetVar><SetVar Name=""Cmd"">Delete</SetVar>"
          $strPost += "</Method>"
       }
       $strPost += "</ows:Batch>"
    
    #   Write-Host "Batch: " $strPost
       $strProcessBatch = $web.ProcessBatchData($strPost)
       
       Write-Host "Result: " $strProcessBatch 
    
       $sw.Stop()
       Write-Host "Items total after delete: ", $myList.Items.Count
       write-host "$y Items add in " $sw.Elapsed.ToString()
       
       $web.Dispose()
       $site.Dispose()
    }
    
    function Set-Contact-Users-From-ActiveDirectory([string]$domaincnx, [object]$ContactList)
    {
    
    	#Filter on User which only exists
    	$strFilter = "(&(objectCategory=user)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2)(givenName=*)(sn=*)(samaccountname=*))"
    	
    	$objDomain = New-Object System.DirectoryServices.DirectoryEntry($domaincnx)
    
    	$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
    	$objSearcher.SearchRoot = $objDomain
    	$objSearcher.PageSize = 10000
    	$objSearcher.Filter = $strFilter
    	$objSearcher.SearchScope = "Subtree"
    	
    	
    	
    	$colProplist = "sn","givenname","name","samaccountname","objectsid","displayname","mail","company","physicaldeliveryofficename","postofficebox","streetaddress","telephonenumber","pager","homephone","postalcode","l","c","st","title","department","facsimiletelephonenumber", "description", "wwwhomepage", "mobile", "info"
    	foreach ($i in $colPropList)
    	{
    		$objSearcher.PropertiesToLoad.Add($i)|out-null
    	}
    	$colResults = $objSearcher.FindAll()
    	if($Verbose)
    	{
    		Write-Output "Domain:", $domaincnx
    	}
    	if($colResults.Count -gt 0)
    	{
    		Write-host "Total Users found in AD", $colResults.Count
    		foreach($user in $colResults)
    		{
    			if($verbose)
    			{
    				Write-host "User found in AD:  ", $user.Properties.samaccountname , "(", $user.Properties.name, ")"
    				Write-host "WebSite:           ", $user.Properties.wwwhomepage
    				Write-host "Description:       ", $user.Properties.description
    				Write-host "Job Title:         ", $user.Properties.title
    				Write-host "Company:           ", $user.Properties.company
    				Write-host "Office:            ", $user.Properties.physicaldeliveryofficename
    				Write-host "Email:             ", $user.Properties.mail
    				Write-host "Notes:	       ", $user.Properties.info
    				Write-host "Pager/DDI:	       ", $user.Properties.pager
    				#Write-host "==================================================="
    			}
    			
    			Set-Contact-Item $user $ContactList
    		}
    	}
    }
    
    function Set-Contact-Item([object]$ADUser, [object]$ContactList)
    {
    	[bool]$userInTheContactList = $false
    	foreach($myItem in $ContactList.Items)
    	{
    		$IsChanged = $false
    		
    		if($myItem["SAMAccountName"] -eq $ADUser.Properties.samaccountname) 
    		#User already exists so check each field value against AD to see if anything has changed
    		#If changes are present then change SP fields and run the update
    		{
    			
    			
    			$notes = [string]$ADUser.Properties.info
    			if(!$notes.Contains("SHAREPOINT=FALSE")) 
    			{
    				if ([string]$myItem["Title"] -ne [string]$ADUser.Properties.sn)
    				{
    					$myItem["Title"] = [string]$ADUser.Properties.sn
    					if($Verbose){Write-Host ("Title","changed")}
    					$IsChanged = $true
    				}
    				
    				if ([string]$myItem["FirstName"] -ne [string]$ADUser.Properties.givenname)
    				{
    					$myItem["FirstName"] = [string]$ADUser.Properties.givenname
    					if($Verbose){Write-Host ("firstname","changed") }
    					$IsChanged = $true
    				}
    				
    				if([string]$myItem["FullName"] -ne [string]$ADUser.Properties.name)
    				{
    					$myItem["FullName"] = [string]$ADUser.Properties.name
    					if($Verbose){Write-Host ("fullname","changed")}
    					$IsChanged = $true
    				}
    				
    				if([string]$myItem["JobTitle"] -ne [string]$ADUser.Properties.title)
    				{
    					$myItem["JobTitle"] = [string]$ADUser.Properties.title
    					if($Verbose){Write-Host ("jobTitle","changed")}
    					$IsChanged = $true
    				}
    				
    				if([string]$myItem["Comments"] -ne "Description: " + [string]$ADUser.Properties.description)
    				{
    					$myItem["Comments"] = "Description: " + [string]$ADUser.Properties.description
    					if($Verbose){Write-Host ("comments","changed")}
    					$IsChanged = $true
    				}
    				
    				if([string]$myItem["Company"] -ne [string]$ADUser.Properties.company)
    				{
    					$myItem["Company"] = [string]$ADUser.Properties.company
    					if($Verbose){Write-Host ("company","changed")}
    					$IsChanged = $true
    				}
    				
    				if([string]$myItem["Office"] -ne [string]$ADUser.Properties.physicaldeliveryofficename)
    				{
    					$myItem["Office"] = [string]$ADUser.Properties.physicaldeliveryofficename
    					if($Verbose){Write-Host ("office","changed")}
    					$IsChanged = $true
    				}
    				
    				if([string]$myItem["Email"] -ne [string]$ADUser.Properties.mail)
    				{
    					$myItem["Email"] = [string]$ADUser.Properties.mail
    					if($Verbose){Write-Host ("email","changed")}
    					$IsChanged = $true
    				}
    				
    					#Check if an extension number is specified as part of the Telephone Number
    					$telNo = [string]$ADUser.Properties.telephonenumber
    					if($telNo.Contains("X"))
    					{
    						#Store it in the extension column if an extension number is found
    						$posn = $telNo.IndexOf("X")
    						if([string]$myItem["ExtensionNo"] -ne $telNo.SubString($posn+1))
    						{
    							$myItem["ExtensionNo"] = $telNo.SubString($posn+1)
    							if($Verbose){Write-Host ("extno","changed")}
    							$IsChanged = $true
    						}
    						#Remove the extension number from the string
    						if([string]$myItem["WorkPhone"] -ne $telNo.SubString(0,$posn))
    						{
    							$myItem["WorkPhone"] = $telNo.SubString(0,$posn)
    							if($Verbose){Write-Host ("workphone","changed")}
    							$IsChanged = $true
    						}
    					}
    					else
    					{
    						if([string]$myItem["WorkPhone"] -ne [string]$ADUser.Properties.telephonenumber)
    						{
    							$myItem["WorkPhone"] = [string]$ADUser.Properties.telephonenumber
    							if($Verbose){Write-Host ("workphone","changed")}
    							$IsChanged = $true
    						}
    						if([string]$myItem["ExtensionNo"] -ne "n/a")
    						{
    							$myItem["ExtensionNo"] = "n/a"
    							if($Verbose){Write-Host ("extno","changed")}
    							$IsChanged = $true
    						}
    					}
    	
    				if([string]$myItem["Direct Dial Inwards"] -ne [string]$ADUser.Properties.pager)
    				{
    					$myItem["Direct Dial Inwards"] = [string]$ADUser.Properties.pager
    					if($Verbose){Write-Host ("pager","changed")}
    					$IsChanged = $true
    				}				
    
    				if([string]$myItem["WorkFax"] -ne [string]$ADUser.Properties.facsimiletelephonenumber)
    				{
    					$myItem["WorkFax"] = [string]$ADUser.Properties.facsimiletelephonenumber
    					if($Verbose){Write-Host ("workfax","changed")}
    					$IsChanged = $true
    				}
    				if([string]$myItem["CellPhone"] -ne [string]$ADUser.Properties.mobile)
    				{				
    					$myItem["CellPhone"] = [string]$ADUser.Properties.mobile
    					if($Verbose){Write-Host ("cellphone","changed")}
    					$IsChanged = $true
    				}
    				if([string]$myItem["HomePhone"] -ne [string]$ADUser.Properties.homephone)
    				{
    					$myItem["HomePhone"] = [string]$ADUser.Properties.homephone
    					if($Verbose){Write-Host ("home phone","changed")}
    					$IsChanged = $true
    				}
    				if([string]$myItem["WorkAddress"] -ne [string]$ADUser.Properties.streetaddress)
    				{
    					$myItem["WorkAddress"] = [string]$ADUser.Properties.streetaddress
    					if($Verbose){Write-Host ("workaddress","changed")}
    					$IsChanged = $true
    				}
    				if([string]$myItem["WorkZip"] -ne [string]$ADUser.Properties.postalcode)
    				{
    					$myItem["WorkZip"] = [string]$ADUser.Properties.postalcode
    					if($Verbose){Write-Host ("workzip","changed")}
    					$IsChanged = $true
    				}
    				if([string]$myItem["WorkCity"] -ne [string]$ADUser.Properties.l)
    				{
    					$myItem["WorkCity"] = [string]$ADUser.Properties.l
    					if($Verbose){Write-Host ("workcity","changed")}
    					$IsChanged = $true
    				}
    				if([string]$myItem["WorkState"] -ne [string]$ADUser.Properties.st)
    				{
    					$myItem["WorkState"] = [string]$ADUser.Properties.st
    					if($Verbose){Write-Host ("workstate","changed")}
    					$IsChanged = $true
    				}
    				if([string]$myItem["WorkCountry"] -ne [string]$ADUser.Properties.c)
    				{
    					$myItem["WorkCountry"] = [string]$ADUser.Properties.c
    					if($Verbose){Write-Host ("workcountry","changed")}
    					$IsChanged = $true
    				}
    				
    				if([string]$myItem["Website"] -ne [string]$ADUser.Properties.wwwhomepage)
    				{
    					[string]$myItem["Website"] = [string]$ADUser.Properties.wwwhomepage
    					if($Verbose){Write-Host ("website","changed")}
    					$IsChanged = $true
    				}
    				if([string]$myItem["Department"] -ne [string]$ADUser.Properties.department)
    				{
    					$myItem["Department"] = [string]$ADUser.Properties.department
    					if($Verbose){Write-Host ("department","changed")}
    					$IsChanged = $true
    				}
    				
    				
    				#Process the update if something has been changed
    				if($IsChanged)
    				{
    					$MyItem.Update()
    					$GLOBAL:TotalUsersEdited = $GLOBAL:TotalUsersEdited + 1
    					if($Verbose){Write-Host ("Record Updated")}
    				}
    				
    				$userInTheContactList = $true
    			}
    		}
    	}
    	if($userInTheContactList -ne $true) #New user to add
    	{
    		$notes = [string]$ADUser.Properties.info
    		if(!$notes.Contains("SHAREPOINT=FALSE")) 
    		{
    		$MyNewItem = $ContactList.Items.Add()
    		$MyNewItem["SAMAccountName"] = [string]$ADUser.Properties.samaccountname
    		$MyNewItem["Title"] = [string]$ADUser.Properties.sn
    		$MyNewItem["Email"] = [string]$ADUser.Properties.mail
    		$MyNewItem["FirstName"] = [string]$ADUser.Properties.givenname
    		$MyNewItem["FullName"] = [string]$ADUser.Properties.name
    		$MyNewItem["JobTitle"] = [string]$ADUser.Properties.title
    		$MyNewItem["Comments"] = "Description: " + [string]$ADUser.Properties.description
    		$MyNewItem["Company"] = [string]$ADUser.Properties.company
    		$MyNewItem["Office"] = [string]$ADUser.Properties.physicaldeliveryofficename
    
    		#Check if an extension number is specified as part of the Telephone Number
    		$telNo = [string]$ADUser.Properties.telephonenumber
    		if($telNo.Contains("X"))
    		{
    			#Store it in the extension column if an extension number is found
    			$posn = $telNo.IndexOf("X")
    			$MyNewItem["ExtensionNo"] = $telNo.SubString($posn+1)
    			#TODO:Remove the extension number from the string
    			$MyNewItem["WorkPhone"] = $telNo.SubString(0,$posn)
    		}
    		else
    		{
    			$MyNewItem["WorkPhone"] = [string]$ADUser.Properties.telephonenumber
    			$MyNewItem["ExtensionNo"] = "n/a"
    		}
    
    		$MyNewItem["Direct Dial Inwards"] = [string]$ADUser.Properties.pager
    		$MyNewItem["WorkFax"] = [string]$ADUser.Properties.facsimiletelephonenumber
    		$MyNewItem["CellPhone"] = [string]$ADUser.Properties.mobile
    		$MyNewItem["HomePhone"] = [string]$ADUser.Properties.homephone
    		$MyNewItem["WorkAddress"] = [string]$ADUser.Properties.streetaddress
    		$MyNewItem["WorkZip"] = [string]$ADUser.Properties.postalcode
    		$MyNewItem["WorkCity"] = [string]$ADUser.Properties.l
    		$MyNewItem["WorkState"] = [string]$ADUser.Properties.st
    		$MyNewItem["WorkCountry"] = [string]$ADUser.Properties.c
    		$MyNewItem["WebSite"] = [string]$ADUser.Properties.wwwhomepage
    		$MyNewItem["Department"] = [string]$ADUser.Properties.department
    		$MyNewItem.Update()
    		
    		$GLOBAL:TotalUsersAdded = $GLOBAL:TotalUsersAdded + 1
    	
    		}
    	}
    }
    
    function Check-GUID-Field([object]$ContactList)
    {
    	$GUIDFieldExist =$false
    	foreach($myField in $ContactList.Fields)
    	{
    		if($myField.InternalName -eq "SAMAccountName")
    		{
    			$GUIDFieldExist =$true
    		}
    	}
    	if($GUIDFieldExist -eq $false)
    	{
    		$ContactList.Fields.Add("SAMAccountName", [Microsoft.SharePoint.SPFieldType]::Text, 1)
    		$ContactList.Update()
    	}
    }
    
    function Set-UserInfoDetails([string]$WebURL, [string]$ListName, [string]$DomainCNX)
    {
    	[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null
    
    	$Thesite = new-object Microsoft.SharePoint.SPSite($WebURL)
    	$myWeb = $Thesite.openweb()
    	$myList = $myWeb.Lists[$ListName]
    	
    	Check-GUID-Field $myList
    	
    	if($myList -ne $null)
    	{
    		write-host "Contact list:", $myList.ID, "-", $myList.Title
    		Set-Contact-Users-From-ActiveDirectory $DomainCNX $myList
    	}
    	
    	$myWeb.Dispose()
    	$Thesite.Dispose()
    }
    
    function StartProcess()
    {
    	# Create the stopwatch
    	[System.Diagnostics.Stopwatch] $sw;
    	$sw = New-Object System.Diagnostics.StopWatch
    	$sw.Start()
    	cls
    
    	# Optional if you want to clean by code your contact list
    	#QuickCleanAllListItems "http://myWebApplication/sites/MySiteCollection" "My Contact ListName"
    
    	Set-UserInfoDetails "http://spwebsite/sp/contacts" "Contacts" "LDAP://DC=domain,DC=local"
    
    	$sw.Stop()
    	# Write the compact output to the screen
    	write-host $TotalUsersAdded " users added and ",$GLOBAL:TotalUsersEdited,"users edited in Time: ", $sw.Elapsed.ToString()
    	
    }
    
    cls
    StartProcess

    Wednesday, August 29, 2012 12:40 PM