Pages

Wednesday, August 24, 2011

SharePoint 2010: Postback to the same page from Ribbon

Most of the examples related to ribbon button actions involve opening dialog. However, if you need to send post back request to the same page/dialog where the ribbon exists, you need to trick a bit. Let’s explain how you can do so.

Define Ribbon XML

You can define the postback action in the command action in Ribbon xml file. To do so, you can use javascript function _doPostBack(‘token’,’’). This will send a postback request to the same page.

<CommandUIHandler
Command="MyButton.Command"
CommandAction="javascript:__doPostBack('MyButtonPostback','');"
EnabledScript="true" />
Figure 1: PostBack action defined in Ribbon xml

 

Identify the PostBack request in the page

The second step is to identity from code behind file, if the postback occurred from ribbon button’s action. From the page load, you can check if the postback occurred due to the command “MyButton.Command”. To do so we can have a check shown below in the page load method.

if (Page.Request["__EVENTTARGET"] == "MyButtonPostback")
{
//The ribbon button initiated the postback
}

 

 

Though this is very simple trick but might be helpful for someone.

1 comment:

Note: Only a member of this blog may post a comment.