Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect in Sharepoint 2010 Event Receiver

I'm looking at some code which has broken the saving of changes of a List item in SP2010. In the constructor of the Event Receiver the HttpContext is being stored as a local variable then in the ItemUpdating at the end a query parameter containing the return url is retrieved and passed the SPUtility.Redirect(...). This has the effect of canceling any changes that were made in the edit form. So following so scenarios I've found online I'm storing the HttpContext as a static class object and redirecting in the ItemUpdated event and edits are now persisted followed by a redirect to where we are expecting.

I have a concern though with storing a specific user's HttpContext as a static on a class which will potentially be hijacked if another user does the same action between the time user 1 sets the context initially and prior to the redirect. User 2 will overwrite the context and there are potential issues.

The only other option I can think of as a quick way out would be to store a static Dictionary where the key is the user id then remove the entry just prior to redirecting so as to not leave stray HttpContext instances leaking.

So looking at the accepted solution from here the first response by servy42 states The fact that it works for a few trivial test cases at first doesn't make it a viable solution. but doesn't suggest a viable solution.

There's another option best approach may be changing Save Button in ribbon such that when clicked, a ECMA script first save your Item, then redirect to other page, or open other page in dialog. suggested further down but is that the only other way to go?

I'm fairly new to SP and just trying to navigate through the wealth of wrong ways of doing things with my limited knowledge, whilst unfortunately being stuck with SP2010.

Any thoughts on how to go about this?

============

Edit: Further info as requested

So there are two sites one say called A which will have a sub site called B. A has a List and adding an item to that list triggers event receivers which creates sub site B and other related stuff. If I modify the B "item" from the SharePoint list of Bs then I don't need to redirect and returning to the list is fine. If I'm looking and a neatly rendered Gantt chart on A listing the Bs the if I edit B after clicking Save we need to come back to A. If I'm on B there's an edit button as well so I want to return to B after editing and clicking save.

So we have the source= appended when we go to the edit screen. From some reading I've done it seems SharePoint will pick that up and redirect. One of the fields on the edit page is the relative url beneath the A site and if that changes then the redirect url will no longer be valid and throw a 404 so we replace the old B sub path with the newly entered value. But we then need code to redirect to the new location.

like image 426
Stephen York Avatar asked May 03 '17 00:05

Stephen York


People also ask

What is Event receiver in SharePoint?

In an SharePoint Add-in model scenario, event receivers are created outside of SharePoint inside a web service and registered with SharePoint. These are called as Remote Event Receivers (RER). In this scenario, the event receiver code runs on the web server where the web service is hosted.

What are the type of event receivers available in SharePoint list?

There are two types of Event Receiver in SharePoint, Synchronous Event Receiver. Asynchronous Event Receiver.

Can we use Event receiver in SharePoint online?

In fully trusted code solutions, you can run event receivers on the SharePoint server. In the new SharePoint Add-in model, because you cannot run the event receiver on the SharePoint server, you need to implement a remote event receiver on a web server.

How do I add an event receiver to a SharePoint list?

Select event receiver and enter the title of the event receiver. Click the Add button. Select List Item Events in the type of event receiver and select the custom list option from the event source. Then select "an item was added" in events and click Finish.


1 Answers

I am not quite sure whether i understand your requirements correctly. But i will give it a try. Just a quick summary of what i understood (correct me if i am wrong...):

  • You have a Root Web in a site collection with a list
  • Each entry in that list corresponds to a subsite which will be created on List Item creation (ER) below the Root Web as a subsite
  • List items can be edited from both the List in the root web as well as from the subsite it maps to
  • On editing from the subsite you want to return to the subsite after saving

My approach would be this:

  1. Follow the steps described in this article SP Dialog and List Forms
  2. Open a dialog on the subsite that links to the edit form of the root web.
  3. After saving the user finds himself already where he should be

Hope this helps... Cheers!

Edit: Just read the requirement about changing of url. Create a custom Edit Form which you enhance with some js that passes values back to the origin site -> Parameters in dialogs. You may then react accordingly in the javascript callback and redirect to the new url.

like image 133
tashunko Avatar answered Dec 06 '22 01:12

tashunko