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. });