Can we use needsAttentionView to fetch document libraries that has documents with missing required information

Rohini Dhale 0 Reputation points
2023-09-07T10:06:38.23+00:00

Hi ,

I need to fetch all those libraries from a site that contains documents with missing required fields.I don't need documents , just count of libraries.We have 'File That need attention' view , is there any field or property that we can use to fulfil this requirement.

SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,748 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Yanli Jiang - MSFT 22,341 Reputation points Microsoft Vendor
    2023-09-08T07:47:30.8966667+00:00

    Hi @Rohini Dhale ,

    Yes. To fetch all the libraries from a site that contain documents with missing required fields, you can use PowerShell script to loop through the libraries and check the number of items in the “File That need attention” view. This view filters the documents that have empty values in required columns. You can use the Get-PnPView cmdlet to get the view object and the Get-PnPListItem cmdlet to get the items in the view. You can also use the Get-PnPList cmdlet to get all the libraries in the site. Here is a sample script that you can have a try:

    #Connect to SPO 
    Connect-PnPOnline -Url “https://tenant.sharepoint.com/sites/sitname”  -Interactive
    
    #Get all the libraries in the site 
    $allLibs = Get-PnPList | Where-Object {$_.BaseType -eq “DocumentLibrary”}
    
    #Initialize a variable to store the count of libraries with missing required fields 
    $libCount = 0
    
    #Loop through each library 
    foreach($lib in $allLibs) { 
    #Get the “File That need attention” view object 
    $view = Get-PnPView -List $lib -Identity “File That need attention”
    
    #Get the number of items in the view
    $itemCount = (Get-PnPListItem -List $lib -Query $view.ViewQuery).Count
    
    #If the item count is greater than zero, increment the libCount variable
    if($itemCount -gt 0)
    {
        $libCount++
    }
    Copy
    }
    
    #Output the result 
    Write-host “The number of libraries with missing required fields is $libCount”
    

    Hope this helps.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Yanli Jiang - MSFT 22,341 Reputation points Microsoft Vendor
    2023-09-11T10:23:09.3266667+00:00

    Hi @Rohini Dhale ,

    You can also use the Get-PnPListItem cmdlet to get the items from a library and filter them by the _ModerationStatus column. This column indicates whether an item has been approved, rejected, or is pending. If an item is pending, it means that it has missing required fields. You can count the number of pending items in each library and output the library name and count if it is greater than zero. Please try below PowerShell script:

    #Connect to SharePoint Online site 
    Connect-PnPOnline -Url "https://tenant.sharepoint.com/sites/amy12345" -Interactive
    #Get all the libraries in the site 
    $libraries = Get-PnPList | Where-Object {$_.BaseType -eq "DocumentLibrary"}
    #Loop through each library 
    foreach ($library in $libraries) {
    Get the items from the library that have missing required fields (_ModerationStatus = 2)
    $items = Get-PnPListItem -List $library -Fields "ModerationStatus" | Where-Object {$.FieldValues._ModerationStatus -eq 2}
    Count the number of items
    $count = $items.Count
    Output the library name and count if it is greater than zero
    if ($count -gt 0) { 
    Write-Output "$($library.Title): $count" } 
    }
    

    The result:09111


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.