Tuesday, April 23, 2013

Restricting People Picker to search users within site collection


Hi,

We had requirement i.e. if any user searches any user id through people picker, it has to populate only if the user id exists in the current site collection.

For this we have run the below power shell script and we achieved it
Later we have rolled back i.e. if we user searches any user id, people picker should populate if user id exists in Active directory
Through power shell script, we have rolled back as below

Finally script has been delivered as below
//RemovePeoplePickerRestrictions.bat

cd /d %~dp0
@echo off
powershell.exe -noexit .\PSRemovePeoplePickerRestrictions.ps1 .\RemovePeoplePickerRestrictions.csv

//RemovePeoplePickerRestrictions.csv

WebAppUrl
https://webappurl/

//PSRemovePeoplePickerRestrictions.ps1

#///<summary>
#/* Script Name : PSRemovePeoplePickerRestrictions
# * Purpose : Used to remove the peoplepicker restrictions and to connect peoplepicker to AZ AD
#///</summary>

#-----Input parameters to the script
param($inputFile)

#------To check if Sharepoint cmdlets are registered are registered
if ( (Get-PSSnapin -Name Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue) -eq $null )
{
Add-PSSnapin Microsoft.SharePoint.Powershell
}
#---------------------------------------------------------------------------------------------------

#-----------------------------------------Start Logging---------------------------------------------
$filepath = $MyInvocation.MyCommand.Definition                                      
$directorypath = [System.IO.Path]::GetDirectoryName($filepath)
$LogTime = Get-Date -Format yyyy-MM-dd_h-mm
$LogFile = $directorypath + "\RemovePeoplePickerRestrictions_Log_$LogTime.txt"
Start-Transcript -Path $LogFile -Force
#---------------------------------------------------------------------------------------------------

#---------------------------------------------------------------------------------------------------
# Function to add the Membership tab to my site
function RemovePeoplePickerRestrictions()
{
    param([string] $webAppUrl)

$WebApp = Get-SPWebApplication -Identity $webAppUrl

if($WebApp -ne $null)
{
Set-Alias -Name stsadm -Value $env:CommonProgramFiles"\Microsoft Shared\Web Server Extensions\14\BIN\STSADM.EXE"

#The following script will remove the restriction from people picker thus search from AZ AD...
stsadm -o setproperty -propertyname peoplepicker-onlysearchwithinsitecollection -propertyvalue "" -url $webAppUrl

#The following script will resolve the users within the site collection
$WebApp1 = Get-SPWebApplication -Identity $webAppUrl
$WebApp1.PeoplePickerSettings.PeopleEditorOnlyResolveWithinSiteCollection = $false;
$WebApp1.Update();
}
else
{
Write-Host 'Not a valid Web application url...'
}
}
#---------------------------------------------------------------------------------------------------

#-----------------------------------------------------------------------------------
#loop through a csv file
Import-Csv $inputFile | ForEach{                            
    $webAppUrl = $_.WebAppUrl
               
    try
    {
write-host "Removing the peoplepicker restrictions from $webAppUrl..."
        RemovePeoplePickerRestrictions $webAppUrl
write-host "Completed..."
    }
    catch
    {
write-host "Completed with errors..."
        Write-Output $_
    }
}
#---------------------------------------------------------------------------------------------------

#-----------------------------------------End Logging-----------------------------------------------
Stop-Transcript
#---------------------------------------------------------------------------------------------------


No comments:

Post a Comment