Wednesday, May 4, 2011

Redirection from SharePoint Control to application page

I was recently working on a requirement where when user clicks on a link, we need to redirect the user to a SharePoint page in the layouts folder where we were doing further processing depending on the business requirement. The link was was embedded in the master page of the site so that it's globally available to the user.

Initially it looked to be a simple requirement and we thought of implementing it using SPControl. On the click of the link we need to pass some parameter to the application page for further processing.

When we implemented the code, and on the click of the link (we redirected using javascript) when we redirected user to the application page we got error related to the view state of the control. We realized that the simple redirect will not serve the purpose.

In order to achieve this requirement (thanks to my architect "Thiya" for suggesting this), we created a form using javascript and posted the same to the required url (application page). On the application page (.aspx) we defined a form. On the code behind we retrieved the value from the request from the previous page. Below is the code snippet for posting the form using javascript from the spcontrol.
I had written the below code in the render method (can be switched anywhere else as per convenience)

string id = this.Page.ClientID + "_ProfilebtnLink1";
writer.WriteLine("");


On the application page, define a form (in the page).
form id ="test"

In the code behind get the value which was posted as below (I did in teh apge laod event)
userName = Request["UID"];

Hope this will help.. :)