Powershell
Sharepoint powershell tips
On 28/05/2024
$String = "François-Demaison!?!#@$%^&*()_+\|}{○<>??/ €$¥£¢ \^$.|?*+()[{ 0123456789"
$String -replace '[^\p{L}\p{Nd}/(/_/_]', '_'
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)"
SharePoint Copy Files To Library
On 27/05/2024
{
"siteUrl": "https://tenant.sharepoint.com/sites/fdiSandBox/",
"targetFolder": "/Shared%20Documents/test",
"sourceFolder": "C:\\temp\\myFolderr",
"checkIfFileExists": true,
"forceOverWrite": false,
"includeChildFolders": true,
"copyDates": false,
"copyAuthor": false,
"lofFileName": "copyfiles",
"forbiddenChars": "~,#,%,&,*,{,},\\,:,<,>,?,/,|,",
"replacementChar": "_"
}
Clear-Host
. .\common.ps1
#get copy configuration
$config = GetConnectionConfiguration -filePath ".\config\configuration.json"
$logFileName = "$($config.lofFileName))_$(Get-Date -Format "yyyyMMddhhmmss").log"
$loadedFolders = @();
$startDate = Get-Date
function CheckAlreadyConnected {
Param([Parameter (Mandatory = $true)][string]$url)
$ctx = Get-PnPContext
if ($null -eq $ctx) {
Connect-PnPOnline -Url $url -Interactive -ErrorAction Stop
WriteInfo -Message "connected to $($url)"
}
else {
$web = Get-PnPWeb
if ($web.Url.Trim().ToLower().trim("/") -ne $url.Trim().ToLower().trim("/")) {
Disconnect-PnPOnline
WriteInfo -Message "Disconnect-PnPOnline $($web.Url)"
Connect-PnPOnline -Url $url -Interactive -ErrorAction Stop
WriteInfo -Message "connected to $($url)"
}
}
}
CheckAlreadyConnected -url $config.siteUrl
$web = Get-PnPWeb
function copyFileToSp {
Param([Parameter (Mandatory = $true)][string]$sourceFilePath,
[Parameter (Mandatory = $true)][string]$destinationPath
, $created, $modified
)
#check target folder exists
if (-not $loadedFolders.Contains($destinationPath)) {
$folder = Get-PnPFolder -Url $destinationPath -ErrorAction SilentlyContinue
if ($null -eq $folder) {
$datas = $destinationPath.Split("/", [System.StringSplitOptions]::RemoveEmptyEntries)
$folderName = $datas[$datas.Length - 1];
$target = $destinationPath.Substring(0, $destinationPath.Length - $folderName.Length - 1)
Add-PnPFolder -Name $folderName -Folder $target
}
$loadedFolders += $destinationPath
}
$destination
$destinationPath
$Asset = @{}
$valuesOk = $false
# modified
if ($null -ne $modified) {
$date = Get-Date -Date $modified
$Asset.add("Modified", $date.ToString("yyyy-MM-dd HH:mm"))
$valuesOk = $true;
}
# created
if ($null -ne $created) {
$date = Get-Date -Date $created
$Asset.add("Created", $date.ToString("yyyy-MM-dd HH:mm"))
$valuesOk = $true;
}
if ($valuesOk -eq $true) {
$spFile = Add-PnPFile -Path $sourceFilePath -Folder $destinationPath -Values $Asset
}
else {
$spFile = Add-PnPFile -Path $sourceFilePath -Folder $destinationPath
}
$pp = 0;
$pp ++;
}
if ($config.includeChildFolders) {
$filesToCopy = Get-ChildItem -LiteralPath $config.sourceFolder -Recurse
}
else {
$filesToCopy = Get-ChildItem -LiteralPath $config.sourceFolder
}
$filesToCopy.Length
foreach ($file in $filesToCopy) {
$file.FullName;
$file.LastAccessTime
if ($file.GetType().Name -eq "FileInfo") {
$filePath = $file.DirectoryName
$destinationFileName = $filePath.Substring($config.sourceFolder.Length).trim("\").Replace("\", "/")
$destinationFileName = "$($web.ServerRelativeUrl)/$($config.targetFolder.Trim().Trim("/"))/$($destinationFileName)".Replace("%20", " ").TrimEnd("/")
Write-Host "dest : '$($destinationFileName)'"
copyFileToSp -sourceFilePath $file.FullName -destinationPath $destinationFileName -modified $file.LastWriteTime -created $file.CreationTime
}
}
WriteInfo -Message "nb files / folders added : $($filesToCopy.Length)"
AddNiceTimeSpan -start $startDate
SharePoint Copy ListItem / Duplicate ListItem
On 29/04/2024
SharePoint Copy ListItem / Duplicate ListItem
[string]$url = "https://yourSite.sharepoint.com/sites/test";
[string]$listTitle = "AddHistoryInExcel";
[int]$itemId = 17;
[string]$fieldsToCopy = "Title,WFInstance,ItemId,TargetListId,TargetDocLibPath";
Connect-PnPOnline -Url $url -Interactive
$itemToCopy = Get-PnPListItem -List $listTitle -Id $itemId
$list = Get-PnPList -Identity $listTitle
$ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$ListItem = $list.AddItem($ListItemInfo)
foreach($field in $fieldsToCopy.Split(","))
{
$ListItem[$field] = $itemToCopy[$field]
}
$ListItem.Update()
Invoke-PnPQuery
Write-Host "ItemId $($itemId) has been duplicated target id $($ListItem.Id) "
On 07/04/2023
Get Sharepoint all Big files with powershell and Sharepoint Search
Import-Module PnP.PowerShell
# Connect-PnPOnline -Url $url -UseWebLogin
$select = "Title,SiteTitle,SPSiteURL,SPWebUrl,ViewsLifeTimeUniqueUsers,ViewsLifeTime,language,Created,Size,Path,LastModifiedTime,ViewsLastMonths3,ViewsLastMonths3Unique,LastAnalyticsUpdateTime,ViewerCount"
$sortlist = @{Size = 'descending' }
$siteName = "serviceanimationreseauFPFSITW"
$search = Submit-PnPSearchQuery -Query "ContentTypeId:0x0101*" -MaxResults 500 -SelectProperties $select -TrimDuplicates $true -SortList $sortlist -StartRow 0
$ret = $search.ResultRows.Count
$count = 0
$stop = $false
$itemsToSave = @();
while ($ret -eq 500 -and -not $stop) {
# Disconnect-PnPOnline
# -MaxResults 10
$lastSize = 0
foreach ($row in $search.ResultRows) {
$mo = 0;
[Int64]::TryParse($row["Size"], [ref] $mo ) | Out-Null
$nice = '{0:N0}' -f $mo
$data = [PSCustomObject]@{
"Title" = $row["Title"]
"Size" = $row["Size"]
"SizeMo" = '{0:N0}' -f ($mo / 1000000)
"NiceSize" = $nice
"SiteTitle" = $row["SiteTitle"]
"ViewsLifeTimeUniqueUsers" = $row["ViewsLifeTimeUniqueUsers"]
"ViewsLifeTime" = $row["ViewsLifeTime"]
"language" = $row["language"]
"Created" = $row["Created"]
"LastModifiedTime" = $row["LastModifiedTime"]
"ViewsLastMonths3" = $row["ViewsLastMonths3"]
"ViewsLastMonths3Unique" = $row["ViewsLastMonths3Unique"]
"LastAnalyticsUpdateTime" = $row["LastAnalyticsUpdateTime"]
"ViewerCount" = $row["ViewerCount"]
"Path" = $row["Path"]
"SPSiteURL" = $row["SPSiteURL"]
"SPWebUrl" = $row["SPWebUrl"]
}
$lastSize = $data.Size
if ([int64]::Parse($data.Size) -lt 10000000) {
$stop = $true;
break;
}
$itemsToSave += $data
}
$count += 500
$search = Submit-PnPSearchQuery -Query "ContentTypeId:0x0101*" -MaxResults 500 -SelectProperties $select -TrimDuplicates $true -SortList $sortlist -StartRow $count
$ret = $search.ResultRows.Count
Write-Host "$($itemsToSave.Count) $($count) $($lastSize)"
}
$itemsToSave | Export-Csv -Path "AllSites_8.csv" -Encodin:UTF8 -NoTypeInformation -Delimiter ";"
Disconnect-PnPOnline
On 05/04/2023
Create certification for azure app
# Create certificate
$mycert = New-SelfSignedCertificate -DnsName "myCertificate.org" -CertStoreLocation "cert:\CurrentUser\My" -NotAfter (Get-Date).AddYears(1) -KeySpec KeyExchange
$mypwd = ConvertTo-SecureString -String "myCertificatePsw" -Force -AsPlainText
# Export certificate to .pfx file
$mycert | Export-PfxCertificate -FilePath myCertificate.pfx -Password $mypwd
# Export certificate to .cer file
$mycert | Export-Certificate -FilePath myCertificate.cer
Connect to site
$url = "https://m365x6422vvvvd.sharepoint.com/";
$appId = "868d7a0c-a3dc-45af-b4a7-f72a70f61a60";
$thumbprint = "A17177BB0E8A465F6AD08B0CEAE2F369C46D6481";
$tenantId = "3533ab30-c2f0-48fd-b4c5-f5dc6ca77ec3"
Connect-PnPOnline -Url $url -Tenant $tenantId -Thumbprint $thumbprint -ClientId $appId
Export audit
premission required
Office 365 Management APIs (3) :: ActivityFeed.Read
possible filters
- FilePreviewed
- FileAccessed
- SignInEvent
- FileModifiedExtended
- FileUploaded
- PageViewed
- PagePrefetched
- FileCheckedIn
- FileModified
- FolderCreated
- ListUpdated
- ListViewed
$ele = Get-PnPUnifiedAuditLog -ContentType SharePoint -StartTime (Get-Date).AddDays(-2) -EndTime (Get-Date).AddDays(-1)
$ele = Get-PnPUnifiedAuditLog -ContentType SharePoint
$ele = Get-PnPUnifiedAuditLog -ContentType SharePoint | Where-Object {$_.Operation -eq "PageViewed"} | Select-Object CreationTime,Operation,Workload,UserId,ObjectId,SourceFileName,SiteUrl,SourceFileExtension,SourceRelativeUrl
$ele | Export-Csv -Path "Audit_3.csv" -Encodin:UTF8 -NoTypeInformation -Delimiter ";"
with sharepoint search request
kqlQuery = "ContentTypeId:0x0101009D1CB255DA76424F860D91F20E6C4118*";//news
kqlQuery = "ContentTypeId:0x0101* language=fr ViewsLastMonths3=0";//documents
kqlQuery = "ContentTypeId:0x0101* ViewsLifeTime=0";
var seletvvv = "Title,ViewsLifeTimeUniqueUsers,ViewsLifeTime,language,Created,Size,Path,LastModifiedTime,ViewsLastMonths3,ViewsLastMonths3Unique,LastAnalyticsUpdateTime";
&sortlist='Size:descending'
&sortlist='ViewsLifeTime:descending'
Powershell Import Export Fields
On 03/02/2023
Write-Output 'Connexion Portail'
# Connect-PnPOnline -Url $siteUrl -Tenant $aadDomain -Thumbprint $certifThumbprint -ClientId $appId
function ExportFieldsToCsv {
Param(
[string]$listTitle,
[string]$csvFilename,
[boolean]$includeHidden = $false
)
#$list = Get-PnPList -Identity $list
if ([string]::IsNullOrEmpty($listTitle)) {
$fields = Get-PnPField
$aFieldToExports = @();
for ($i = 0 ; $i -lt $fields.Length ; $i++) {
$field = $fields[$i];
$aFieldToExport = New-Object -TypeName PSObject -Property @{
'Title' = $field.Title;
'InternalName' = $field.InternalName;
'Id' = $field.Id;
'TypeAsString' = $field.TypeAsString;
'Hidden' = $field.Hidden;
'StaticName' = $field.StaticName;
'Required' = $field.Required;
'Description' = $field.Description;
'TypeDisplayName' = $field.TypeDisplayName;
'Group' = $field.Group;
}
$aFieldToExports += $aFieldToExport
$aFieldToExports | Export-Csv -Path "$($csvFilename)" -Encodin:UTF8 -NoTypeInformation -Delimiter ";"
}
}
else {
<# Action when all if and elseif conditions are false #>
}
}
function ExportFieldsToXml {
Param(
[string]$listTitle,
[string]$xmlFilename,
[boolean]$includeHidden = $false
)
#$list = Get-PnPList -Identity $list
if ([string]::IsNullOrEmpty($listTitle)) {
$fields = Get-PnPField
$stringBuilder = New-Object System.Text.StringBuilder
$null = $stringBuilder.AppendLine("")
for ($i = 0 ; $i -lt $fields.Length ; $i++) {
$field = $fields[$i];
$null = $stringBuilder.AppendLine($field.SchemaXml);
}
$null = $stringBuilder.AppendLine("")
$stringBuilder.ToString() | Out-File -FilePath "$($xmlFilename)" -Encodin:UTF8 -Append
}
}
function ImportFields {
Param(
[string]$listTitle,
[string]$xmlFilePath,
[boolean]$includeHidden = $false
)
[XML]$xmlfile = Get-Content -Path $xmlFilePath -Encoding:UTF8
if ([string]::IsNullOrEmpty($listTitle)) {
foreach ($field in $xmlfile.fields.Field) {
Write-Host $field.Name
$exists = Get-PnPField -Identity $field.Name -ErrorAction:SilentlyContinue
if ($null -eq $exists) {
Write-Host "$($field.Name) is null"
Add-PnPFieldFromXml -FieldXml $field.OuterXml
Write-Host "$($field.Name) added"
}
else {
Write-Host "$($field.Name) exists"
}
}
}
else {
foreach ($field in $xmlfile.fields.Field) {
$siteField = Get-PnPField -Identity $field.Name -ErrorAction:SilentlyContinue
$listField = Get-PnPField -Identity $field.Name -List $listTitle -ErrorAction:SilentlyContinue
if ($null -eq $siteField -and $null -eq $listField) {
Write-Host "$($field.Name) is null"
Add-PnPFieldFromXml -FieldXml $field.OuterXml
Add-PnPField -List $listTitle -Field $field.Name
Write-Host "$($field.Name) added"
}
elseif($null -eq $listField){
Write-Host "$($field.Name) exists"
Add-PnPField -List $listTitle -Field $siteField.InternalName
Write-Host "$($field.Name) added to $($listTitle)"
}
}
}
}
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)'"
Export List Fields To Csv With Pnp Powershell
On 21/01/2021
Export List Fields To Csv With Pnp Powershell
param($url="https://myTestSite", $listTitle="/Lists/aList")
clear
#connect to your site using windows identity manager
Connect-PnPOnline -Url "$($url)" -Credentials 'myWindowsOrWebIdentity'
$list = Get-PnPList -Identity $listTitle -Includes Fields
function ExctractFieldsNoContentType()
{
param($targetList)
$targetList.Title
$toExport = @()
for($i = 0 ; $i -lt $targetList.Fields.Count ; $i++)
{
$field = $targetList.Fields[$i]
#put in an object some field properties
$aFieldToExport = New-Object -TypeName PSObject -Property @{
'Title' = $field.Title
'InternalName' = $field.InternalName
'TypeAsString' = $field.TypeAsString
'Hidden' = $field.Hidden
'Group' = $field.Group
'Required' = $field.Required
'Description' = $field.Description
}
$toExport += $aFieldToExport
}
#export to csv
$toExport | Export-Csv -Path "$($targetList.Title)_fields.csv" -Encodin:UTF8 -NoTypeInformation -Delimiter ";"
}
ExctractFieldsNoContentType -targetList $list
Disconnect-PnPOnline
manage identities in
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)
On 15/08/2019
On 23/11/2018
On 16/11/2018
param(
[string]$siteCollUrl = "http://test/sites/site1/", [string]$listTitle = "Stockage",
[boolean]$checkin = $false, [boolean]$publish = $false, [boolean]$deleteAllVersion = $true
)
$web = Get-SPWeb $siteCollUrl
$splList = $web.Lists[$listTitle]
$spqQuery = New-Object Microsoft.SharePoint.SPQuery
$spqQuery.Query = "1 ";
$spqQuery.ViewAttributes = "Scope=""RecursiveAll""";
$spqQuery.ViewFields = " " #_UIVersionString
$spqQuery.ViewFieldsOnly = $true
$spqQuery.RowLimit = 1000;
# IsCheckedoutToLocal
$iNumber = 1
do {
if ($iNumber -eq 1) {
$spqQuery.ListItemCollectionPosition = $splListItems.ListItemCollectionPosition;
}
$splListItems = $splList.GetItems($spqQuery)
writeInfo "page number : $($iNumber)"
foreach ($splListItem in $splListItems) {
writeInfo "web $($web.ServerRelativeUrl) splList $($splList.RootFolder.ServerRelativeUrl) File id : $($splListItem.Id) # $($splListItem.Name) Title: $($splListItem["Title"]) Version: $($splListItem["owshiddenversion"]) _Level: $($splListItem["_Level"]) _IsCurrentVersion: $($splListItem["_IsCurrentVersion"]) _UIVersion: $($splListItem["_UIVersion"]) ver count : $($splListItem.File.Versions.Count)"
#check in file
try {
if ($checkin) {
$splListItem.File.CheckIn("CheckIn as admin");
}
}
catch {
WriteError "error CheckIn web $($web.ServerRelativeUrl) splList $($splList.RootFolder.ServerRelativeUrl) File id : $($splListItem.Id) "
}
#publish file
try {
if ($publish) {
$splListItem.File.Publish("Publish as admin");
}
}
catch {
WriteError "error Publish web $($web.ServerRelativeUrl) splList $($splList.RootFolder.ServerRelativeUrl) File id : $($splListItem.Id) "
}
#this is a folder
if ($splListItem.ContentTypeId.ToString() -eq "0x00C2208B8CE6E1422CADC1C521EAB2A68B") {
writeInfo "folder file $($splListItem.File.ServerRelativeUrl)"
continue
}
#delete old versions
if ($deleteAllVersion -and $splListItem.File -ne $null -and $splListItem.File.Versions -ne $null -and $splListItem.File.Versions.Count -gt 1) {
$splListItem.File.Versions.DeleteAll();
writeInfo "versions deleted for $($web.ServerRelativeUrl) splList $($splList.RootFolder.ServerRelativeUrl) File id : $($splListItem.Id)"
}
}
$iNumber += 1
$spqQuery.ListItemCollectionPosition = $splListItems.ListItemCollectionPosition;
}while ($spqQuery.ListItemCollectionPosition -ne $null)
$web.Dispose()
Set Content Type Field Property
On 22/10/2018
On 12/10/2018
Powershell Get SPList Last Modified
On 11/10/2018
Add task to reset sharepoint search index with powershell
On 05/10/2018
Create a powershell script (task1.ps1) with code below
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
Add-PSSnapin Microsoft.SharePoint.PowerShell -Verbose -ErrorAction:Stop
}
#the $True parameter denotes: Disable Alerts and Ignore Timeout error.
(Get-SPEnterpriseSearchServiceApplication).reset($true, $true)
Add a new task in your task scheduler
Check the radio button 'Run whether user is logged on or not
Set a name and add a trigger to set scheduling
set the action : program / script
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
argument with -command followed by the pass to your powershell script
-executionpolicy unrestricted -command D:\PS1\Task1.ps1
Save your task
PowerShell Export List Fields To Xml
On 01/10/2018
Sharepoint PowerShell query search service
On 29/09/2018
SharePoint PowerShell query search service
Sharepoint Execute Job With Powershell
On 28/09/2018
Sharepoint Powershell Clear Search Cache
On 28/09/2018
On 28/09/2018
On 28/09/2018
To add your assembly in powershell script / console
Add-Type -AssemblyName System.Web
Copy string to clipboard
"your datas" | c:\windows\system32\clip.exe
Reference sharepoint in powershell console
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Set your execution policy if you see this message
set-executionpolicy -executionpolicy unrestricted
powershell.exe -executionpolicy unrestricted -command .\test.ps1
Sort and filter your collections
$l.Fields | select internalname, title, type, hidden | Where-Object {$_.Hidden -eq $false} | Sort-Object -Property internalname | Format-Table
get spfields by group
$w.Fields | Where-Object {$_.Group -eq "Site colums FDI"} | select internalName, typeasstring
List files in folder and copy to clipboard
$files = ls | select name
$files | c:\windows\system32\clip.exe
Regular expression to check file names
"toto.spx" -match "^[a-zA-Z0-9._ -]+$"
Load an assembly
Import-Module ".\Microsoft.SharePoint.Client.Runtime.dll"
Get logs from all servers on sharepoint farm
Merge-SPLogFile -Path "C:\temp\FarmMergedLog41.log" -Overwrite -Level Unexpected -StartTime (Get-Date).AddMinutes(-20)
Merge-SPLogFile -Path "C:\temp\FarmMergedLog22.log" -Overwrite -Correlation "52b2019e-d446-70aa-8f27-8efc5ec7f143" -StartTime (Get-Date).AddMinutes(-90)
Open a powershell console as another account
Start-Process powershell.exe -Credential "domain\spadmin" -NoNewWindow -ArgumentList "Start-Process powershell.exe -Verb runAs"
Export webpart from page
Create a site collection
New-SPSite -Template STS#0 -OwnerAlias fdi\spdev -Url http://fdi-sp2013-1/sites/test6541 -Verbose -Confirm:$false -Language 1036 -Name test987 -Description oio
Do not use jquery $(document).ready(function () {
Use instead :
_spBodyOnLoadFunctionNames.push("loadAnnonce");//loadAnnonce is function loadAnnonce() called
Edit page on url change
?ControlMode=Edit&DisplayMode=Design
List assemblies
[AppDomain]::CurrentDomain.GetAssemblies() | Where-Object {$_.Location -ne $null -and $_.Location -like "*t.Client.*"}
Use stringBuilder to mange string concat
$stringBuilder = New-Object System.Text.StringBuilder
List of all sharepoint features