Forum Discussion

harishpatil's avatar
harishpatil
Brass Contributor
Aug 22, 2022

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.

 

 web.lists.getByTitle("Documents").items.getById(Itemid).versions.get().then(v => {    
        console.log("admin");
        Logger.Log("getByTitle: " + v);  

        Logger.Log("Inside v: " + v);
        Object.keys(v).forEach((key) => {
          console.log(v[key]);
          //Logger.Log("v[key]: " + v[key]);      
          Logger.Log("OData__x005f_CheckinComment: " + v[key].OData__x005f_CheckinComment);
          Logger.Log("OData__x005f_UIVersionString: " +v[key].OData__x005f_UIVersionString);
   
        });
       
        })
        .catch((e) => {

            console.log("adminerror" + e);
            return false;

        });

 

How to get versions and comments of file?

 

Thanks,

Harish Patil

1 Reply

  • harishpatil 

     

    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

     

     

Resources