Thursday, June 25, 2015

Redirect a SharePoint Edit form with ID without using any JavaScript using SharePoint Save feature.

To get ID of an item from a OOB SharePoint Custom edit page, we have to add get this from Query String. To pass it into OOB Save functionality. We have to pass it on the below

To redirect without any XSL variable:

<td class="ms-separator">&#160;</td>
                                                                                                                <td class="ms-toolbar" nowrap="">
                                                                                                                <input type="button" class="ms-ButtonHeightWidth" style="width:13em!important" value="Save and Redirect" name="btnSave" onclick="javascript: {ddwrt:GenFireServerEvent('__commit;__redirect={abc.aspx?Type=New}')}" />

In edit form with ID parameter:

Find this text:
                <xsl:param name="dvt_apos">&apos;</xsl:param>
                <xsl:variable name="dvt_1_automode">0</xsl:variable>

Add below text to store value in an XSL variable:

                <xsl:param name="ListItemId" />
    <xsl:variable name="RedirectLoc">abc.aspx?ItemID=<xsl:value-of select="$ListItemId"/></xsl:variable>

<input type="button" class="ms-ButtonHeightWidth" style="width:16em!important"  value="Save and Redirect" name="btnSave" onclick="javascript: {ddwrt:GenFireServerEvent(concat('__commit;__redirect={',$RedirectLoc,'}'))}"/>

Now if you wan tot fire PreSaveAction in addition to it, here are the important links to understand what is happening. Below I am writing what we need to add on our page:

Add this function to your JS of form:

function PreSaveItem()
       if ("function"==typeof(PreSaveAction))
         return PreSaveAction();
     return true;

Then change your button to below code:

<input type="button" class="ms-ButtonHeightWidth" style="width:16em!important" value="Save and Redirect" name="Save and Redirect" onclick="if (!PreSaveItem()) return false;{ddwrt:GenFireServerEvent(concat('__commit;__redirect={',$RedirectLoc,'}'))}" />

Now PreSaveAction will also be fired before redirecting. Also in case of multiple save button on forms, we can write something Just before PreSaveAction.