SOLVED

Getting a file's metadata field values using Pnp

%3CLINGO-SUB%20id%3D%22lingo-sub-169617%22%20slang%3D%22en-US%22%3EGetting%20a%20file's%20metadata%20field%20values%20using%20Pnp%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-169617%22%20slang%3D%22en-US%22%3E%3CP%3EUsing%20Powershell%20Pnp%20and%20SharePoint%20Online%2C%20I%20want%20to%20get%20the%20values%20in%20the%20custom%20fields%20of%20a%20file.%20I%20do%20not%20want%20to%20download%20the%20file%2C%20I%20just%20need%20the%20custom%20(metadata)%20fields.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20example%2C%20I%20have%20a%20custom%20field%20in%20a%20document%20library%20%22Customer%20ID%22%20and%20a%20file%20in%20that%20library%20named%20%22CustomerFile.xlsx%22.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20I%20try%20Get-PnpFolderItem%2C%20it%20gives%20me%20back%20a%20few%20standard%20attributes%20(Name%2C%20Type%2C%20Size%2C%20Last%20Modified)%2C%20but%20none%20of%20the%20custom%20fields.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20I%20try%20Get-PnpFile%20with%20the%20-Url%20and%20-AsListItem%2C%20it%20gives%20me%20back%20the%20ID%2C%20Title%2C%20and%20GUID%20only.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIs%20there%20a%20way%20to%20use%20Pnp%20to%20get%20the%20value%20of%20%22Customer%20ID%22%20(i.e.%20Customer_x0020_ID)%20for%20a%20given%20file%20that%20I%20know%20exists%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-170791%22%20slang%3D%22en-US%22%3ERe%3A%20Getting%20a%20file's%20metadata%20field%20values%20using%20Pnp%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-170791%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%2C%20that's%20similar%20(but%20simpler)%20than%20the%20method%20I%20eventually%20came%20up%20with.%3C%2FP%3E%0A%3CP%3ESince%20I%20am%20pulling%20in%20documents%20from%20a%20CAML%20query%2C%20I%20did%20this%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E(Get-PnpListItem%20-List%20%22My%20Documents%22%20-Query%20%24docsQuery).FieldValues%20%7C%20Foreach-Object%20-Process%20%7B%0A%20%20%20%20%24docFilename%20%3D%20%24_.FileLeafRef%3CBR%20%2F%3E%20%20%20%20%24customerID%20%3D%20%24_.Customer_x0020_ID%0A%7D%0A%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-170384%22%20slang%3D%22en-US%22%3ERe%3A%20Getting%20a%20file's%20metadata%20field%20values%20using%20Pnp%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-170384%22%20slang%3D%22en-US%22%3E%3CP%3EI've%20just%20did%20a%20quick%20test.%20The%20following%20gives%20me%20the%20expected%20value%20from%20a%20custom%20column%20in%20one%20of%20my%20files%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3EPS%20C%3A%5CUsers%5CPaul%26gt%3B%20Connect-PnPOnline%20https%3A%2F%2Fmytenant.sharepoint.com%2Fsites%2Ftest%20-Credentials%20%24credentials%0APS%20C%3A%5CUsers%5CPaul%26gt%3B%20%24f%20%3D%20Get-PnPFile%20-Url%20%22%2Fsites%2Ftest%2FLibrary%2FDocument.pdf%22%20-AsListItem%0APS%20C%3A%5CUsers%5CPaul%26gt%3B%20%24f%5B%22CustomField%22%5D%0ACustom%20Field%20Value%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHope%20this%20helps!%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New Contributor

Using Powershell Pnp and SharePoint Online, I want to get the values in the custom fields of a file. I do not want to download the file, I just need the custom (metadata) fields.

 

For example, I have a custom field in a document library "Customer ID" and a file in that library named "CustomerFile.xlsx".

 

If I try Get-PnpFolderItem, it gives me back a few standard attributes (Name, Type, Size, Last Modified), but none of the custom fields.

 

If I try Get-PnpFile with the -Url and -AsListItem, it gives me back the ID, Title, and GUID only.

 

Is there a way to use Pnp to get the value of "Customer ID" (i.e. Customer_x0020_ID) for a given file that I know exists?

 

2 Replies
Highlighted
Solution

I've just did a quick test. The following gives me the expected value from a custom column in one of my files:

 

PS C:\Users\Paul> Connect-PnPOnline https://mytenant.sharepoint.com/sites/test -Credentials $credentials
PS C:\Users\Paul> $f = Get-PnPFile -Url "/sites/test/Library/Document.pdf" -AsListItem
PS C:\Users\Paul> $f["CustomField"]
Custom Field Value

 

Hope this helps!

Highlighted

Thanks, that's similar (but simpler) than the method I eventually came up with.

Since I am pulling in documents from a CAML query, I did this:

 

(Get-PnpListItem -List "My Documents" -Query $docsQuery).FieldValues | Foreach-Object -Process {
    $docFilename = $_.FileLeafRef
$customerID = $_.Customer_x0020_ID }