Forum Discussion

buszi99's avatar
buszi99
Brass Contributor
Sep 20, 2023

Rest API filter by two and more columns

Hi. I want to filter a Site Page library by SharePoint Rest Api in PowerShell. I made script like this

$varList = 'Site Pages'
$URL = "/_api/web/lists/GetByTitle('$varList')/items?`$select=Title,Modified,ContentType,ECCKAssetID,ID,Created&`$filter=ContentType eq 'Site Page' &&Modified lt '2023-06-09'"
$items=Invoke-PnPSPRestMethod -Connection $connection -Method Get -Url $URL -ContentType "application/json;odata=verbose"
I want to filter results by Last Modified Date and theirs content type. When I run this script i get pages with content type that's ok but modified date not equals in filter.
Please help. 

 

  • SvenSieverding's avatar
    SvenSieverding
    Bronze Contributor

    Hi buszi99 

    It works if you use "and" instead of "&&"

    $URL = "/_api/web/lists/GetByTitle('$varList')/items?`$select=Title,Modified,ContentType,ID,Created&`$filter=ContentType eq 'Site Page' and Modified lt '2023-06-09'"


    Best Regards,
    Sven

     

  • LeonPavesic's avatar
    LeonPavesic
    Silver Contributor

    Hi buszi99,

    To filter a Site Page library by SharePoint Rest Api in PowerShell by two and more columns, you can use the following steps:

    1. Create a new PowerShell script.
    2. Import the SharePoint PnP PowerShell module.
    3. Connect to your SharePoint site using the Connect-PnPOnline cmdlet.
    4. Create a variable for the Site Page library name.
    5. Create a variable for the REST API URL. The URL should include the following parameters:
      • $select: This parameter specifies the fields that you want to return in the response.
      • $filter: This parameter specifies the filter criteria that you want to use.
    6. Use the Invoke-PnPSPRestMethod cmdlet to call the REST API and retrieve the Site Page items.
    7. Save the script and run it.

    Here is an example of a PowerShell script that filters a Site Page library by Last Modified Date and Content Type:

     

     

    # Import the SharePoint PnP PowerShell module.
    Import-Module SharePointPnPPowerShellOnline
    
    # Connect to your SharePoint site.
    Connect-PnPOnline -Url "https://contoso.sharepoint.com/sites/teamsite"
    
    # Create a variable for the Site Page library name.
    $varList = "Site Pages"
    
    # Create a variable for the REST API URL.
    $URL = "/_api/web/lists/GetByTitle('$varList')/items?`$select=Title,Modified,ContentType,ECCKAssetID,ID,Created&`$filter=ContentType eq 'Site Page' && Modified le '2023-06-09'"
    
    # Use the Invoke-PnPSPRestMethod cmdlet to call the REST API and retrieve the Site Page items.
    $items = Invoke-PnPSPRestMethod -Method Get -Url $URL -ContentType "application/json;odata=verbose"
    
    # Display the Site Page items.
    foreach ($item in $items) {
        Write-Host "$($item.Title) - $($item.Modified)"
    }

     


    I recommend using SharePoint online Management Shell opened in elevated mode (as administrator):

    Download SharePoint Online Management Shell from Official Microsoft Download Center


    Please click Mark as Best Response & Like if my post helped you to solve your issue.
    This will help others to find the correct solution easily. It also closes the item.


    If the post was useful in other ways, please consider giving it Like.


    Kindest regards,


    Leon Pavesic

     

Resources