Thursday 7 November 2013

Change the Redirection URL of SharePoint Buttons

When you open a site using SharePoint designer and view one of its site pages, for example EditForm.aspx, you can see the html code of the default OK and Cancel buttons in the HTML Code view. 

The html code of the buttons are:

<SharePoint:SaveButton runat="server" ControlMode="Edit" id="savebutton1" Text="OK"/>

<SharePoint:GoBackButton runat="server" ControlMode="Edit" id="gobackbutton2"/>


In the above html code, you will notice that there are no available attributes / properties for the elements <SharePoint:SaveButton> and <SharePoint:GoBackButton> where you can specify or edit its redirection url. Basically, the redirection url of these buttons are being handled already by SharePoint so there is no way to make the default buttons redirect to a different url.

But there is a workaround to achieve that kind of requirement. Instead of using the default sharepoint buttons, you can use the HTML button <input type="button"> as shown below.

<input type="button" class="ms-ButtonHeightWidth" value="OK" name="btnSave" onclick="javascript: {ddwrt:GenFireServerEvent('__commit;__redirect={}')}" />

<input type="button" class="ms-ButtonHeightWidth" value="Cancel" name="btnCancel" onclick="javascript: {ddwrt:GenFireServerEvent('__redirect={}')}" />


Here we set the class attribute equal to "ms-ButtonHeightWidth" in order to have the same look and feel as of the default SharePoint button. In the __redirect={} part of the onclick attribute value, you can put any URL you like, for example __redirect={http://www.google.com} or __redirect={/news/PressReleases/}. Please take note that there should be no quotes around the URLs passed into the __redirect directive.


Here is the full example:

<input type="button" class="ms-ButtonHeightWidth" value="OK" name="btnSave" onclick="javascript: {ddwrt:GenFireServerEvent('__commit;__redirect={http://www.google.com}')}" />

<input type="button" class="ms-ButtonHeightWidth" value="Cancel" name="btnCancel" onclick="javascript: {ddwrt:GenFireServerEvent('__redirect={/news/PressReleases/}')}" />




   

No comments:

Post a Comment