reactjssharepointsharepoint-onlinespfx

How to send data from one webpart to another webpart using dynamic data in spfx


I have two webparts inside a project i am trying to send a value to another web part. I gone through the documents in microsoft sharepoint on dynamic data . But hardly i get anything.

One webpart will be filtering and another webpart will display the filtered results.I just wanted to know to pass the value so that filtering could occur in second webpart.

Any suggestions will be helpful


Solution

  • Connect SharePoint Framework components using dynamic data

    Sample test demo:

    SourceWebPart:

    import { Version } from '@microsoft/sp-core-library';
    import {
      BaseClientSideWebPart,
      IPropertyPaneConfiguration,
      PropertyPaneTextField
    } from '@microsoft/sp-webpart-base';
    import { escape } from '@microsoft/sp-lodash-subset';
    import { IDynamicDataCallables, IDynamicDataPropertyDefinition } from '@microsoft/sp-dynamic-data';
    import styles from './SourceWebPartWebPart.module.scss';
    import * as strings from 'SourceWebPartWebPartStrings';
    
    export interface IPoint { 
      x: number;
      y: number;
    }
    
    export interface ISourceWebPartWebPartProps {
      description: string;
    }
    
    export default class SourceWebPartWebPart extends BaseClientSideWebPart<ISourceWebPartWebPartProps> implements IDynamicDataCallables{
      protected onInit(): Promise<void> {
    
        this.context.dynamicDataSourceManager.initializeSource(this);
    
        return Promise.resolve();
      }
      public getPropertyDefinitions(): ReadonlyArray<IDynamicDataPropertyDefinition> {
        return [
          {
            id: 'x',
            title: 'Mouse-X'
          },
          {
            id: 'y',
            title: 'Mouse-y'
          }
        ];
      } 
      public getPropertyValue(propertyId: string): number {
        switch (propertyId) {
          case 'x':
            return this.mousePosition.x;
          case 'y':
            return this.mousePosition.y;
        }
    
        throw new Error('Bad property id');
      }
      private mousePosition: IPoint;  
    
      public onMouseClick(e) {  
        this.mousePosition = { x: e.clientX, y: e.clientY };
        this.context.dynamicDataSourceManager.notifyPropertyChanged('x');
        this.context.dynamicDataSourceManager.notifyPropertyChanged('y');
      }
    
      public render(): void {
        this.domElement.innerHTML = `
        <div id="webpartdiv" style="width: 700px; height: 200px; background-color: yellow">
    
        </div>`;
    
    
      this.domElement.onclick=this.onMouseClick.bind(this);
    
      }
    
      protected get dataVersion(): Version {
        return Version.parse('1.0');
      }
    
      protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
        return {
          pages: [
            {
              header: {
                description: strings.PropertyPaneDescription
              },
              groups: [
                {
                  groupName: strings.BasicGroupName,
                  groupFields: [
                    PropertyPaneTextField('description', {
                      label: strings.DescriptionFieldLabel
                    })
                  ]
                }
              ]
            }
          ]
        };
      }
    }
    

    TargetWebPart:

    import { Version } from '@microsoft/sp-core-library';
    import {
      BaseClientSideWebPart,
      IPropertyPaneConfiguration,
      PropertyPaneTextField,
      IWebPartPropertiesMetadata,
      IPropertyPaneConditionalGroup,
      DynamicDataSharedDepth,
      PropertyPaneDynamicFieldSet,
      PropertyPaneDynamicField
    } from '@microsoft/sp-webpart-base';
    import { escape } from '@microsoft/sp-lodash-subset';
    
    import styles from './TargetWebPartWebPart.module.scss';
    import * as strings from 'TargetWebPartWebPartStrings';
    
    import { DynamicProperty } from '@microsoft/sp-component-base';
    
    export interface ITargetWebpartWebPartProps {
      description: string;
      x: DynamicProperty<number>;
      y: DynamicProperty<number>;
    }
    
    export default class TargetWebpartWebPart extends BaseClientSideWebPart<ITargetWebpartWebPartProps> {
    
      public render(): void {
    
        const x: number | undefined = this.properties.x.tryGetValue();
        const y: number | undefined = this.properties.y.tryGetValue();
        console.log(x);
        this.domElement.innerHTML = `
          <div class="${ styles.targetWebPart }">
            <div class="${ styles.container }">
              <div class="${ styles.row }">
                <div class="${ styles.column }">
                  <span class="${ styles.title }">TargetWebpart</span>                 
                    <div>Mouse X: ${ x == undefined ? '0' : x }</div>
                    <div>Mouse Y: ${ y == undefined ? '0' : y }</div>
                </div>
              </div>
            </div>
          </div>`;
      }
    
      protected get dataVersion(): Version {
        return Version.parse('1.0');
      }
    
      protected get propertiesMetadata(): IWebPartPropertiesMetadata {
        return {
          'x': {
            dynamicPropertyType: 'number'
          },
          'y': {
            dynamicPropertyType: 'number'
          }
        };
      }
    
      protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
        return {
          pages: [
            {
              groups: [
                {
                  groupFields: [
                    PropertyPaneDynamicFieldSet({
                      label: 'Select data source',
                      fields: [
                        PropertyPaneDynamicField('x', {
                          label: 'Position x'
                        })
                      ]
                    }),
                    PropertyPaneDynamicFieldSet({
                      label: 'Select data source',
                      fields: [
                        PropertyPaneDynamicField('y', {
                          label: 'Position y'
                        })
                      ]
                    })
                  ]
                }
              ]
            }
          ]
        };
      }
    }
    

    enter image description here

    Or

    Sharing data between SPFx web parts