0

I'm trying to display only documents from a library named "How to get it done" and I want to show all document types that have the Managed Metadata tag of "HFM".

Some of my documents have multiple tags on them. I need to include all documents that have the tag, regardless of whether there are other tags on the document.

I'm using O365 SharePoint and cannot seem to get the CAML query to work correctly. I have created the Term Set for the site and have the Term tagged to the document in the library.

What query I should use?

I've tried to manipulate several examples that I've found on forums but to no avail. I'm not good with queries in general, so I need the help.

Thanks in advance!

1 Answer 1

0

The CAML Query as below.

<View>
    <Query>
        <Where>
            <Contains>
                <FieldRef Name='ManagedMetadata' /><Value Type='Text'>HFM</Value>
            </Contains>
        </Where>
    </Query>
</View>

The following example PowerShell Script about getting file items using CAML Query above from library for your reference.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Set parameter values
$SiteURL="https://tenant.sharepoint.com/sites/team"
$ListName="DL"

#Get Credentials to connect
$Cred= Get-Credential

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Get the List
$List = $Ctx.Web.lists.GetByTitle($ListName)

#Define the CAML Query
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "@
<View>
    <Query>
        <Where>
            <Contains>
                <FieldRef Name='ManagedMetadata' /><Value Type='Text'>HFM</Value>
            </Contains>
        </Where>
    </Query>
</View>"

#Get All List Items matching the query
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()

Write-host "Total Number of Items:"$ListItems.count

#Loop through each List Item
ForEach($Item in $ListItems)
{
    Write-host "FileName:"$Item["FileLeafRef"]
}
4
  • This is very helpful! Thanks! But when I put input the query, I'm getting the following: X "A column that you are using as a filter has been removed."
    – K.M.I.
    Commented Jan 6, 2020 at 20:10
  • The "ManagedMetadata" is internal field name, you need modify it.
    – LZ_MSFT
    Commented Jan 7, 2020 at 1:04
  • What do you mean by "modify it". Sorry I'm new to this... do you mean rename it? I really, really appreciate the help on this!
    – K.M.I.
    Commented Jan 16, 2020 at 22:38
  • I mean, you need check the metadata column internal name and rename the "ManagedMetadata" in my code to your column internal name.
    – LZ_MSFT
    Commented Jan 17, 2020 at 1:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.