How to get records of the same customer with two or more records in SharePoint using PowerShell and CAML query?

35 Views Asked by At

Get records from SharePoint using PowerShell

I have been trying to get records from SharePoint using PowerShell. With below query, I'm getting records where "customer_number" is not null. Instead, I want to get records where same customer having two or more records and not required to get records where customer having single records.

$Query.Query = "@
<Where>
    <And>
        <And>
            <And>
                <And>
                    <Eq><FieldRef Name='Status' /><Value Type='Lookup' >Unassigned</Value></Eq>
                    <Eq><FieldRef Name='channel' /><Value Type='Text' >CCOM</Value></Eq>
                </And>
                <Eq><FieldRef Name='RemType' /><Value Type='Text' >Remuy</Value></Eq>
            </And>
            <And>
                <Geq><FieldRef Name='Created' /><Value Type='DateTime' IncludeTimeValue='True'>2023-04-24T00:00:00Z</Value></Geq>
                <Leq><FieldRef Name='Created' /><Value Type='DateTime' IncludeTimeValue='True'>2023-04-24T23:59:59Z</Value></Leq>
            </And>
        </And>
        <IsNotNull><FieldRef Name='customer_number' /></IsNotNull>
    </And>
</Where>
"


$ListItems = $List.GetItems($Query) 
Write-host "Total Number of Items: "$ListItems.count

$dt = $ListItems.GetDataTable()
$go = $dt | Group-Object "customer_number"
$ad = $go | Sort-Object -Property {$_.Values[1]},{$_.Count} -Descending

$ad | Select-Object -Property {$_.Values[0]},{$_.Count}  |  Export-CSV ".\ExportAlert.csv" -Delimiter '|' -NoType
0

There are 0 best solutions below