2

I have an event handler that changes the title of the file. However shortly after this event handler ends SharePoint will open the editpage for the user to edit it. However properties.AfterUrl is still pointed at the original file name and is read-only. Is there some clever way to make the edit page redirect to the new name once the event receiver finishes?

My event receiver code:

    public override void ItemAdded(SPItemEventProperties properties)
    {
        SPWeb web = properties.OpenWeb();
        bool allowUpdates = web.AllowUnsafeUpdates; //store original value
        this.EventFiringEnabled = false;
        try
        {
            web.AllowUnsafeUpdates = true;

            var currItem = properties.ListItem;
            string fileName = printDate + username + printTime;
            currItem["Name"] = fileName;
            currItem["Title"] = fileName;

            currItem.Update();


        }
        catch (Exception ex)
        {
            writeLog(ex.Message);
        }
        finally
        {
            this.EventFiringEnabled = true;
            web.AllowUnsafeUpdates = allowUpdates; //restore original value
        }

1 Answer 1

2

Redirecting after an item is added might be tricky. If you would handle ItemAdding event then the only option that I know of to redirect from within an event receiver is to use CancelWithRedirect:

properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
properties.RedirectUrl = "https://www.google.co.in/";

You need to keep in mind that in SharePoint 2013 CancelWithRedirectUrl is obsolete.

There is another approach which is more suitable for SharePoint 2013 - http://blog.sharepointalist.com/2013/03/sharepoint-2013-event-receiver-redirect.html

The form needs to be rendered in CSRRenderMode.ServerRender mode. Then you can use the following code snippet:

public class TestEventHandler : SPItemEventReceiver
 {
  private readonly HttpContext _currentContext;
  public TestEventHandler()
  {
   _currentContext = HttpContext.Current;
  }
  // Methods
  public override void ItemAdding(SPItemEventProperties properties)
  {
   var url = new StringBuilder("test.aspx");
   string urlRedirect = null;
   bool flag = SPUtility.DetermineRedirectUrl(url.ToString(), SPRedirectFlags.RelativeToLayoutsPage, _currentContext, null, out urlRedirect);
   _currentContext.Response.Redirect(urlRedirect, true);
  }
 }

Keep in mind that you would still need to use ItemAdding instead of ItemAdded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.