CSOM
Csom query All Items More than 5000
On 28/05/2024
$url = "https://test.sharepoint.com/sites/monSitte" # Connect-PnPOnline -Url $url -Interactive Clear-Host $listTitle = "EarthStation_Authorisations" $list = Get-PnPList -Identity "Antenna_Model_Ressources" -ThrowExceptionIfListNotFound $ctx = Get-PnPContext $page = $null $pageNumber = 0; $rowLimit = 3000 $datas = @(); #get first item $reqFirst = "<View
Scope='RecursiveAll'> <Query> <ViewFields> <FieldRef Name='ID' /> </ViewFields> <Where> </Where> <OrderBy> <FieldRef Name='ID' Ascending='TRUE' /> </OrderBy> </Query> <RowLimit>1</RowLimit> </View>" $spqQuery = New-Object Microsoft.SharePoint.Client.CamlQuery # $req123 # $spqQuery.ViewXml = $reqFirst # $itemki = $list.GetItems($spqQuery); $ctx.Load($itemki) $ctx.ExecuteQuery(); $itemId = $itemki[0].ID # get last item $reqFirst = "<View
Scope='RecursiveAll'> <Query> <ViewFields> <FieldRef Name='ID' /> </ViewFields> <Where> </Where> <OrderBy> <FieldRef Name='ID' Ascending='FALSE' /> </OrderBy> </Query> <RowLimit>1</RowLimit> </View>" $spqQuery = New-Object Microsoft.SharePoint.Client.CamlQuery # $req123 # $spqQuery.ViewXml = $reqFirst # $itemki = $list.GetItems($spqQuery); $ctx.Load($itemki) $ctx.ExecuteQuery(); $max = $itemki[0].ID $startDate = Get-Date . ..\common.ps1 Do { $stringBuilder = New-Object System.Text.StringBuilder
$stringBuilder.Append("<View Scope='RecursiveAll'>") | Out-Null $stringBuilder.Append("<Query><Where><And><And><Geq><FieldRef Name='Modified' /><Value Type='DateTime'>1900-12-01T19:49:00Z</Value></Geq><Gt><FieldRef Name='ID' /><Value Type='Counter'>$($itemId)</Value></Gt></And><Leq><FieldRef Name='ID' /><Value Type='Counter'>$($itemId + $rowLimit)</Value></Leq></And></Where>") | Out-Null # $stringBuilder.Append("<Query><Where><And><And><Geq><FieldRef Name='Modified' /><Value Type='DateTime'>1900-12-01T19:49:00Z</Value></Geq><Gt><FieldRef Name='ID' /><Value Type='Counter'>$($itemId)</Value></Gt></And><Leq><FieldRef Name='ID' /><Value Type='Counter'>$($itemId + $rowLimit)</Value></Leq></And></Where>") | Out-Null $stringBuilder.Append("<OrderBy><FieldRef Name='ID' Ascending='TRUE' /></OrderBy>") | Out-Null $stringBuilder.Append("</Query>") | Out-Null $stringBuilder.Append("<ViewFields>") | Out-Null $stringBuilder.Append("<FieldRef Name='Title' />") | Out-Null $stringBuilder.Append("<FieldRef Name='FileLeafRef' />") | Out-Null $stringBuilder.Append("<FieldRef Name='FileRef' />") | Out-Null $stringBuilder.Append("<FieldRef Name='Modified' />") | Out-Null $stringBuilder.Append("<FieldRef Name='Created' />") | Out-Null $stringBuilder.Append("<FieldRef Name='ID' />") | Out-Null $stringBuilder.Append("<FieldRef Name='Editor' />") | Out-Null $stringBuilder.Append("</ViewFields>") | Out-Null $stringBuilder.Append("<RowLimit Paged='TRUE'>$($rowLimit)</RowLimit>") | Out-Null $stringBuilder.Append("</View>") | Out-Null $req123 = Get-Content -Path ".\req\req1.xml" -Encoding:UTF8 $spqQuery = New-Object Microsoft.SharePoint.Client.CamlQuery # $req123 # $spqQuery.ViewXml = $stringBuilder.ToString(); $spqQuery.ListItemCollectionPosition = $page $pageNumber ++; $itemki = $list.GetItems($spqQuery); $spqQuery.ListItemCollectionPosition = $itemki.ListItemCollectionPosition try { $ctx.Load($itemki) $ctx.ExecuteQuery(); } catch { <#Do this if a terminating exception happens#> Write-Host "$($_)" Write-Host "$($_)" exit 0 } Write-Host "################## PAGE " $($page.PagingInfo) " #########################" Write-Host "processing query results. Recs: $($itemki.Count) itemId $($itemId) max $($max)" $Counter = 0; foreach ($item in $itemki) { # Write-Host "$($item["ID"]) title pageNumber '$($pageNumber)' : $($item["Title"])" $datas += [PSCustomObject]@{ "FileLeafRef" = $item["FileLeafRef"] "Title" = $item["Title"] "FileRef" = $item["FileRef"] "Modified" = $item["Modified"] "Created" = $item["Created"] "Editor" = $item["Editor"].Email } $itemId = $item.ID } $page = $itemki.ListItemCollectionPosition # $itemId += $rowLimit } Until($itemId -ge $max) Write-Host "$($listTitle) list.ItemCount $($list.ItemCount)" $datas | Export-Csv -Path ".\datas\$($listTitle)_7.csv" -Encoding utf8 -NoTypeInformation -Delimiter ";" -Append Write-Host "$(AddNiceTimeSpan -start $startDate)"
Csom Context Sharepoint ClientContext
On 23/05/2023
c# Connect to Sharepoint with ClientContext appId and certificat ThumbPrint
using Microsoft.Identity.Client;
using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace MY.PNP.Powershell.EXT.CSOM
{
public class CsomContext : IDisposable
{
public ClientContext _clientContext;
public ClientRuntimeContext _context;
private async Task GetAccessToken(string tenantName, string clientId, string thumbprint)
{
var certificate = GetCert(thumbprint);
var authority = $"https://login.microsoftonline.com/{tenantName}.onmicrosoft.com/";
var azureApp = ConfidentialClientApplicationBuilder.Create(clientId)
.WithAuthority(authority)
.WithCertificate(certificate)
.Build();
var scopes = new string[] { $"https://{tenantName}.sharepoint.com/.default" };
var authResult = await azureApp.AcquireTokenForClient(scopes).ExecuteAsync();
return authResult.AccessToken;
}
public async Task CallClientObjectModel(string tenantName, string url, string clientId, string thumbprint)
{
var token = await GetAccessToken(tenantName, clientId, thumbprint);
var siteUrl = url;//;$"https://{tenantName}.sharepoint.com";
var context = new ClientContext(siteUrl);
context.ExecutingWebRequest += (s, e) =>
{
e.WebRequestExecutor.RequestHeaders["Authorization"] =
"Bearer " + token;
};
var web = context.Web;
context.Load(web);
context.ExecuteQuery();
Console.WriteLine(web.Title);
Console.WriteLine(web.Url);
_context = web.Context;
_clientContext = context;
}
private X509Certificate2 GetCert(string thumbprint)
{
X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
certStore.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certCollection = certStore.Certificates.Find(
X509FindType.FindByThumbprint,
// Replace below with your cert's thumbprint
thumbprint,
false);
X509Certificate2 cert = null;
// Get the first cert with the thumbprint
if (certCollection.Count > 0)
{
cert = certCollection[0];
// Use certificate
Console.WriteLine(cert.FriendlyName);
}
certStore.Close();
return cert;
}
public void Dispose()
{
if (_context != null)
_context.Dispose();
if (_clientContext != null)
_clientContext.Dispose();
}
}
}
Add WebPart On Modern Page PNP Powershell
On 07/05/2021
Add custom WebPart On Modern Page Powershell
List your webpart with :
$page = Get-PnPPage -Identity "aTestDisp2.aspx"
$page.AvailablePageComponents() select your webpart by name or guid
param([string]$siteUrl="https://myyTenant.sharepoint.com/sites/muSite", [string]$pageName="aTestDisp2", [string]$webPartName="Test - Display Item") clear Connect-PnPOnline -Url $siteUrl -UseWebLogin #set your wenpart properties $wpProps = @{webUrl="https://myyTenantsharepoint.com/sites/
muSite" listId="b4d5f780-bd74-4cd5-b051-fed423e43125" field="Title" itemId="1"} #get the page $page = Get-PnPPage -Identity $pageName # get the target webpart $wp = $page.AvailablePageComponents() | Where-Object {$_.Name -eq $webPartName } if($wp -eq $null) { Write-Host "webpart '$($webPartName)' not found " -ForegroundColor:Red } #Add webpart with default user, to column 1 in section 1 Add-PnPPageWebPart -Page $pageName -Component $wp -Section 1 -Column 1 -WebPartProperties $wpProps
Disconnect-PnPOnline
You can check webpart properties, in result
PowerShell Csom Batch Delete / delete all list items
On 09/03/2021
Powershell CSOM clear list items / delete all items
clear
# Connect-PNPOnline -url $ProdWaveplaceURL -UseWebLogin -ErrorAction Stop
# Disconnect-PnPOnline
$listTitle = "testTaxo"
$list = Get-PnPList -Identity $listTitle -ThrowExceptionIfListNotFound
$ctx = Get-PnPContext
$page = $null
$pageNumber = 0;
$rowLimit = 100
$startDate = Get-Date
$deletedItemsCount = 0;
Do{
$stringBuilder = New-Object System.Text.StringBuilder
$stringBuilder.Append("<View scope='RecursiveAll'>") | Out-Null
$stringBuilder.Append("<Query><Where></Where>")| Out-Null
$stringBuilder.Append("<Orderby><Fieldref ascending='TRUE' name='ID'></Fieldref></Orderby>")| Out-Null
$stringBuilder.Append("</Query>")| Out-Null
$stringBuilder.Append("<Viewfields>")| Out-Null
$stringBuilder.Append("<Fieldref name='ID' />")| Out-Null
$stringBuilder.Append("</Viewfields>")| Out-Null
$stringBuilder.Append("<Rowlimit paged='TRUE'>$($rowLimit)</Rowlimit>")| Out-Null
$stringBuilder.Append("</View>")| Out-Null
$spqQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$spqQuery.ViewXml = $stringBuilder.ToString();
$spqQuery.ListItemCollectionPosition = $page
$pageNumber ++;
$spqQuery.ViewXml = $stringBuilder.ToString();
$itemki=$list.GetItems($spqQuery);
$spqQuery.ListItemCollectionPosition = $itemki.ListItemCollectionPosition
$ctx.Load($itemki)
$ctx.ExecuteQuery();
Write-Host "################## PAGE " $($page.PagingInfo) " #########################"
Write-Host "processing query results. Recs: $($itemki.Count)"
$Counter = $itemki.Count;
if($itemki.Count -eq 0){
exit 0
}
do{
$itemki[$Counter - 1].DeleteObject()
$Counter--
$deletedItemsCount++
}while($Counter -gt 0)
Invoke-PnPQuery
Write-Host "deletedItemsCount $($deletedItemsCount)"
$page = $itemki.ListItemCollectionPosition
$comp = $endDate - $startDate
$endDate = Get-Date
Write-Host "time to delete $($rowLimit) elements : Days '$($comp.Days)' Hours '$($comp.Hours)' Minutes '$($comp.Minutes)' Seconds '$($comp.Seconds)'"
}
Until($page -eq $null)
$comp = $endDate - $startDate
Write-Host "time to delete : Days '$($comp.Days)' Hours '$($comp.Hours)' Minutes '$($comp.Minutes)' Seconds '$($comp.Seconds)'"
Sharepoint Caml Query
On 08/09/2020
Basic CSOM caml query
<View>
<Query>
<Where>
<Eq>
<FieldRef Name='Matricule'/>
<Value Type='Text'>90136794</Value>
</Eq>
</Where>
<OrderBy><FieldRef Name='ID' Ascending='True'/></OrderBy>
</Query>
<ViewFields>
<FieldRef Name='Matricule' />
<FieldRef Name='Email' />
</ViewFields>
<RowLimit>2</RowLimit>
</View>
Query Null Text fields or not
<Where>
<IsNotNull>
<FieldRef Name="Famille"/>
</IsNotNull>
</Where>
<Where>
<IsNull>
<FieldRef Name="Famille"/>
</IsNull>
</Where>
by ID
<Where>
<Eq>
<FieldRef Name='ID'/>
<Value Type='Counter'>645</Value>
</Eq>
</Where>
Get tasks by itemId (relatedItems in tasks list)
<Where>
<Eq>
<FieldRef Name='RelatedItems'/>
<Value Type='RelatedItems'>""ItemId"":435,</Value>
</Eq>
</Where>
boolean / Yes/No
<Eq><FieldRef Name='YourFieldName'/><Value Type='Boolean'>1</Value></Eq>
Comparators
<Eq> => equals
<Neq> => not equals
<Lt> => Less than
<Leq> => less or equal
<Gt> greater than
<Geq> greater or equal
<Contains>
<BeginsWith>
Errors
-
Sharepoint error Error in my cases One or more field types are not installed properly. Go to the list settings page to delete these fields. Fields in your query are not in list Cannot complete this action.
Please try again.
Syntax error in your query
CSOM Delete Items By Id Range
On 02/09/2020
CSOM delete items by id range
Get items
public ListItemCollection GetItems(string listRelativUrl, string where, string orderBy, string viewFields, int rowlimit = 100)
{
string xmlView = "";
try
{
List lst = CurrentWeb.GetList(CurrentWeb.ServerRelativeUrl + listRelativUrl);
StringBuilder vf = new StringBuilder();
if (!string.IsNullOrEmpty(viewFields))
{
vf.Append("<ViewFields>");
foreach (string fieldName in viewFields.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
{
vf.Append($"<FieldRef Name='{fieldName.Trim()}' />");
}
vf.Append("</ViewFields>");
}
CamlQuery camlQuery = new CamlQuery();
camlQuery = new CamlQuery();
Logger.LogInfo($"listRelativUrl {listRelativUrl} : <View><Query>{where}{orderBy}</Query>{vf.ToString()}<RowLimit>{rowlimit}</RowLimit></View>");
xmlView = $"<View><Query>{where}{orderBy}</Query>{vf.ToString()}<RowLimit>{rowlimit}</RowLimit></View>";
camlQuery.ViewXml = xmlView;
ListItemCollection coll = lst.GetItems(camlQuery);
SPContext.Load(coll);
SPContext.ExecuteQuery();
Logger.Log($"coll.Count : '{coll.Count}' : camlQuery.ViewXml {camlQuery.ViewXml}");
return coll;
}
catch (ServerException ex1)
{
Logger.LogError($"Error ServerException Common.Sharepoint.SPTools.GetItems url '{_url}' login : '{_login}' " +
$" listRelativUrl '{listRelativUrl}' where '{where}' orderBy '{orderBy}' rowlimit '{rowlimit}' xmlView : '{xmlView}' Exception '{ex1}'");
throw;
}
catch (Exception ex)
{
Logger.LogError($"Error .Common.Sharepoint.SPTools.GetItems url '{_url}' login : '{_login}' " +
$" listRelativUrl '{listRelativUrl}' where '{where}' orderBy '{orderBy}' rowlimit '{rowlimit}' xmlView : '{xmlView}' Exception '{ex}'");
throw;
}
}
delete items
public int DeleteItemByIdRange(string listRelativeUrl, int rowLimit, int from, int to = -1)
{
int deleted = 0;
try
{
string req = "";
if (to == -1)
req = $"<Where><Geq><FieldRef Name='ID'/><Value Type='Counter'>{from}</Value></Geq></Where>";
else
{
if (from > to)
throw new Exception($"DeleteItemByIdRange from should be < than to listRelativeUrl '{listRelativeUrl}' from '{from}' to '{to}'");
req = $"<Where><And><Geq><FieldRef Name='ID' /><Value Type='Counter'>{from}</Value></Geq><Leq><FieldRef Name='ID' /><Value Type='Counter'>{to}</Value></Leq></And></Where>";
}
ListItemCollection coll = GetItems(listRelativeUrl, req, "", "ID", rowLimit);
while (coll.Count > 0)
{
string id = coll[0].Id.ToString();
coll[0].DeleteObject();
this.ExecuteQuery($"DeleteItemByIdRange listRelativeUrl '{listRelativeUrl}' from '{from}' to '{to}' Id = '{id}'");
deleted++;
}
return deleted;
}
catch (Exception ex)
{
Logger.LogError($"Error Sharepoint.SPTools.DeleteItemByIdRange from id '{from}' to id '{to}' url '{_url}' login : '{_login}' Exception '{ex}'");
throw;
}
}
catch ServerException to get more details on CSOM exceptions
CSOM powershell SSL TSL site
On 23/11/2019
Exception calling "ExecuteQuery" with "0" argument(s): "The request was aborted: Could not create SSL/TLS secure channel." SOLVED
Exception lors de l'appel de ExecuteQuery" avec 0" argument(s): La demandea été abandonnée: Impossible de créer un canal sécurisé SSL/TLS." : résolu
How to connect to a SSL/ TSL sharepoint site with CSOM in powershell
Start to call a request with your certificate
$CertificateThumbprint = "53836D3C35F949959D7E4038D5D39D7B"
$response = Invoke-WebRequest -Verbose -URI $anUrl -CertificateThumbprint $CertificateThumbprint -UseDefaultCredentials -SessionVariable websession -ErrorAction:Stop
# in this ExecutingWebRequest you should add the certificate to yoour request and add the authentification cookie
$request = $EventArgs.WebRequestExecutor.WebRequest
to list all your available certificate execute following script
Powershell Csom Paged Caml Query
On 18/09/2019
$list = Get-PnPList -Identity $listTitle -ThrowExceptionIfListNotFound
$ctx = Get-PnPContext
$page = $null
$pageNumber = 0;
$rowLimit = 200
Do{
$stringBuilder = New-Object System.Text.StringBuilder
$stringBuilder.Append("<View Scope='RecursiveAll'>") | Out-Null
$stringBuilder.Append("<Query><Where></Where>")| Out-Null
$stringBuilder.Append("<OrderBy><FieldRef Name='ID' Ascending='TRUE' /></OrderBy>")| Out-Null
$stringBuilder.Append("</Query>")| Out-Null
$stringBuilder.Append("<ViewFields>")| Out-Null
$stringBuilder.Append("<FieldRef Name='ID' />")| Out-Null
$stringBuilder.Append("<FieldRef Name='Title' />")| Out-Null
$stringBuilder.Append("</ViewFields>")| Out-Null
$stringBuilder.Append("<RowLimit Paged='TRUE'>$($rowLimit)</RowLimit>")| Out-Null
$stringBuilder.Append("</View>")| Out-Null
$spqQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$spqQuery.ViewXml = $stringBuilder.ToString();
$spqQuery.ListItemCollectionPosition = $page
$pageNumber ++;
$spqQuery.ViewXml = $stringBuilder.ToString();
$itemki=$list.GetItems($spqQuery);
$spqQuery.ListItemCollectionPosition = $itemki.ListItemCollectionPosition
$ctx.Load($itemki)
$ctx.ExecuteQuery();
Write-Host "################## PAGE " $($page.PagingInfo) " #########################"
Write-Host "processing query results. Recs: $($itemki.Count)"
$Counter = 0;
foreach($item in $itemki)
{
Write-Host "$($item["ID"]) title pageNumber '$($pageNumber)' : $($item["Title"])"
}
$page = $itemki.ListItemCollectionPosition
}
Until($page -eq $null)
Add SPList With CSOM
On 15/08/2019