Thursday, March 29, 2012

Create User Profile Section using Powershell (SharePoint 2010)

Run the following script from SharePoint 2010 Management Shell.

Then REMEMBER  to perform IISReset in order for the new section to appear on the Manage User Properties page (Alternatively wait for 1 hour for the timer job to run and perform the required synchronization).


---------------------------------
#The script is to create/update User Profile section
#

param([string] $siteURL,        # Site Collection URL       
      [string] $name,
      [string] $displayName,
      [int]    $displayOrder
     )       
       
   
    try
    {
        $context = Get-SPServiceContext -Site $siteUrl;
        $cm = New-Object Microsoft.Office.Server.UserProfiles.UserProfileConfigManager($context);   
       
        $pcol = $cm.GetPropertiesWithSection();

        $exists = $false;
        foreach ($temp in $pcol)
        {
            if ($temp.Name -eq $name)
            {
                Write-Host "Section $name Exists!";
                $exists = $true;
                $p = $temp
            }
        }

        if ($exists -ne $true)
        {
            $p = $pcol.Create($true);
        }

        $p.Name = $name;           
        $p.ChoiceType = [Microsoft.Office.Server.UserProfiles.ChoiceTypes]::Off;

        $p.DisplayName = $displayname;
        $p.Commit();       

        $pcol = $cm.GetPropertiesWithSection();
        $pcol.SetDisplayOrderBySectionName($p.Name, $displayOrder);
        $pcol.CommitDisplayOrder();

        Write-Host -ForegroundColor Green "Section $name created/updated successfully!";
    }
    catch
    {
        Write-Host -ForegroundColor Red $_
        Exit 1
    }

No comments:

Post a Comment