Category: SharePoint Online

Resetting Office 365 Cloud Search Index

From time to time we need to remove all the items out of the Office 365 search index. There are 3 options:

1. Re-index everything on premises and after the indexing completes delete the content sources to trigger a delete crawl to run. Of course re-indexing everything is not efficient, it takes time and if items have been deleted from the on premises content you still run the risk of missing orphans in the Office 365 search index.

2.  Call Microsoft Office 365 support and raise a ticket to ask for an index purge, something that takes time and again is inefficient for the task at hand.

3. Use DeleteAllCloudHybridSearchContent method of PushTenantManager.

More information on DeleteAllCloudHybridSearchContent method can be found here and here.

That was another page in the Chronicles of SharePoint Bits, happy scripting!

SharePoint Online: Using PowerShell to delete alerts in site/sub sites using CSOM

Metalogix Content Matrix is a great tool to migrate from SharePoint on-premises As part of migration to SharePoint Online we had to re-migrate some of the Here is a PowerShell script to delete alerts in SharePoint Online. This PowerShell script will delete all alerts in root web and all sub webs in SharePoint Online site.

####################################################################################################
#
#  Author.......: David Shvartsman
#  Date.........: 09/14/2018
#  Description..: SharePoint Online: Delete alerts in site/sub sites using CSOM
#
#################################################################################################### 
CLS
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" -ErrorAction Stop
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" -ErrorAction Stop

#use the $AlertsExportFile to store existing Alers
$AlertsExportFile = "C:\temp\Alerts.csv"
if (Test-Path $AlertsExportFile) {
    Remove-Item $AlertsExportFile -Confirm:$false
}

$UserName = Read-Host "Please enter User Name"
$Password = Read-Host "Enter a Password" -AsSecureString
$SiteUrl = Read-Host "Please enter SharePoint Online Site URL"

$credentials= New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$Password)

$ctx= New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$ctx.Credentials = $credentials

$web = $ctx.Web
$ctx.Load($web)
$ctx.Load($web.Webs)
$siteUsers=$web.SiteUsers
$ctx.Load($siteUsers)
$siteUsers=$web.SiteUsers
$ctx.Load($siteUsers)
$alerts=$web.Alerts
$ctx.Load($alerts)
$ctx.ExecuteQuery()
Write-Host "Processing $($web.Url)"
#List all Alerts for RootWeb site    
if ($alerts.Count -gt 0) {
    $alerts |SELECT @{Label="SiteUrl";Expression={"$($web.Url)"}},AlertFrequency, AlertTime, AlertType, AlwaysNotify, DeliveryChannels, EventType, Filter, ID, UserID, Status | Export-Csv $AlertsExportFile -Append -NoTypeInformation
}
foreach($alert in $alerts) {
	$web.Alerts.DeleteAlert($alert.ID)
	$web.Update()
	$ctx.ExecuteQuery()
}
#enumerate through seub webs
foreach ($webSub in $web.Webs) {
    Write-Host "Processing $($webSub.Url)"
    $alertsSub = $Null
    $ctxSub= New-Object Microsoft.SharePoint.Client.ClientContext($webSub.Url)
    $ctxSub.Credentials = $credentials
    $webSub = $ctxSub.Web
    $ctxSub.Load($webSub)
    $alertsSub=$webSub.Alerts
    $ctxSub.Load($webSub)
    $ctxSub.Load($alertsSub)
    $ctxSub.ExecuteQuery()
    #List all Alerts for each sub web
    if ($alertsSub.Count -gt 0) {
        $alertsSub |SELECT @{Label="SiteUrl";Expression={"$($webSub.Url)"}},AlertFrequency, AlertTime, AlertType, AlwaysNotify, DeliveryChannels, EventType, Filter, ID, UserID, Status | Export-Csv $AlertsExportFile -Append -NoTypeInformation
    }
    foreach($alert in $alertsSub){        
	    $webSub.Alerts.DeleteAlert($alert.ID)
	    $webSub.Update()
	    $ctxSub.ExecuteQuery()
    }
}

###################################################################################################

That was another page in the Chronicles of SharePoint Bits, happy scripting!

Working with Communication sites Using PowerShell

A SharePoint communication site is a great modern design site that can be used to share news, reports, and other information in a visually compelling format and uses adoptive layout. More information can be found in this Microsoft article: What is a SharePoint communication site?

The template used to create a Communication SharePoint site is SITEPAGEPUBLISHING#0.

The following code will create a Communication Sites using the SharePoint Online Management Shell. A connection to SPO tenant have to be established first using Connect-SPOService command.

####################################################################################################
#
#  Author.......: David Shvartsman
#  Date.........: 05/04/2018
#  Description..: Create a Communication Site 
#
#################################################################################################### 
CLS
# Tenant Configuration
$tenantName = "Tenant Name" 
$AdminURI = "https://$($tenantName)-admin.sharepoint.com"
$PersonalURI = "https://$($tenantName)-my.sharepoint.com"
$TenantURI = "https://$($tenantName).sharepoint.com"

#Site Configuration Parameters
$SiteTitle = "Test Communication Site"
$SiteUrl = "CommunicationTestSite" # Note this URL must be available (check with "/_api/GroupSiteManager/GetValidSiteUrlFromAlias")
$SiteTemplate = "6142d2a0-63a5-4ba0-aede-d9fefca2c767" #"Showcase" = "6142d2a0-63a5-4ba0-aede-d9fefca2c767" and "Blank" = "f6cc5403-0d63-442e-96c0-285923709ffc"
$SiteDescription = "Test Communication Site"
$siteClassification = "LBI"
$siteLCID = 1033

$SiteFullURL = "$($TenantURI)/sites/$($SiteUrl)"

# Communication site creation request
$RequestBody = @{
    request = @{__metadata = @{type ="SP.Publishing.CommunicationSiteCreationRequest"}
    AllowFileSharingForGuestUsers = 'false'
    Classification = $siteClassification
    Description = $SiteDescription
    SiteDesignId = $SiteTemplate
    Title = $SiteTitle
    Url = $SiteFullURL
    lcid = $siteLCID
    }
}
$jsonBody = ConvertTo-Json $RequestBody

$contentType = 'application/json;odata=verbose'

# Get a user based context for SharePoint (app credentials not supported for this approach)
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($TenantURI)
$Context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $SecurePWD)
$Context.ExecuteQuery()

# Get url, cookie and forms digest for authentication
$RequestUrl = "$($TenantURI)/_api/sitepages/communicationsite/create"
$AuthenticationCookie = $Context.Credentials.GetAuthenticationCookie($TenantURI, $true)
$FormsDigest = $Context.GetFormDigestDirect()

$WebSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$WebSession.Credentials = $Context.Credentials
$WebSession.Cookies.SetCookies($TenantURI, $AuthenticationCookie)

$Headers = @{ 
    'X-RequestDigest' = $FormsDigest.DigestValue;
    'accept' = 'application/json;odata=verbose';
    'content-type' = 'application/json;odata=verbose' }

# Call REST API to create new site
try {
    $Result = Invoke-RestMethod -Method Post -WebSession $WebSession -Headers $headers -Body $jsonBody -Uri $RequestUrl -UseDefaultCredentials -ContentType $contentType  
    # Site has been created
    Write-Output "New site created at: $($Result.d.Create.SiteUrl), Status: $($Result.d.Create.SiteStatus)"
}
catch {
    Write-Host "Error creating a site"
}
finally {
    $Context.Dispose()
}

Currently SharePoint Admin Portal does not show Communication Sites. To show a list of all Communication Sites using the SharePoint Online Management Shell use the following code:

####################################################################################################
#
#  Author.......: David Shvartsman
#  Date.........: 05/04/2018
#  Description..: List all Communication Site 
#
#################################################################################################### 
Get-SPOSite -Template SITEPAGEPUBLISHING#0 -Limit ALL

To use CSOM or PnP libraries check out How to list all Communication sites in your tenant internet article.

That was another page in the Chronicles of SharePoint Bits, happy scripting!

Accessing SharePoint online through proxy

The bellow PowerShell script will allow you to access SharePoint Online using PowerShell through proxy:

####################################################################################################
#
#  Author.......: David Shvartsman
#  Date.........: 10/6/2017
#  Description..: Connecting to SharePoint Online thru Proxy 
#
#################################################################################################### 
$proxyString = "http://proxyaddress:proxyport"
$proxyUri = new-object System.Uri($proxyString)
[System.Net.WebRequest]::DefaultWebProxy = new-object System.Net.WebProxy ($proxyUri, $true)
$userName ="user@domain" #SPO User
$SecurePWD = read-host -assecurestring "Enter Password for $userName" 

$credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $userNameProxy, $SecurePWD

[System.Net.WebRequest]::DefaultWebProxy.Credentials = $credential

$tenantName="tenantName"

$AdminURI = "https://$($tenantName)-admin.sharepoint.com"

Try {
    Connect-SPOService -Url $AdminURI -credential $credential
    Write-Host "Connected" -ForegroundColor Green
    Disconnect-SPOService
    Write-Host "Disconnected" -ForegroundColor Green
}
catch {
    Write-Host "Not able to connect" -ForegroundColor Red
    $Error 
}

That was another page in the Chronicles of SharePoint Bits, happy scripting!