[EP] POST HTML Form from SharePoint EP

 Today's topic: say you have a request to post a form from EP to an external page; the scenarios may be different from case to case, however, you are also required to perform this when the EP page opens (no other clicks are required in the flow).
 My particular case here: you have to send a form to an external credit card processor for a sales order  e-payment.

 Solution: this is the short version, the long one should come as a .ascx/ascx.cs file:

1. In the code behind file of your web control for the page:
  protected void Page_Load(object sender, EventArgs e)
    {
        // declare first the condition that will dictate if we are to execute the script
        if (recId == 0)
        {
            lblMessage.Text = "You must have a valid sales order.";
        }
        else
        {
            lblMessage.Text = "You are in the process of paying for your purchase. Thank you.";
            Page.ClientScript.RegisterStartupScript(typeof(AxForm), "Load",
                "<script type=\"text/javascript\">submitForm('" + formOrder.ClientID + "');</script>",
                false);
        }
        if (!Page.IsPostBack)
        {
            formOrder.InnerHtml = "your html text like : <input name="ORDER_DATE" type="hidden" value="2012-01-10 17:30:56"/>" ;
        }
    }


2. In the .ascx file of your web control:
...
 <script type="text/javascript">
  function submitForm(containerElement) {
          var theForm = document.createElement("form");
          theForm.action = 'external_web_url';
          theForm.method = "post";
          theForm.target = "_blank";
          theForm.innerHTML = document.getElementById(containerElement).outerHTML;
          var formToSubmit = document.body.appendChild(theForm);
          formToSubmit.submit();
  }
</script>
<div runat="server" id="formOrder">
</div>

No comments:

Post a Comment