How to get default column values during upload using pnp shell?

Raki 486 Reputation points
2020-12-03T01:46:06.177+00:00

Hello,
I have a document library. In that library I have root folders and sub folders. Is that possible to get folders values (root/sub folder) into two columns when user will upload from inside of the sub-folder using PnP shell? I know I could achieve same by going library settings and then default column value settings but this not practical though as I need to work with too many folders and sub folders. Example: let’s say I have root folder called as Verizon and sub folder called 11 Broadway. Now I want to get both folders values automatically during upload into two columns (Agent and Address)

Thanks in advanced!

Microsoft 365 and Office | SharePoint | For business | Windows
0 comments No comments
{count} votes

Answer accepted by question author
  1. MichaelHan-MSFT 18,136 Reputation points
    2020-12-03T08:16:09.043+00:00

    Hi @Raki ,

    The below script would help you achieve this:

    #Connect to PnP Online  
    connect-pnponline https://tenant.sharepoint.com/sites/test  
    $Library = "doc1"  
    #Get all root folders of the library  
    $RootFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $Library -ItemType Folder | Where {$_.Name -ne "Forms"}  
    #Loop through the RootFolders  
    ForEach($RootFolder in $RootFolders){  
     $RootFolderURL = $Library+"/"+$RootFolder.Name  
     #Get all sub folders of the rootFolder  
     $Subfolders=Get-PnPFolderItem -FolderSiteRelativeUrl $RootFolderURL -ItemType Folder  
     #Loop through the SubFolders  
     ForEach($SubFolder in $SubFolders){  
     $SubFolderPath=$RootFolder.Name+"/"+$SubFolder.Name  
     write-host "Setting default column values for folder:" $SubFolderPath  
     Set-PnPDefaultColumnValues -List $Library -Field Agent -Value $RootFolder.Name -Folder $SubFolderPath  
     Set-PnPDefaultColumnValues -List $Library -Field Address -Value $SubFolder.Name -Folder $SubFolderPath  
      
     }  
    }  
    

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    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.

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Raki 486 Reputation points
    2020-12-03T15:08:48.09+00:00

    Hi,

    Thanks for your answer. i tried to run the above script but for some reason i did not see any effect in default column value settings. usually it showed green on the folder when it applied. i have uploaded a document, it did not get the values for fields. what i noticed during debug, $Subfolders value not getting and its not passing through for loop. what could be the issue issue?

    44777-image.png


  2. Raki 486 Reputation points
    2020-12-04T14:53:23.253+00:00

    Hello Michael,

    Appreciated for your help on this. its working like a charm now. another question is, can i remove default column value settings from folders adding below command into above script?

    Clear-PnPDefaultColumnValues -List $Library -Field 'Agent' -Value $RootFolder.Name -Folder $SubFolderPath
    Clear-PnPDefaultColumnValues -List $Library -Field 'Address' -Value $SubFolder.Name -Folder $SubFolderPath

    Thanks

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.