Thursday, October 27, 2016

Copy Permissions from One SharePoint Site to Another using PowerShell

Requirement: Copy permissions from one site to another in SharePoint!

Solution: If you want to copy one SharePoint site to another site, there is no OOTB ways! However, You can use PowerShell to copy permissions between sites. Here is my PowerShell script to copy site permissions.

Copy Permissions from one site to another using PowerShell:
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
 
#PowerShell Function to copy permissions from one site to another
Function Copy-SitePermissions()
{
 param(
     $SourceWebURL,
     $TargetWebURL
 )
 Try {
  #Set the Error Action
    $ErrorActionPreference = "Stop"
  
  #Get the Source and Target Webs
  $SourceWeb = Get-SPWeb $SourceWebURL
  $TargetWeb = Get-SPWeb $TargetWebURL
  
  #if permissions are Inherited in the target, Break it!
  if($TargetWeb.Permissions.Inherited)
  {
   #Reset the Inheritence in Target Web
   $TargetWeb.BreakRoleInheritance($false)
  }
   #Copy permissions from Source to Target Web
     $SourceWeb.RoleAssignments | foreach-object {
     $TargetWeb.RoleAssignments.Add($_)
     }
    $TargetWeb.Update()
    Write-Host "Permissions copied from Source Site to the Target!" -f Green
 }
 catch {
   Write-Host $_.Exception.Message -ForegroundColor Red
 }
 finally {
  #Reset the Error Action to Default
  $ErrorActionPreference = "Continue"
 }
}

#Call the function to copy Web permissions 
Copy-SitePermissions -SourceWebURL "http://portal.crescent.com/ProjectHub/" -TargetWebURL "http://portal.crescent.com/TaxAudits/" 


You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Sunday, October 23, 2016

Customize Suitebar Text and Link in SharePoint 2016 using PowerShell

Requirement:
By default, SharePoint 2016 comes with "SharePoint" as the suitebar branding text.  What if you would like to change it to something meaningful, say "Your Company Intranet"? Well, PowerShell can help to customize the branding text, Logo and URL in SharePoint 2016.
sharepoint 2016 add link to suite bar powershell
PowerShell Script to change Suitebar navigation text and link:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$WebApp= Get-SPWebApplication "http://intranet.crescent.com"
$WebApp.SuiteNavBrandingText = "Crescent Portal"

#Blank image from Layouts folder
$webapp.SuiteNavBrandingLogoUrl = "/_layouts/images/dot_ruleper.gif"
$webapp.SuiteNavBrandingLogoTitle = "Crescent Logo"
$webapp.SuiteNavBrandingLogoNavigationUrl = "http://portal.crescent.com"
$webapp.Update()

Here is the result of customized SharePoint 2016 suite bar branding using PowerShell!
customizing the sharepoint 2016 suite bar branding using powershell
My another post Customizing the SharePoint 2013 suite bar branding using PowerShell

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Monday, October 10, 2016

Add-Remove User to SharePoint Multi-Valued People Picker Field using PowerShell

Requirement: 
We've a SharePoint list called "Projects" with 1000's of items. The list has a field called "Team Members" which allows multiple user values. We often get a requirement to either add or remove a particular user to all items or specific items filtered by other columns in the list.

Solution: Adding-removing user from multiple items in a bulk can be achieved with PowerShell. Lets use PowerShell to add or remove user from SharePoint people picker (person or group) field value.
powershell to add remove user to people picker column value in sharepoint


PowerShell to Add new user to Person or Group Field:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Configuration parameters
$SiteURL = "http://portal.crescent.com/projects/"
$ListName = "Projects"                
$FieldName= "TeamMembers"
$UserToAdd="Crescent\Sam"

#Get site and List objects
$web = Get-SPWeb $SiteURL
$List = $web.Lists.TryGetList($ListName)

#Item Ids to update
$IDColl= @(4,5,7,14,55,169,258,260,261)

#Iterate through each item
foreach($ItemID in $IDColl)
{
    #Get the Item
    $Item = $List.GetItembyID($ItemID)
 Write-Host "Processing: "$item["ProjectName"]
    
    #Get Existing field value
    $MultiUserCollection = [Microsoft.SharePoint.SPFieldUserValueCollection]$item[$FieldName]
          
    #Prepre the user to Add
    $User = $Web.EnsureUser($UserToAdd)

 #Add new user to the collection
 $NewUser = new-object Microsoft.SharePoint.SPFieldUserValue($Web, $User.ID,$User.Name)
 $MultiUserCollection.Add($NewUser)

 #Update the field value
 $item[$FieldName] = $MultiUserCollection
 $item.update()   
 
 write-host "Team Member Added!"     
}

PowerShell to remove user from Multi-user People Picker field:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Configuration parameters
$SiteURL = "http://portal.crescent.com/projects/"
$ListName = "Projects"                
$FieldName= "TeamMembers"
$UserToRemove="Crescent\Sam"

#Get site and List objects
$web = Get-SPWeb $SiteURL
$List = $web.Lists.TryGetList($ListName)

$IDColl= @(184,259,281,282,306,318,331,378,404,410)
foreach($ItemID in $IDColl)
{
        #Get the Item
        $Item = $List.GetItembyID($ItemID)
        Write-Host "Processing: "$item["ProjectName"]
        
        #Get Existing field value
        $MultiUserCollection = [Microsoft.SharePoint.SPFieldUserValueCollection]$item[$FieldName]
        $NewUserCollection = new-object Microsoft.SharePoint.SPFieldUserValueCollection
       
        #Prepre the user to remove
        $User = $Web.EnsureUser($UserToRemove)
  
        #Create a new collection - exclude user to remove      
        Foreach($MultiUser in $MultiUserCollection)
        {
            if($MultiUser.User.LoginName -ne $User.LoginName)
            {
                #Add user to new collection
                $NewUser = new-object Microsoft.SharePoint.SPFieldUserValue($Web, $MultiUser.User.ID,$MultiUser.User.Name)
                $NewUserCollection.Add($NewUser)
            }
         }
        #Update the list item 
        $item[$FieldName] = $NewUserCollection
        $item.update()   
  
        write-host "User Removed From Existing People Picker field Value!"     
 }


You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Monday, October 3, 2016

Update SharePoint List Items from CSV File using PowerShell

Requirement: Update SharePoint List Items from CSV file.

Bit background: We've a list with 1000's of rows of organization wide experts. Later we added a new field for "Department". Now the requirement is to update Department field for specific list of users. While the datasheet view can be used for bulk editing, filtering and editing 100's of rows would be time consuming. So the solution is: Have list of user names and their departments in CSV file format, give it to PowerShell!

PowerShell Script to Read from CSV file and Update SharePoint List Items:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Read the CSV file
$CSVData = Import-CSV -path "C:\UserData.csv" 
 
#Get the Web
$web = Get-SPWeb -identity "http://portal.crescent.com"
 
#Get the Target List
$List = $web.Lists["ExpertProfiles"]
 
#Iterate through each Row in the CSV
foreach ($Row in $CSVData) 
{
    #Get the List Item matching "Name" field in the CSV    
    $Item = $List.Items | Where-Object { $_["Title"] -eq $Row.Name }

    if($item -ne $null)
    {
 #Update List Item - Internal Name!
        $item["Department"] = $row.Department
        $item.Update()
        Write-Host "Updated:"$row.Name -ForegroundColor Green
    }
    else
    {
        write-host "No matching Item Found for:"$row.Name -f Red
    }
}
While the above code serves the purpose, Lets optimize it with CAML-SPQuery

PowerShell to Update SharePoint List from CSV:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Read the CSV file
$CSVData = Import-CSV -path "C:\UserData.csv" 
 
#Get the Web
$web = Get-SPWeb -identity "http://portal.crescent.com"
 
#Get the Target List
$List = $web.Lists["ExpertProfiles"]
 
#Iterate through each Row in the CSV
foreach ($Row in $CSVData) 
{
    #Filter using CAML Query
    $CAMLQuery="<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>$($Row.Name)</Value></Eq></Where>"
    $SPQuery=New-Object Microsoft.SharePoint.SPQuery
    $SPQuery.ViewAttributes = "Scope='Recursive'"  #Get all items including Items in Sub-Folders!
    $SPQuery.Query=$CAMLQuery
    $SPQuery.RowLimit = 1 
     
    #Get the List item based on Filter 
    $Item=$List.GetItems($SPQuery)[0]

    If($Item -ne $null)
    {
        #Update List Item
        $Item["Department"] = $Row.Department
        #$item.Update()
        Write-Host "Updated:"$row.Name -ForegroundColor Green
    }
    else
    {
        write-host "No matching Item Found for:"$row.Name -f Red
    }
}
Update People Picker Field Value from CSV file:
In an another case, I had to update people picker field value:
 #If the matching project found
    If($Item -ne $null)
    {
        #Update Team members Multi-People picker field
  $TeamMembersList = new-object Microsoft.SharePoint.SPFieldUserValueCollection
  $TeamMembers = $row."TeamMembers" -split ';'
  foreach ($TeamMember in $TeamMembers)
  {
   if ($TeamMember -ne $null) 
   {
    #Prepre the user to add
       $User = $Web.EnsureUser($TeamMember)
       $NewUser = new-object Microsoft.SharePoint.SPFieldUserValue($Web, $User.ID, $User.LoginName)
    $TeamMembersList.Add($NewUser)
   }
  }
  #Update Team members field
  $item["Team Members"] = $TeamMembersList
  $item.Update()
        Write-Host "Updated:"$Row.ProjectName -ForegroundColor Green
    }
    else
    {
        write-host "No matching Item Found for:"$row.ProjectName -f Red
    } 

Related Posts:


You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


You might also like:

Related Posts Plugin for WordPress, Blogger...