Forum Discussion

VISKR05's avatar
VISKR05
Copper Contributor
May 12, 2023

How to open version history in pop up in SPFx react project ?

Hi All,

I have a anchor tag on the page with href = siteurl/_layouts/15/Versions.aspx?list=listGUID&ID=200. 

This opens in a separate tab. Please help me to open the version.aspx page as pop up to see the version history of the item. Same as when we click to Version history in the SharePoint, It opens up as popup. 

I tried this  - SP.UI.ModalDialog.showModalDialog(options) 

but this does not support in Spfx. Request you to please help me to get the solution to see item version history when click in anchor tag in SPFx react solution.

 

Thank you !!

4 Replies

  • VISKR05 

    In SharePoint Framework (SPFx) with React, you can use the `SPFxDialog` component from the `@pnp/spfx-controls-react` library to open the version history page as a popup. Here's how you can achieve this:

     

    1. Install the necessary packages: Run the following command in your SPFx project directory to install the required packages:

     

    npm install @pnp/spfx-controls-react

    2. Import the required modules: In your React component file, import the necessary modules as follows:

    import { SPFxDialog } from '@pnp/spfx-controls-react/lib/SPFxDialog';
    import { WebPartContext } from '@microsoft/sp-webpart-base';

     

    3. Define the dialog state: Add a dialog state property to your component's state to control the visibility of the dialog:

    export interface IYourComponentState {
    isDialogOpen: boolean;
    }

    4. Open the dialog on anchor click: In your component's render method, set up an anchor tag with an onClick event handler to open the dialog:

    public render(): React.ReactElement<IYourComponentProps> {
    return (
    <div>
    <a href="#" onClick={this.openDialog}>Open Version History</a>
    <SPFxDialog
    context={this.props.context}
    isOpen={this.state.isDialogOpen}
    title="Version History"
    width={800}
    height={600}
    onDismiss={this.closeDialog}
    >
    <iframe
    src={this.getVersionHistoryUrl()}
    width="100%"
    height="100%"
    frameBorder={0}
    />
    </SPFxDialog>
    </div>
    );
    }

    5. Implement the dialog methods: Add the necessary methods to open and close the dialog:

    private openDialog = (): void => {
    this.setState({ isDialogOpen: true });
    };
    
    private closeDialog = (): void => {
    this.setState({ isDialogOpen: false });
    };
    
    private getVersionHistoryUrl = (): string => {
    const { listGUID, itemID } = this.props; // Get the list GUID and item ID
    
    return `${this.props.context.pageContext.web.absoluteUrl}/_layouts/15/Versions.aspx?list=${listGUID}&ID=${itemID}`;
    };

    Make sure to replace `listGUID` and `itemID` in the `getVersionHistoryUrl` method with the actual list GUID and item ID you want to display the version history for.

     

    With this implementation, when the user clicks the "Open Version History" link, the dialog will open, displaying the version history page in an iframe within the dialog.

     

    Remember to adjust the code according to your component's structure and requirements.

     

    If I have answered your question, please mark your post as Solved
    If you like my response, please give it a like
    • Pavel2235's avatar
      Pavel2235
      Copper Contributor

      Deleted Could you tell please, is it possible to implement, using ListView Command Set extension?

    • VISKR05's avatar
      VISKR05
      Copper Contributor
      Thank you so much for your reply.
      I am able to open IFrameDialog. but please help me how to hide some contents like left navigation when IFrameDialog opens. below is my code:
      <IFrameDialog
      url={this.state.listGUID}
      iframeOnLoad = {this._onIframeLoaded.bind(this)}
      hidden={this.state.hideiframedialog}
      onDismiss={() => this._closeIFrameDialog()}
      modalProps={{
      isBlocking: true,
      }}
      dialogContentProps={{
      type: DialogType.close,
      showCloseButton: true,
      title: 'Version History',
      }}
      width={'900px'}
      height={'550px'} />

      //On IFrame load, below code
      protected async _onIframeLoaded (){
      setTimeout(() => {
      var pageId = document.getElementById('sideNavBox');
      alert(pageId);
      document.getElementById("sideNavBox")[0].setAttribute("style", "display:none !important");
      },20000);
      };
      but it's not working..
      • Deleted's avatar
        Deleted
        In the URL, please add the parameter "&IsDlg=1" , if this is present then, sharepoint itself will hide the other areas

Resources