Below code help to identify the SharePoint Online list/library (all) items folder and file. Based on that you can delete the file or folder. If you want generate the inventory of the list/ library you can use this code. (Change the list template Id or name)
Prerequisites
- Download SharePoint online Management Shell and Install
- Enable the execution policy true (Check DOS command..
- Copy the below code and create XX.ps1 file and execute it Online Management Shell.
- You Should have Tenant admin permission
Powershell code to generate folder and file item in a SharePoint online list/library
#Copy start here
--------------------------------------------------------------
function Get-SPOWeb()
{
param (
$SPOCredentials,
$Url,
$IncludeSubsites=$false
)
write-host "Inside spoweb" -foregroundcolor green
$ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$ctx.Credentials = $SPOCredentials
$ctx.Load($ctx.Web)
$ctx.Load($ctx.Web.lists)
$ctx.Load($ctx.Web.Webs)
$ctx.ExecuteQuery()
write-host "Inside spoweb - Before for each loop " -foregroundcolor green
Write-Host "(Lists count: "$ctx.Web.lists.count")"
foreach($list in $ctx.Web.lists)
{
try{
#.. write out every document library which is not the standard "Site Assets"
# if(($list.BaseTemplate -eq 101) -and ($list.Title -ne "Site Assets") -and ($list.Title -eq "TestLibrary100"))
#if document library use the below if
# if(($list.BaseTemplate -eq 101)
#If coustom library use the below if Replace the TestLibrary name with your custom library
if($list.Title -eq "TestLibrary")
{
Write-Host "Web:" $ctx.Web.Url -foregroundcolor green
Write-Host "ListTitle:" $list.Title -foregroundcolor green
#Write-Host "BaseTemplate:" $list.BaseTemplate -foregroundcolor green
$camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
#Retrive all list item with filter <Gt> replace based on your requirements
$camlQuery.ViewXml ="<View Scope='RecursiveAll'><Query><OrderBy><FieldRef Name='Modified'/></OrderBy><Where><Gt><FieldRef Name='Modified' /><Value IncludeTimeValue='TRUE' Type='DateTime'>2013-01-01T11:34:54Z</Value></Gt></Where></Query></View>"
$allItems=$list.GetItems($camlQuery)
$ctx.Load($allItems)
$ctx.ExecuteQuery()
Write-Host "ExecuteQuery passs"
$Itemscount=$allItems.count
Write-Host "List items count:" $Itemscount
$fileref=''
$itemexit=$false
$itemcounter=0
if ($Itemscount-gt0)
{
for ($i=$Itemscount-1; $i-ge0; $i--)
{
$item=$allItems[$i];
$fileref=$item["FileRef"]
$siteInfos = $itemStructure | Select-Object *;
Write-Host "Fileref" $fileref
Write-Host "SiteURL" $ctx.Web.Url
Write-Host "Modified" $item["Modified"]
Write-Host "FSObjType" $item["FSObjType"]
$siteInfos.File=$fileref
$siteInfos.SiteURL=$ctx.Web.Url
$siteInfos.Modified=$item["Modified"]
$siteInfos.Modified=$item["FSObjType"]
$global:sitesList += $siteInfos
# FSObject type data 0 means item is file and 1 means folder. based on that you can generate the result and delete files or folder
# t
if ($item["FSObjType"] ==0)
{
$allItems[$i].DeleteObject()
$itemexit=$true
$itemcounter=$itemcounter+1
}
}
#Execute only when items found
if($itemexit)
{
$ctx.ExecuteQuery()
Write-Host "Files Deleted succesfully"
}
}
}
}
catch{
Write-Host "Error while reading list:" $list.Title "$($_.Exception.Message)" -foregroundcolor red
}
}
if($ctx.Web.Webs.Count -gt 0 -and $IncludeSubsites)
{
Write-Host "--" -ForegroundColor DarkGreen
for($i=0;$i -lt $ctx.Web.Webs.Count ;$i++)
{
write-host $ctx.Web.Webs[$i].Url
Get-SPOWeb -SPOCredentials $SPOCredentials -Url $ctx.Web.Webs[$i].Url -IncludeSubsites $IncludeSubsites
}
}
}
Import-Module Microsoft.Online.SharePoint.PowerShell
#Replce with your sharepoint Online admin URL not a site URL
# You need to have admin access in the tenant else it wont work
$adminUrl = "https://#######.sharepoint.com"
#Replace your user name
$userName = "############"
$password = Read-Host "Please enter the password for $($userName)" -AsSecureString
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $password
$SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $password)
Connect-SPOService -Url $adminUrl -Credential $credentials
write-host "Info: Connected succesfully to Office 365" -foregroundcolor green
# Local variables
$global:sitesList = @()
$delimiter=","
# Build structure what are the fields you want pls update List/library columns
$itemStructure = New-Object psobject
$itemStructure | Add-Member -MemberType NoteProperty -Name "File" -value ""
$itemStructure | Add-Member -MemberType NoteProperty -Name "Modified" -value ""
$itemStructure | Add-Member -MemberType NoteProperty -Name "FSObjType" -value ""
$itemStructure | Add-Member -MemberType NoteProperty -Name "SiteURL" -value ""
write-host "SPOWeb-Start"
# give your site collection url-- Get-SPOWeb -SPOCredentials $SPOCredentials -Url "https://##########/xyz" -IncludeSubsites $true
try{
#File creation
$guid=[guid]::NewGuid()
$listfilepath="H:\PowershellResults\Documentlist_"+$guid+".csv"
$sitesList | Where-Object {$_} | Export-Csv -Delimiter "$delimiter" -Path $listfilepath -notype
#####
write-host "Excel file created in this location:" $listfilepath -ForegroundColor Green
}
catch{
write-host "Excel file not created. Please check file path and try again." -ForegroundColor Red
}
# Copy End Here
-----------
Hmm is anyone else encountering problems with the images on this blog loading? I'm trying to find out if its a problem on my end or if it's the blog. Any feed-back would be greatly appreciated.
ReplyDeleteTerrific article! This is the type of information that should be shared across the internet. Shame on the search engines for not positioning this put up upper! Come on over and visit my site . Thank you =)
ReplyDelete