TransWikia.com

Getting latest modified item ID and Modified date from SharePoint list using PowerShell

SharePoint Asked by sk5991 on October 25, 2021

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

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

Answered by Baker_Kong on October 25, 2021

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>

Answered by Marko Tica on October 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP