Connected Webparts In SharePoint Hosted Apps

Sometimes, we need to send the data from one web part to another web part for filtering/display. In SSOM, we do have connected web parts using interface. But in Apps, we don't have something by default. We need to use postMessage and event listeners to achieve the same.

Requirement

I have a client add-in (Sender), where users fill the form like Name, Age, Company etc. and on clicking the Submit button, we have to show the data in another client add-in (Receiver).

  • Create a SharePoint Hosted App.

  • Creating Sender Web part, add a Client Web part and name it as Sender.

    • Which will add a Web part, Element.xml, and an ASPX page.

    • Add all HTML elements for collecting the data.

    • Create a JavaScript file (name it Sender.js) and paste the below code and add reference to the Sender.aspx page in it. We have to capture the details entered by a user into the object and send to the host web using postMessage.

      1. $(document).ready(function () {    
      2.    $("#Send").on('click'function () {    
      3.    var appMsg = {    
      4.       metadata:"AppSender",    
      5.       Name: $("#Name").val(),    
      6.       Age: $("#Age").val(),    
      7.       Company: $("#Company").val()    
      8.    };    
      9. window.parent.postMessage(appMsg, "*");    
      10. })    
      11. });    
  • Creating Receiver Webpart, add a Client Webpart (name it Receiver) which will add Webpart, Element.xml and aspx page.

    • Add all HTML elements for displaying data.

    • Create a JavaScript file (name it Receiver.js) and paste the below code. Add reference to Receiver.aspx. We have added an event listener and calling a callback function to assign the data to HTML elements.

      1. $(document).ready(function () {     
      2.     
      3.           window.addEventListener("message", receiveMessageInApp, false);     
      4.       function receiveMessageInApp(event) {    
      5.       var dataReceived = event.data;    
      6.       $("#metadata").html(dataReceived.metadata);    
      7.       $("#Name").html(dataReceived.Name);    
      8.       $("#Age").html(dataReceived.Age);    
      9.       $("#Company").html(dataReceived.Company);    
      10. }    
      11.     
      12. });    
  • Deploy the Solution and add two web parts onsite page inside your Hosted Web

  • On the same page, add script editor webpart and add the below code. We are adding event listener to the host web window and capturing the data sent by sender webpart and checking the metadata property (custom property created in the object). If matched, then it is sending message to the receiver app by using iframe postMessage.

    1. <script src="/sites/DeveloperSite/SiteAssets/scripts/jquery-1.9.1.min.js"></script>  
    2. <script>  
    3.     $(document).ready(function() {  
    4.         window.addEventListener("message", receiveMessage, false);  
    5.   
    6.         function receiveMessage(event) {  
    7.             var receivedData = event.data;  
    8.             //console.log(receivedData);    
    9.             if (receivedData.metadata && receivedData.metadata == "AppSender") {  
    10.                 procesHello(receivedData);  
    11.             }  
    12.         }  
    13.   
    14.         function procesHello(msgData) {  
    15.             //console.log(msgData);    
    16.             var iFrame = document.getElementsByTagName("iframe")[1];  
    17.             iFrame.contentWindow.postMessage(msgData, "*");  
    18.         }  
    19.     });  
    20. </script>