Reindex Complete SharePoint Site Collection

In SharePoint Online we do not have the option, to trigger reindexing the complete site collection. The reindex request must be send for each web in the site collection. The following script could be used, when the whole site collection needs to be reindexed.

#
# Parameters
# ==========
# 
# Url : root url of the site collection
# Credentials : username and password to connect with
#

param (
    [string]$Url,
    $Credentials
)

function Iterate-WebCollection
{
   param (
       $WebCollection
   )

    foreach ($web in $WebCollection)
    {
        $webUrl = $web.Url

        Write-Host -NoNewline $webUrl " ... "

        Connect-PnPOnline -Url $webUrl -Credentials $Credentials

        Request-PnPReIndexWeb

        Write-Host -ForegroundColor Green "Done."

        $CurrentWeb = Get-PnPWeb -Includes Webs

        Iterate-WebCollection -WebCollection $CurrentWeb.Webs

        Connect-PnPOnline -Url $webUrl -Credentials $Credentials
    }
}

if ($Credentials -eq $null)
{
    $Credentials = Get-Credential
}

Connect-PnPOnline -Url $Url -Credentials $Credentials

$web = Get-PnPWeb -Includes Webs
$webUrl = $web.Url

Write-Host -NoNewline $Url " ... "

Request-PnPReIndexWeb

Write-Host -ForegroundColor Green "Done."

Iterate-WebCollection -WebCollection $web.Webs

Advertisements

Site logo link to root site

Sometimes it is necessary to have the hyperlink of the logo on the top left link to the root site of our site collection. With a little JavaScript and the PowerShell PnP extensions, this task is pretty easy.

First we need a simple JavaScript that does the modifications, when the page was loaded. We are using jQuery in this case.


$(document).ready(function() {
	$(".ms-siteicon-a").attr("href", _spPageContextInfo.siteServerRelativeUrl);
});

Next we have to upload the script file to the root site of our site collection. And when the file was uploaded, we register a JavaScript link with a site scope. For this task we use the PowerShell PnP extensions.


Add-PnPFile -Path C:\Temp\RewriteLogoHref.js -Folder "SiteAssets/js"

Add-PnPJavaScriptLink -Name "RewriteLogoHref" -Url https://{your-tenant}.sharepoint.com/sites/{your-site}/SiteAssets/js/RewriteLogoHref.js -Scope Site

That’s it. In each subsite we can navigate to the root site of our site collection by clicking the site logo.

Simple Template for Sharepoint Online Command Line Tool

For testing purposes in SharePoint Online (and even SharePoint on-prem) I often use a simple command line tool. Following a simple template as a good starting point.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security;
using Microsoft.SharePoint.Client;

namespace AnyDummy
{
    class Program
    {
        static void Main(string[] args)
        {
            var url = "{add your url here}";
            var user = "{add the username here}";
            var pw = "{add the user password here}";

            var password = new SecureString();
            foreach (char c in pw.ToCharArray()) password.AppendChar(c);

            using (var ctx = new ClientContext(url))
            {
                ctx.Credentials = new SharePointOnlineCredentials(user, password);
				// comment the previous line and use the following line, when running on-premise
				// ctx.Credentials = new System.Net.NetworkCredential(user, password);
				
                Web web = ctx.Web;
                ctx.Load(web);
                ctx.ExecuteQueryRetry();
            }
        }
    }
}

 

Step by step: Line Coloring in SharePoint View

In a blog post by Wes Preston we can see how to implement line coloring for a SharePoint view. In this article, I will show the steps to do, to see the results.

First, I have a simple custom list with just to columns: the Title and a field called “Prio”. Depending on the value of the field “Prio”, the line in the view should be colored in red.

For the line coloring, I use the following script (yes, it’s copied from the post by Wes Preston) and I store the file with the script in a library called “Scripts” in my SharePoint site.

(function () {
	var overrideCtx = {};
	overrideCtx.Templates = {};

	overrideCtx.OnPostRender = [
		HighlightRowOverride
	];

	SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
})();

function HighlightRowOverride(inCtx) {
	for (var i = 0; i < inCtx.ListData.Row.length; ++i) {
		var listItem = inCtx.ListData.Row[i];

		var iid = GenerateIIDForListItem(inCtx, listItem);
		var row = document.getElementById(iid);

		// in this example, the internal name of the field is "Prio". Change the next
		// line depending on the structure of the list
		if (listItem.Prio == "1") {
			if (row != null) {
				row.style.backgroundColor = "rgba(255, 0, 0, 0.5)"; //red
			}
		}
	}

	inCtx.skipNextAnimation = true;
}

Next, I open the default view in my list. It is important that the field needed for coloring is available in this view (in my case the field “Prio”). When the view is opened, I start editing the page. Open the properties of the webpart and enter the JSLink in the properties pane.

It is important to add the ~site token, when the SharePoint site is hosted in SharePoint Online. Apply the changes to the webpart, end editing the page and reload the page. Depending on the data in the list, we will see some of the lines colored.

That’s all. We can use the same script for several lists or views, we only need to change the line, where the value of the field is checked.

How to switch the display language in Office 365

Office 365 and SharePoint Online are multilingual. When a SharePoint Online site is configured for different languages, the user could select his display language in the profile settings. No configuration needed in the browser settings.

When you are in any SharePoint Online site, click the user symbol on the top left and click the “About me” link.

This will open the Delve page for the user. On the Delve page click the “Update Profile” button.

On the update profile page in the bottom you can see the link “How can I change language and regional settings?”. Clicking this link will display a short hint. In this hint click the “here” link.

A new tab will open in the browser displaying the details page from the user profile. Click the “…” on top and choose “Language and Region”.

On top of the page you find the section for the language preferences. Using the “Pick a new language” combobox you can select the preferred language. You can also remove the actually selected language.

When finished, click the “Save all and close” button at the bottom of the page.

That’s all. It might take some time, until the new setting is active.

Prepare Deployment of Azure Web Jobs

An Azure Web Job could be a simple command line program. Creating such a piece of software in Visual Studio is more than simple. To deploy the program as a Web Job in a Web App in Azure, we just need a zip-file with the content of the bin\release-folder. To automate the creation of this step to prepare the deployment package, we just need a simple PowerShell script and a post-build event in Visual Studio.

The PowerShell script will look like this:

param (
	$Path,
	$DestinationPath,
	$ZipFile
)

Write-Host "Path: $Path"
Write-Host "DestinationPath: $DestinationPath"
Write-Host "ZipFile: $ZipFile"

If ((Test-Path $DestinationPath) -eq $false)
{
	Write-Host "Create path $DestinationPath"

	New-Item -ItemType Directory -Path $DestinationPath | Out-Null
}

Compress-Archive -Path $Path -DestinationPath $DestinationPath\$ZipFile -Force

Write-Host -ForegroundColor Green "Done."

Place the PowerShell script to the root folder of the project in Visual Studio.

The post-build command line will just call this script to create the package and store it in a folder in the project.

To make copy & paste easier, the post-build command line (do not copy the hash sign, just the single line of text):

#
if $(ConfigurationName)==Release (PowerShell -Command "..\..\CreateDeploymentPackage.ps1 -Path '$(TargetDir)*.*' -DestinationPath '$(ProjectDir)ZipPackage' -ZipFile $(TargetName).zip" )
#

The zip-file for the deployment to the Web Job in Azure will be stored in a sub-folder of the project. Just grab this file and use another PowerShell script or the manual way to deploy the Web Job.

That’s it.

Recreate Site Collection in SharePoint Online

During development I often need to recreate a site collection in a SharePoint Online tenant. Usually this is simply deleting the old site and creating the site again. The following script would automate these steps, using the Office 365 PnP PowerShell extensions.

But be careful, the existing site collection will be removed without any confirmation.

When the site collection was created, the Connect-PnPOnline cmdlet is called to connect to the new site collection.

param (
	[Parameter(Mandatory=$true)]
    [string]$Tenant,
	[Parameter(Mandatory=$true)]
    [string]$WebUrl,
	[Parameter(Mandatory=$true)]
	[string]$Title
)

$url = "https://$Tenant.sharepoint.com/sites/$WebUrl"
$owner = "admin@$Tenant.onmicrosoft.com"
$lcid = 1033
$template = "STS#0"
$timezone = 4
$resourceQuota = 10
$storageQuota = 1

Try
{
	Write-Host -ForegroundColor Yellow "Checking, if site already exists..."
	
    $site = Get-PnPTenantSite -Url $url -ErrorAction SilentlyContinue
}
Catch
{
}

if ($site -ne $null)
{
    Write-Host -ForegroundColor Yellow "Site already exist, removing current site..."

    Remove-PnPTenantSite -Url $url -Force
	
	Write-Host
}

Write-Host -ForegroundColor Yellow "Creating new site..."

New-PnPTenantSite -Title $Title -Url $url -Owner $owner -Lcid $lcid -Template $template -TimeZone $timezone -ResourceQuota $resourceQuota -StorageQuota $storageQuota -Wait -RemoveDeletedSite

Write-Host

Connect-PnPOnline -Url $url

Write-Host -ForegroundColor Green "Done."