1

For example: I have a following SharePoint list:

ID FileName Modified

11 Text.docx 15 Aug 2018

13 Text2.docx 25 May 2013

14 Text3.docx 12 Mar 2011

My output should look like this: List ID 11 and Modified on 15 Aug 2018

I tried something below but in the section, I have added which gives all the update done in the last 30 days. However this doesn't satisfies my example's output above:

$SiteURL="https://abc.sharepoint.com/site/sitename $ListName="Testing"

Connect-PnPOnline -Url $SiteURL -Credentials $Credentials

$Query="<View <Query <Where <Gt <FieldRef Name='Modified' Type='DateTime'/ <Value Type='DateTime' IncludeTimeValue='TRUE' <Today OffsetDays='-30'/ </Value </Gt </Where </Query </View"

$ListItems = Get-PnPListItem -List $ListName -Query $Query Write-host "Total Number of Items Found:"$ListItems.count

$ListItems | ForEach-Object { Write-host ("List Item: {0} was Modified on {1}" -f $["FileLeafRef"],$["Modified"]) }

Please help.

2 Answers 2

0

Try with this query. First you sort by latest Modified and take 1 item (latest item).

<View>
<Query>
<OrderBy><FieldRef Name='Modified' Ascending='FALSE' /></OrderBy>
</Query>
<ViewFields>
    <FieldRef Name='ID'/>
    <FieldRef Name='Modified'/>
</ViewFields>
<RowLimit>1</RowLimit>
</View>
0

i understand you want to get the lastest modified item, and you prefer to use pnp powershell. If so, please take a referecne of below script:

$Username='[email protected]'
$Password = 'xxxxxxxx'

#region Credentials
[SecureString]$SecurePass = ConvertTo-SecureString $Password -AsPlainText -Force 
[System.Management.Automation.PSCredential]$PSCredentials = New-Object System.Management.Automation.PSCredential($Username, $SecurePass) 
#endregion Credentials

#connect to hubsite
Connect-PnPOnline -Url 'https://xxx.sharepoint.com/sites/s01' -Credentials $PSCredentials



$listName = "listname" 
$caml=@"
    <View>
  <Query>
   <OrderBy>
      <FieldRef Name='Modified' Ascending='False' />
   </OrderBy>
</Query>
</View>
"@

Get-PnPListItem -List $listName -Query $caml| select -first 1
1
  • 1
    Thank u so much!!! It worked for me
    – sk5991
    Commented Jul 20, 2020 at 12:00

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.