Skip to content

Commit b03f87d

Browse files
nicodecleyreplamber
authored andcommitted
Adds 'list all files with missing required metadata' sample. Closes pnp#3277
1 parent 3e1972a commit b03f87d

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed
Loading
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
[
2+
{
3+
"name": "pnp-list-all-files-with-missing-required-metadata",
4+
"source": "pnp",
5+
"title": "List all files with missing required metadata",
6+
"url": "https://pnp.github.io/cli-microsoft365/sample-scripts/spo/list-all-files-with-missing-required-metadata",
7+
"creationDateTime": "2022-12-31",
8+
"updateDateTime": "2022-12-31",
9+
"shortDescription": "List all files with missing required metadata.",
10+
"longDescription": [
11+
"List all files with missing required metadata."
12+
],
13+
"products": [
14+
"SharePoint"
15+
],
16+
"categories": [],
17+
"tags": [
18+
"files",
19+
"reports"
20+
],
21+
"metadata": [
22+
{
23+
"key": "CLI-FOR-MICROSOFT365",
24+
"value": "6.0.0"
25+
}
26+
],
27+
"thumbnails": [
28+
{
29+
"type": "image",
30+
"order": 100,
31+
"url": "https://raw.githubusercontent.com/pnp/cli-microsoft365/main/docs/docs/sample-scripts/spo/list-all-files-with-missing-required-metadata/assets/preview.png",
32+
"alt": "preview image for the sample"
33+
}
34+
],
35+
"authors": [
36+
{
37+
"gitHubAccount": "nicodecleyre",
38+
"pictureUrl": "https://avatars.githubusercontent.com/u/35696168?v=4",
39+
"name": "Nico De Cleyre"
40+
}
41+
],
42+
"references": [
43+
{
44+
"name": "Want to learn more about CLI for Microsoft 365 and the commands",
45+
"description": "Check out the CLI for Microsoft 365 site to get started and for the reference to the commands.",
46+
"url": "https://aka.ms/cli-m365"
47+
}
48+
]
49+
}
50+
]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
tags:
3+
- files
4+
- reports
5+
---
6+
7+
# List all files with missing required metadata
8+
9+
Author: [Nico De Cleyre](https://www.nicodecleyre.com), Inspired by [Veronique Lengelle](https://veronicageek.com/2021/find-missing-metadata-using-powershell/)
10+
11+
The following script iterates through all files in all the document libraries of a SharePoint Online site. It returns all the files that have missing required metadata and excludes the `name` metadata
12+
13+
=== "PowerShell"
14+
15+
```powershell
16+
$siteUrl = "https://<TENANT-NAME>.sharepoint.com/sites/<YOUR-SITE>"
17+
$allLists = m365 spo list list --webUrl $($siteURL) --output json | ConvertFrom-Json
18+
$allLibs = $allLists | where {$_.BaseTemplate -eq 101}
19+
$results = @()
20+
$fields = @('FileLeafRef', 'FileSystemObjectType')
21+
22+
foreach ($lib in $allLibs) {
23+
$allRequiredFields = m365 spo field list --webUrl $($siteURL) --listId $($lib.Id) --query "[?Required == ``true``]" --output json | ConvertFrom-Json
24+
25+
if($allRequiredFields.Length -eq 0){
26+
continue
27+
}
28+
29+
[array]$allRequiredFieldsInternalName = $($allRequiredFields | select InternalName).InternalName
30+
31+
ForEach ($field in $fields)
32+
{
33+
If (-not ($allRequiredFieldsInternalName -contains $field))
34+
{
35+
$allRequiredFieldsInternalName += $field
36+
}
37+
}
38+
39+
$allItems = m365 spo listitem list --webUrl $($siteURL) --listId $($lib.Id) --fields $($allRequiredFieldsInternalName -join ",") --output json | ConvertFrom-Json
40+
$allItems = $allItems | where {$_.FileSystemObjectType -eq 0}
41+
42+
foreach($item in $allItems){
43+
foreach($requiredfield in $allRequiredFields){
44+
if($requiredfield.InternalName -eq "FileLeafRef"){
45+
continue
46+
}
47+
48+
if ($null -eq $item["$($requiredfield.InternalName)"]) {
49+
$results += [pscustomobject]@{
50+
FileName = $item.FileLeafRef
51+
FieldName = $requiredfield.Title
52+
ListOrLibrary = $lib.Title
53+
FileLocation = $lib.RootFolder.ServerRelativeUrl
54+
}
55+
}
56+
}
57+
}
58+
}
59+
$results
60+
```

0 commit comments

Comments
 (0)