Popup Documents using PnP IFrameDialog Control in SPFx

This article provides steps to implement the Popup Documents using PnP IFrameDialog Control in the SharePoint Framework (SPFx) Field Customizer Extension, generally, PnP IFrameDialog control renders a Dialog with an iframe as content. In this article, we opening documents in the popup from SharePoint document library

Create a new extension project

Open power shell and run following comment to create a new web part by running the Yeoman SharePoint Generator

yo @microsoft/sharepoint

When prompted:

Accept the default value of field-extension as your solution name, and then select Enter.
Select SharePoint Online only (latest), and select Enter.
Select Use the current folder, and select Enter.
Select N to not allow solution to be deployed to all sites immediately.
Select N on the question if solution contains unique permissions.
Select Extension as the client-side component type to be created.
Select Field Customizer as the extension type to be created

The next set of prompts ask for specific information about your web part:

Enter your web part name, and then select Enter.
Enter your web part description, and then select Enter.
Select React framework as the framework you would like to use, and then select Enter.

Start Visual Studio Code (or your favorite code editor) within the context of the newly created project folder.

cd .\web part name\
code .

Install the library and required dependencies

npm install @pnp/spfx-controls-react --save --save-exact

Configure the custom properties

In the react component properties interface add some more props for transfer values from BaseFieldCustomizer to React component, also create one state interface

export interface ISpfxPnpIframedialogExtensionProps {
  FileName: string;
  FileURL: string;
}

export interface ISpfxPnpIframedialogExtensionState {
  shouldhide: boolean;
}

Add some import statements to import

import { Log } from '@microsoft/sp-core-library';
import { override } from '@microsoft/decorators';
import * as React from 'react';
import { IFrameDialog } from "@pnp/spfx-controls-react/lib/IFrameDialog";
import { DialogType, IconButton, IIconProps } from 'office-ui-fabric-react';
import { autobind } from 'office-ui-fabric-react/lib/Utilities';
import styles from './SpfxPnpIframedialogExtension.module.scss';

Replace this render function with the following code.

  @override
  public render(): React.ReactElement<{}> {
    return (
      <div className={styles.cell}>
        <IconButton iconProps={emojiIcon} onClick={this._alertClicked} />
        <IFrameDialog
          url={this.props.FileURL}
          hidden={this.state.souldhide}
          onDismiss={() => this.setState({ shouldhide: true })}
          modalProps={{
            isBlocking: false
          }}
          dialogContentProps={{
            type: DialogType.close,
            showCloseButton: true
          }}
          width={'1000px'}
          height={'600px'} />

      </div>
    );
  }

place the below code inside the react component code, this event function set values in the react state

  @autobind
  private _alertClicked() {
    this.setState({ shouldhide: false });
  }

Also you have to change the feild value, feild group name and feild type. in the file of elements.xml located under sharepoint\assets\

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <Field ID="{c78fa9c1-6875-47d4-9c47-f9da19edc564}" Name="SPFxPopup" DisplayName="Popup" Type="Calculated" Min="0" Required="FALSE" Group="SPFx Columns" ClientSideComponentId="23ef2026-7f63-40d4-9b6a-11bbea6c1ec2">
        <Formula>="value"</Formula>
    </Field>
</Elements>

Debug the Field Customizer Extension

In the config folder we have to change some values in the server.json, you can find there are two set of two set of configuration, this will requred only when we debug this in many lists or tenant. in the below code you can find the word of Popup this should be your field name and rest everything leave as it is. replace the list list where you going to debug the Field Customizer

{
  "$schema": "https://developer.microsoft.com/json-schemas/core-build/serve.schema.json",
  "port": 4321,
  "https": true,
  "serveConfigurations": {
    "default": {
      "pageUrl": "https://ravichandran.sharepoint.com/sites/TheLanding/Policies/Forms/AllItems.aspx",
      "fieldCustomizers": {
        "Popup": {
          "id": "23ef2026-7f63-40d4-9b6a-11bbea6c1ec2",
          "properties": {
            "sampleText": "Value"
          }
        }
      }
    }
  }
}

Run below comment

gulp serve

This will redairected to the list you have mention in the server.json file, and Accept the loading of debug manifests by selecting Load debug scripts when prompted.

Deploy the solution

You’re now ready to build, bundle, package, and deploy the solution.

Run the gulp commands to verify that the solution builds correctly.

gulp build

Use the following command to bundle and package the solution.

gulp bundle
gulp package-solution

Browse to the app catalog of your target tenant and upload the solution package. You can find the solution package under the sharepoint/solution folder of your solution. It is the .sppkg file. After you upload the solution package in the app catalog. goto the site content and select add app and search your app and select to install.

Goto the list settings and Under Columns, select Add from existing site columns

Under the SPFx Columns group, select the Popup field that was provisioned from the solution package, and then select OK. now you can access your feild Customized column in the default list view

Sharing is caring!

If you have any questions, feel free to let me know in the comments section.
Happy coding!!!

Comments