Wednesday, August 29, 2012

Update User Information for an AD Group email in SharePoint...must be set for Group Alerts

If email won't sync in from AD correctly.

Open SP Management Shell (Powershell) and copy and paste:

function Sync-SPGroupEmail([string]$userName, [string]$email) {
  Get-SPSite -Limit All | foreach {
    $web = $_.RootWeb
    if ($_.WebApplication.UseClaimsAuthentication) {
      $claim = New-SPClaimsPrincipal $userName -IdentityType WindowsSamAccountName
      $user = $web | Get-SPUser -Identity $claim -ErrorAction SilentlyContinue
    } else {
      $user = $web | Get-SPUser -Identity $userName -ErrorAction SilentlyContinue
    }
    if ($user -ne $null) {
      $web | Set-SPUser -Identity $user -SyncFromAD
     
      $list = $web.Lists["User Information List"]
      $query = New-Object Microsoft.SharePoint.SPQuery
      $query.Query = "<Where><Eq><FieldRef Name='Name' /><Value Type='Text'>$userName</Value></Eq></Where>"
      foreach ($item in $list.GetItems($query)) {
        $item["EMail"] = $email
        $item.SystemUpdate()
      }
    }
    $web.Dispose()
    $_.Dispose()
  }
}

Or can run function above from C:\Powershell\SPGroupEmail.ps1

cd \
.\Powershell\SPGroupEmail.ps1

Then put in:

Sync-SPGroupEmail "PMC1\group_name" "test@phmic.com"

This Work e-mail must be set for Alerts to be sent to AD groups from SharePoint Lists.



Reference Link