Forum Discussion
How to get file versions and comments
Hi All,
I am SharePoint online site, SPFX and React. I am trying below code to get versions and comments but in comments I am getting same comment to all version which is latest. Not getting why we gets same comments to all versions. Is there any other way to get this.
How to get versions and comments of file?
Thanks,
Harish Patil
1 Reply
- kalpeshvaghelaIron Contributor
This is the default behavior of versions method of pnp js when you use with list item.
You should try versions method associated with file. I have achieved it using below code:
const sp = spfi().using(SPFx((this.props.wpContext as unknown) as ISPFXContext)) const currentItem = await sp.web.lists.getByTitle("VersionDemo").items.getById(2).select('FileRef')(); // Get latest version comment const lastestVersionComment = await sp.web.getFileByUrl(currentItem['FileRef'])(); console.log(lastestVersionComment["UIVersionLabel"],lastestVersionComment['CheckInComment']); // In this method it will not return latest version it will return only previous version const olderVersionComments = await sp.web.getFileByUrl(currentItem['FileRef']).versions(); olderVersionComments.forEach((currentVersion) => { console.log(currentVersion["VersionLabel"],currentVersion["CheckInComment"]); });
Find sample solution at "versioncomment"
Important: I have used latest version of PnP JS i.e. 3.6.0. If you are using older version of pnp js then you need to adjust your code accordingly.
Hope it will helpful to you and if so then Please mark my response as Best Response & Like to help others in this community