Tuesday, September 30, 2014

SharePoint 2013: Update User Information without User Profile Synchronization Service

I had to update user information in SharePoint 2013 without using the User Profile Synchronization Service. I used a PowerShell script to do so. The user, who need an update need be entered in a XML. Here's the script:
Add-PSSnapin Microsoft.SharePoint.PowerShell
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
Import-Module ActiveDirectory

# Load XML file
$configFile = "PATHtoXML\XMLNAME.xml"
$config = [xml](Get-Content $configFile)

foreach($userName in $config.Users.User)
{
  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)
  {
      #Cut off domain information [AD]
      $parts = $user.toString().Split("\");
      $cutUser = $parts[1]
      # Get all user properties from [AD]
      $adUser = Get-ADUser -Identity $cutUser -Properties *
      $web | Set-SPUser -Identity $user -SyncFromAD
      # Get User Information List [SP]
      $list = $web.SiteUserInfoList
      #Query for user [SP]
      $query = New-Object Microsoft.SharePoint.SPQuery
      $query.Query = "$user"

      foreach($item in $list.GetItems($query))
      {
        #Update the User Information in SharePoint
        $item["WorkPhone"] = $adUser.OfficePhone
        $item.Update()
      }
    }
    $web.Dispose()
  }
}

And here's the XML:
<Users>
  <User>DOMAIN\USER</User>
</Users>

2 comments:

  1. Very informative post. I sometimes do presentations on SharePoint and was wondering if I could use your Print List example in my presentations and refer my audience to your website for further info.

    ReplyDelete