SharePoint (2003 thru Online): May 2024

Thursday, May 9, 2024

Add/Remove sites (bulk list) from Search Index thru PowerShell

 We need to remove 150 sites from Search Index. We have to make sure they don't show up in search results by turning off that option for each site. The normal way of doing this is too slow for so many sites. We use a faster PowerShell script method to handle 150 sites at once.

Regular method

By default, the option Allow this site to appear in Search results is set to Yes for all Site collections in Sharepoint online. This means the site content can show up in search results.

On the site, select Settings (Wheel) icon., and then select Site settings. If you don't see Site settings, select Site information, and then select View all site settings.

Under Search, click Search and offline availability.

In the Indexing Site Content section, under Allow this site to appear in Search results, select Yes to allow the content of the site to appear in search results.


If you don't want the content to show up in search results, choose No.

NOTE: Search results are always security trimmed, so users will only see content they have permission to see.


PowerShell Script Method

List all the sites in a CSV file with URL as Header (As shown below).


# symbol in PowerShell script is used for comments.

# Connect to Admin Center
$adminSiteUrl = "https://gurram-admin.sharepoint.com/"
$adminConnection = Connect-PnPOnline -Url $AdminSiteUrl -Interactive

$CSVImport = "D:\Sites.csv"
$SitesCollections = Import-CSV -Path $CSVImport
 
ForEach($Site in $SitesCollections)
   {
         $siteCollectionConnection = Connect-PnPOnline -Url $Site.URL -Interactive
         Set-PnPSite -Identity $Site.URL -DenyAndAddCustomizePages $false
         $Web = Get-PnPWeb -Connection $siteCollectionConnection
         #Indexing Site Content = Yes(false), = No(true)
         $Web.NoCrawl = $false
         $Web.Update()
         Invoke-PnPQuery
         #To unlock the site (-LockState Unlock), To lock the site (-LockState ReadOnly)
         #we did not use the below option, because we want to delete sites later
         Set-PnPSite -Identity $Site.URL -LockState ReadOnly
         #to know the completed site
         write-host $Site.URL
    }


This script completed successfully in less than 10 minutes.

Monday, May 6, 2024

Add/Remove SharePoint sites (bulk list) to Retention Policy thru PowerShell.

We have a requirement to add 150 sites to the exclusion list of our retention policy. While we have traditionally accomplished this by manually editing the retention policy and adding the sites, this method is not feasible for such a large number of sites.

NOTE: 
Excluding sites from policy requires time, ranging from 10 minutes to 24 hours, to take effect.

Edit Retention Policy > Click on Edit (under Excluded) and add the sites.



Instead, we would like to use a PowerShell script to automate the process. This will allow us to add all the sites quickly and efficiently.

List all the sites in a CSV file with URL as Header (As shown below).

NOTE: I had to exclude 150 sharepoint sites from the retention policy, but it only allows 100 at a time. So I split them into two CSV files, one with 100 and one with 50. Then I ran the Powershell Script for each CSV file separately.


Below is the PowerShell Script.
NOTE: This activity requires certain permissions, such as Compliance Admin or Global Admin.
____________________________________________________________________________________
#Variables
$PolicyName = "FilesRetentionPolicy"
$CSVPath = "D:\DEV_Sites.csv"

#Get the CSV file contents, column URL to the array.
[array]$excludesites = Import-CSV -Path $CSVPath | Select -ExpandProperty URL

#Connect to Compliance Center through Exchange Online Module
Connect-IPPSSession -UserPrincipalName spadmin@gurram.onmicrosoft.com

#Get the Policy
$Policy = Get-RetentionCompliancePolicy -Identity $PolicyName

#Add SPO Sites (array) to Retention Policy
Set-RetentionCompliancePolicy -Identity $Policy.Name -AddSharePointLocationException $excludesites

----------------Use Remove when needed----------------
#Remove SPO Sites (array) from Retention Policy
Set-RetentionCompliancePolicy -Identity $Policy.Name -RemoveSharePointLocationException $excludesites
---------------------------------------------------------------------

#Disconnect the Exchange Online Module
Disconnect-ExchangeOnline
____________________________________________________________________________________
After running the PowerShell Script successfully, you can manually verify the added sites in Retention Policy.


Click on Edit in the above screen. You will see as shown below.