Caml query to find all items in a list

To get all list items in a list (or library) in SharePoint Online with the client side object model, a simple caml query is needed. I usually use this one:


<View>
  <Query>
    <Where>
	  <Gt>
	    <FieldRef Name='ID' />
		<Value Type='Number'>0</Value>
      </Gt>
	</Where>
  </Query>
</View>

Advertisements

Simple solution to remove the gear icon in SharePoint Online

The gear icon in SharePoint Online is the key to a lot of functionality in SharePoint Online, like Site Content or the Site Settings. But sometimes the owner of a site wants to have this disabled. Because SharePoint is a web application, we can do a lot using JavaScript.

So the script to remove the gear icon can look like this:


// make the gear icon on the top right invisible for users, who do not have
// manage permissions for the web.
// the solutions assumes that jQuery is available

$(document).ready(function() {
	if (_spPageContextInfo.hasManageWebPermissions == false) {
		setInterval(function() {
			$("#O365_MainLink_Settings.o365cs-nav-item.o365cs-nav-button.o365cs-topnavText.ms-bgc-tdr-h.o365button").hide();
		}, 500);
	}
});

In the script we check, whether the user has the permission to manage the web. When not, the gear icon is removed.

To make this script available in all pages within a site collection we just need some PowerShell scripting that makes use of the PowerShell PnP extensions. This example will store the script file in a library called "Scripts" and is assuming that this library already exists.


Write-Host "Upload RemoveGearIcon.js"

$f = Add-PnPFile -Path .\RemoveGearIcon.js -Folder "scripts"

$site = Get-PnPSite
$fileUrl = $site.Url + "/Scripts/RemoveGearIcon.js"

Write-Host "Add JavaScript Link"

$jsLink = Get-PnPJavaScriptLink -Name "RemoveGearIcon" -Scope Site

if ($jsLink -ne $null)
{
	Remove-PnPJavaScriptLink -Identity "RemoveGearIcon" -Scope Site -Force
}

$link = Add-PnPJavaScriptLink -Name "RemoveGearIcon" -Url $fileUrl -Sequence 200 -Scope Site

That’s all. The result will look like this:

Which template was used for my SharePoint Online site?

When in the web browser, we are able to get information about the used template during site creation. Just open the console in the developer tools and enter

_spPageContextInfo.webTemplateConfiguration

This will show up the web template and the used configuration.

Create SharePoint Content Type with Document Template

With the PnP PowerShell extensions for SharePoint Online it’s very easy to create a new content type by using the Add-PnPContentType cmdlet. But sometimes it is necessary to assign a new document template to the content type (or an existing one). This could be done using Client-site Object Model within the PowerShell Script as shown in the following script.


$filePath = ".\Document Template\DocumentTemplate.docx"
$filename = "DocumentTemplate.docx"

$serverRelativeSiteUrl = "/sites/my-demo"
$ctName = "Any new Document"

# create the content type
$ct = Add-PnPContentType -Name $ctName -ContentTypeId 0x0101006604da7f262243448cb56226f4f30c79 -Group "Tester" -Description "No description available."

# upload the document template to the corresponding folder of the content type (site relative url)
$f = Add-PnPFile -Path $filePath -Folder "/_cts/$ctName"

# get the content type object
$ct = Get-PnPContentType -Identity $ctName 

# set the document template in the content type to the uploaded file and update in the content database
$ct.DocumentTemplate = $filename
$ct.Update($true)
(Get-PnPContext).Load($ct)
Invoke-PnPQuery

# that's it

The result in the web view of the settings of the content type will look like this:

Keep it simple .

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

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();
            }
        }
    }
}