Wednesday, September 30, 2020

Copy all views from one SharePoint list to another via Powershell

 Here's a PnP Powershell script that copies all views from one SharePoint list to another list:

# Url to the site
$siteUrl = ""

# The list where we want to copy all of the views
$listWithViewsToCopy = "Banana"

# The list name to copy the views to
$listToCopyTo = "Pineapple"

Write-Host "Connecting to PnP..."
Connect-PnPOnline -Url $siteUrl -Credential (Get-credential)

# Get all the views and properties for the list
$viewsToCopy = Get-PnPView -List $listWithViewsToCopy -Includes "ViewQuery","ViewType","ViewData","ViewJoins","ViewProjectedFields","RowLimit","Paged"

# Loop through each view and create it in the new list
foreach ($view in $viewsToCopy)
   Write-Host "Adding View: "$view.Title
   # Loop through all fields and create an array

   $fields = [System.Collections.ArrayList]::new()

   foreach ($field in $view.ViewFields)

   # Add the new view
   Add-PnPView -List $listToCopyTo -Title $view.Title -Fields $fields -Query $view.ViewQuery -ViewType $view.ViewType -RowLimit $view.RowLimit

Thursday, June 11, 2020

Grant access to an item or folder: Add a SharePoint Group

In Power Automate, if trying to grant access to an item or a folder, and you try to use a SharePoint Group, it won't resolve and you'll get an error that the field is required.

But if you just Initialize a variable and assign it the name of the SharePoint Group:

And then use that variable instead:

You no longer get an error, and the group will get added with the access level specified when you run the flow!

Monday, June 8, 2020

Get-PnPListItem in a list with greater than 5000 items

If you need to grab all the items from a list with greater than 5000 items in SharePoint Online, you might get the following error:

The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator

To get around that, add a query with the scope of RecursiveAll to your query like so:

Connect-PnpOnline -Url '' -Credentials (Get-Credential)
$query = "<View Scope='RecursiveAll'><RowLimit>5000</RowLimit></View>"
$items = Get-PnPListItem -List 'My List' -Query $query

Thursday, October 3, 2019

Display all the sites a user can access

I've heard many people ask for a place to view all the sites they have access to.  Using the content search web part you can accomplish this and have a page that by using search, which returns unique results for each user based on their permissions, each user gets a unique list of the sites they can access that looks something like this:

The following steps describe how to create this:

Upload the template files and css
First, download the the code from the following in github:

Open up SharePoint Designer and select on All Files in the left hand navigation.  Then go to /_catalogs/masterpage/Display Templates/Content Web Parts.  Copy Control_Sites_Directory.html and Item_Sites_Directory.html here.  Edit the url in these files (where it says "") to your url.

Go to Site Assets, and create a Styles folder (if not there already), and copy SitesDirectory.css there.

Select the templates
First, add a content search web part to your page and edit it.  In the dropdowns, select the newly added Sites Directory for the Control and Item Sites Directory for the item.

Setup the web part query
edit it, hit the Change Query button, Switch to Advanced Mode and paste in the following "contentclass:STS_Site" into the Query text box

You might want to remove some sites/paths from the query as needed by using the Property filter (e.g. -Path:  If you want to add subsites as well, make the query text (contentclass:STS_Site OR contentclass:STS_Web).

And that should be it!

NOTE:  If some sites aren't showing, just kickoff a re-index of the site in Site Settings  -> Search and offline availability...

The paging functionality was referenced from Sean McDonough's blog here:

Tuesday, June 5, 2018

Task Approval by Email Replies

This post details how to setup a script to process emails, and setup your workflows to send emails that can be processed by the script, so that end users can ultimately just reply to an email and it will get processed and close the task.


We'll setup a script as a scheduled task, that will check a designated inbox for emails to process.  It will scan the body of those emails for keywords to approve or reject.

We'll then take a workflow task process, and update the email to have an approve and a reject mailto link, which will open up an email, which when a user sends, will get processed and will update and close the task.

Step 1:  Setup Email Inbox

The first step is to create an email inbox that we'll use for all processing.  In this inbox, create three folders:
  • Email Approvals
  • Email Approvals Processed
  • Email Approvals Not Processed
Create an inbox rule that will move the item to the Email Approvals folder if the subject begins with "REPLY:"

This Email Approvals folder will be the folder our script will check to process any emails that arrive there.

Step 2:  Setup the script

Download the script from github, and change the necessary variables to local paths on your machine.  Set it up on the server that it will be running on.

Step 3:  Create Encrypted Credentials 

The script assumes there are two users, one that has the required permissions to update the task in SharePoint ($UserSharePoint), and one user for the mailbox where the emails are being sent to ($UserEmail).  Create encrypted files for each by following the instructions here, and make sure to match the name and path of the file to the $CredsFileSharePoint and $CredsFileEmail in the script.

Step 4:  Create a Scheduled Task

Setup a scheduled task to run the script, and under Actions set the following:

Program/script:  C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Add Arguments:  "F:\Scripts\EmailApprovals\Process-Email-Approvals.ps1"
Start in:  F:\Scripts\EmailApprovals\

Step 5:  Create a Workflow Task Process

Next create a workflow with a task process.  For the email option, use the following as a template:

Email to user (sent from workflow)

Subject:  ACTION REQUIRED:  PO 98451 Change Request
Body:  A new PO Change Request has been submitted and requires your approval:

Title: Requesting a new phone 
PO Number: 98451
PO Owner: Jane Doe 
Original Amount: $699.00 
New Amount: $799.00 
Description: ​Upgraded the model

Please either approve or reject the request below:

Approve         Reject


Mailto Link Sample

In each email from the workflow, we have a mailto link for approving and rejecting, which should resemble the following.$699.00%0D%0ANew%20Amount:%20$799.00%0D%0ADescription:%20%E2%80%8BUpgraded the model%0D%0A%0D%0A______________________________%0D%0A%0D%0AFor%20Processing%0D%0A%0D%0Aurl=

Mailto Response Email

Note that the script keys off of certain words, so if “approve”, “acknowledge” or “yes” is in the body, it will be approved, if “reject” is in the body text, it will be rejected.

Subject: REPLY: PO Change Request
Body:  I approve the following PO Change Request.

Title: Requesting a new phone 
PO Number: 98451
PO Owner: Jane Doe 
Original Amount: $699.00 
New Amount: $799.00 
Description: ​Upgraded the model
For Processing


The following should be in place for the process to work properly:
  • The link should be to a Tasks lists (as the script is updating task columns)
  • The body of the email received must contain either (case insensitive):
    ·       Approve
    ·       Acknowledge
    ·       Yes
    ·       Reject
  • The body must also contain a "url=" with a link to the task item in the format, which is typically included at the end of the email.
  • All processing is written out to a log to review any potential issues.  A new file is created each time the process is run, and this folder can be regularly cleaned up.

Using Encrypted Credentials in PowerShell

*** NOTE: This must be done when logged into the computer running the program, as the user you’ll be running the program as. E.g. if your Scheduled Task is running as sys_admin, you must be logged in as sys_admin on the machine that will be running the scheduled task before encrypting the credentials ***

Create the Encrypted File

First create the encrypted text file by running the following:

$cred = Get-Credential
$cred.Password | ConvertFrom-SecureString | Out-File .\adcreds.txt

Using the Encrypted Credentials

To use the credential file in powershell you can use:

$User = ""
$File = "C:\Development\Vivity\adcreds.txt"
$cred = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)


If you change the machine or account that this runs as, you will have to recreate this file!


Thursday, May 31, 2018

Using jQuery for Validation

If you want to make a column required based on non-standard column validation, you can do so via jQuery by creating a function to make the field red and disable the Save button like so:

function MakeFieldRequired(fieldId) {
    $("input[value$='Save']").attr('disabled', true);
    $("span[id^='" + fieldId+"']").css('color','red').css('font-weight','bold');