Tag Archives: CSOM

VIDEO – Toggle Required Fields OFF

When making bulk data changes to SharePoint List or Library you may need to temporarily turn off Required fields.   Opening this can allow us to import records, apply changes, and modify metadata without prompting that required fields are missing.   PowerShell below can help disable all Required fields OFF.    Script generates a CSV snapshot of prior configuration during run.    CSV can be used to restore original configuration for which fields are Required.

Cheers shades_smile


PowerShell Code

param (
# Module
Import-Module "SharePointPnPPowerShellOnline" -ErrorAction "SilentlyContinue" | Out-Null
# Config
$appid = "APP ID HERE"
$appsecret = "APP SECRET HERE"
function Main() {
    # Connect
    Connect-PnPOnline -Url "https://tenant.sharepoint.com/" -AppId $appid -AppSecret $appsecret 
    $ctx = Get-PnPContext
    $list = Get-PnPList "Test"
    if ($restoreFilename) {
        # ENABLE Required Fields
        $csv = Import-Csv $restoreFilename
        $fields = Get-PnPField -List $list
        foreach ($row in $csv) {
            $f = $fields | ? { $_.Id -eq $row.Guid }
            $f.Required = $true
    else {
        # DISABLE Required Fields
        $coll = @()
        $guid = (New-Guid).ToString()
        $fields = Get-PnPField -List $list
        foreach ($f in $fields) {
            if ($f.Required) {
                Write-Host "CHANGED FIELD $($f.Title) NOT REQUIRED"
                $f.Required = $false
                $coll += $f.Id
        $coll | Export-Csv "PNPToggleRequiredField-$guid.csv"
# Main

GitHub Link


PowerShell – Create SharePoint Alert remotely (CSOM)

I want to migrate user alerts to Office 365 from SharePoint 2013 on-prem and was surprised to learn there is no client side API for creating Alerts.   Not SOAP, not REST, nothing but the web browser ASPX web form GUI.

So I took Fiddler and inspected the HTTP POST traffic when you click “OK” on the SharePoint “SubNew.aspx” page in the browser.   Lots of great detail!    You can see all the ASPX web form controls and internal values.   Clicking the “OK” button posts that event back to the server side Dot Net for processing with Request Digest, ViewState, and lots of cool stuff.

So …. could we simulate sending that same HTTP traffic?   From somewhere else?    Like PowerShell??    That’d be awesome!


Below is the working concept. PowerShell function with a handful of sample calls. Immediate alert, Daily, Weekly scheduled, etc. With various recipients and filters. Enjoy!  shades_smile










Code  (CreateSPAlert.ps1)

Function CreateSPAlert($webURL, $listID, $userLogin, $userDisplayName, $alertTitle, $changeType, $sendAlertsFor, $whenToSend, $whenToSendDay, $whenToSendTime) {
    ASPNet WebForm Input Values
    -1 All changes
     1 New items are added
     2 Existing items are modified
     4 Items are deleted
    0 'Anything changes'
    1 ''
    2 ''
    3 ''
    0 immediate
    1 daily
    2 weekly
    0 Sunday
    1 Monday
    2 Tuesday
    3 Wednesday
    4 Thursday
    5 Friday
    6 Saturday
    12:00 AM
    1:00 AM
    11:00 AM
    12:00 PM
    1:00 PM
    11:00 PM
    # Convert parameters
    switch ($sendAlertsFor) {
        '1' {$sendAlertsFor=''}
        '2' {$sendAlertsFor=''}
        '3' {$sendAlertsFor=''}
        default {$sendAlertsFor='Anything changes'}
    # List Scope
    $url = "$webURL/_layouts/15/SubNew.aspx?List=$listID"
    $fields = Invoke-WebRequest -Uri $url -UseDefaultCredentials -UseBasicParsing  | select -ExpandProperty inputfields | select name, value
    # HTTP POST Body WeBforms
    $postParams = @{
    '__REQUESTDIGEST' = ($fields|? {$_.Name -eq '__REQUESTDIGEST'}).Value;
    '__VIEWSTATE'=($fields|? {$_.Name -eq '__VIEWSTATE'}).Value;
    '__EVENTVALIDATION'=($fields|? {$_.Name -eq '__EVENTVALIDATION'}).Value;
	# Append if needed
	if ($whenToSendDay) {
	if ($whenToSendTime) {
    # Execute HTTP
    $response = Invoke-WebRequest -Uri $url -WebSession RequestForm -Method POST -Body $postParams -ContentType 'application/x-www-form-urlencoded' -UseBasicParsing
# Main
$start = Get-Date
# Create SP alerts
CreateSPAlert 'http://sharepoint/sites/test' '%7B0D568A08%2DD7AA%2D405B%2DADE1%2DF324216C0272%7D' 'i:0#.w|company\\jsmith' 'John Smith' 'Documents' '-1' 'Anything changes' '0'
CreateSPAlert 'http://sharepoint/sites/test' '%7B0D568A08%2DD7AA%2D405B%2DADE1%2DF324216C0272%7D' 'i:0#.w|company\\jsmith' 'John Smith' 'Documents' '4' 'Anything changes' '0'
CreateSPAlert 'http://sharepoint/sites/test' '%7B0D568A08%2DD7AA%2D405B%2DADE1%2DF324216C0272%7D' 'i:0#.w|company\\jsmith' 'John Smith' 'Daily-Documents' '4' 'Anything changes' '1' '0' '7:00 PM'
CreateSPAlert 'http://sharepoint/sites/test' '%7B0D568A08%2DD7AA%2D405B%2DADE1%2DF324216C0272%7D' 'i:0#.w|company\\jsmith' 'John Smith' 'Weekly-Documents' '4' 'Anything changes' '2' '5' '5:00 PM'
# Run duration
$end = (Get-Date) - $start
$ms = $end.TotalMilliseconds
"$ms MS duration"




FIXED – 403 ExecuteQuery CSOM from a SharePoint Server

I came across this error when running CSOM requests to Office 365.   While troubleshooting the CSOM call worked beautifully from “powershell_ise” but not regular “powershell.”  Fiddler monitoring showed the cmdlet making HTTP traffic correctly with login handshake from ISE.    However, the regular “powershell” window did not attempt any login handshake.

Strange?   Definitely.

The root cause was “$profile” loading the Server Object Model (SOM) cmdlets before CSOM DLL were loaded.   Why would that matter?  How could it cause a CSOM issue?   There seems to be a DLL namespace overlap internally between these plugins (SOM, CSOM, SPO) so loading sequence matters.  A lot.

By proactively loading the CSOM DLL in $profile before SOM, everything worked correctly from both ISE and the regular PowerShell console.

Hope this helps!  shades_smile



  1. Run CSOM request in PowerShell
  2. From a SharePoint Server on premise (2013 here)
  3. Which also has Microsoft SharePoint Online (SPO) cmdlets installed (https://www.microsoft.com/en-us/download/details.aspx?id=35588)
  4. See this error
  5. Exception calling “ExecuteQuery” with “0” argument(s):  “The remote server returned an error:  (403) Forbidden.






  1. Open PowerShell and type “notepad $profile”
  2. Ensure below code is present.
  3. NOTE – CSOM must load before SOM (Server Object Model) for requests to execute correctly.   Workaround for an internal Microsoft naming overlap.  Both are probably using the same object somewhere.  Loading CSOM first allows CSOM to reserve the namespace first.


Code [$profile]

#CSOM first
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime") | Out-Null
Add-PSSnapIn Microsoft.SharePoint.PowerShell



Code [csom-only-test.ps1]

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime") | Out-Null
$url = "https://tenant.sharepoint.com/sites/team"
$user = "admin@tenant.onmicrosoft.com"
$pass = "password"
$secpw = $pass | ConvertTo-SecureString -AsPlainText -Force
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($user, $secpw)
$ctx.Credentials = $cred
$site = $ctx.Site
if (!$site.TrimAuditLog) {
    $site.TrimAuditLog = $true
    $site.AuditLogTrimmingRetention = 180
Return to Top ▲Return to Top ▲