none
Unable doing a page redirect in ItemAdding after item is added

    Question

  • My problem is we are trying to add an Item to the document library and then kick off a custom page,
    which will then allow the user to set some custom variables, before starting a workflow associated with the newly added
    document.

    The problem I'm having is that if I add the item to the list and call the update method, the system exits the function without
    doing the page redirect.

    If I donot update the item, the redirect carries out , but the item is not add to the document library. According to blogs
    on the net I have seen the code below should work, any help would be appreciated as this is driving me batty.


    namespace KboDocLibEvtHandler
    {
        /// <summary>
        /// This class is used to plug into sharepoint and handles the
        /// ItemAdded event for the document library that it is added to
        /// </summary>
        public class KboDocLibEvtHandlerClass : SPItemEventReceiver
        {
            HttpContext hContext = null;

            string strComment = string.Empty;

            public KboDocLibEvtHandlerClass()
            {
                hContext = HttpContext.Current;
            }

            // This overides the Sharepoint Item event
            public override void ItemAdded(SPItemEventProperties properties)
            {
                //base.ItemAdded(properties);
            }

            // The other item funstions are overriden but still but go
            // to there default implementations
            public override void ItemAdding(SPItemEventProperties properties)
            {
                //perform validation if required.
               
                // get the list which item to be added
                SPSite objsite = new SPSite(properties.SiteId);
                SPWeb objweb = objsite.OpenWeb(properties.RelativeWebUrl);
                SPList objlist = objweb.Lists[properties.ListId];

                //use this method to disable reoccurence of events.

                DisableEventFiring();
                SPListItem itemToAdd = objlist.Items.Add();

                //add item to list

                itemToAdd.Update();
                EnableEventFiring();

                //redirect it to your new destination like newly provisioned sub site or any other page you want.

                SPUtility.Redirect("http://sharepointsrv:20766/_layouts/kboWeb/KboUpload.aspx", SPRedirectFlags.Trusted, hContext);
            }

        }
    }
    Friday, August 21, 2009 9:44 AM

Answers

  • -My understanding of the problem is the redirect will kill the thread and therefore the list item won't be added;
    it looks like the best solution is to manually add the list item as described in this post ("Redirecting User on ItemAdding in SharePoint EventHanlders") 
    -Of course an alternative is to create your custom new form that will be redirected when you click on the ok button.



    Serge Luca; blog: http://www.redwood.be

    The solution wored, until I found out that , update only applies to lists, not to adding documents to the libraries!! grrr
    So I have gone done the rout of creating a cutom upload page, which is on the lists of tabs for the site. The user then chooses 
    which doucment library to upload the doucment to.

    When they click on the upload button, the upload is completed and a workflow is kicked off automatically. It would have been nice
    to have had the abilitly to do that from the item added event, and if I kind find a way to do that , that would be the preferred solution . 
    • Marked as answer by Chengyi Wu Monday, August 24, 2009 1:15 AM
    Friday, August 21, 2009 10:23 PM

All replies

  • -My understanding of the problem is the redirect will kill the thread and therefore the list item won't be added;
    it looks like the best solution is to manually add the list item as described in this post ("Redirecting User on ItemAdding in SharePoint EventHanlders") 
    -Of course an alternative is to create your custom new form that will be redirected when you click on the ok button.



    Serge Luca; blog: http://www.redwood.be
    Friday, August 21, 2009 10:13 AM
  • Hi, 

     Here its seems that you want to first add item and then you want to redirect to a page for adding custom variable. So i suggest you to write your code in ItemAdded Event Because ItemAddign event will fire before . and then ItemAdded Event. So in this case you adding item in ITemadding event and then redirecting. So that it might be creating conflicts. So  I suggest to keep your code in ItemAdded Event.

    Regards,
    Milan
    Friday, August 21, 2009 10:44 AM
  • @Milan - You can't do redirects in the ItemAdded event as the HTTP Context is not available.
    My SharePoint Blog - http://www.davehunter.co.uk/blog
    Friday, August 21, 2009 12:38 PM
  • Howdy,

    Please check this article http://www.techblooms.com/post/2008/09/SharePoint-Event-Receivers---Redirect-to-custom-page-or-URL-from-item-events.aspx

    I have posted working code for redirecting to custom url in Item Adding event.

    Good Luck.

    Gopinath Devadass - http://techblooms.com
    Friday, August 21, 2009 8:54 PM
  • -My understanding of the problem is the redirect will kill the thread and therefore the list item won't be added;
    it looks like the best solution is to manually add the list item as described in this post ("Redirecting User on ItemAdding in SharePoint EventHanlders") 
    -Of course an alternative is to create your custom new form that will be redirected when you click on the ok button.



    Serge Luca; blog: http://www.redwood.be

    The solution wored, until I found out that , update only applies to lists, not to adding documents to the libraries!! grrr
    So I have gone done the rout of creating a cutom upload page, which is on the lists of tabs for the site. The user then chooses 
    which doucment library to upload the doucment to.

    When they click on the upload button, the upload is completed and a workflow is kicked off automatically. It would have been nice
    to have had the abilitly to do that from the item added event, and if I kind find a way to do that , that would be the preferred solution . 
    • Marked as answer by Chengyi Wu Monday, August 24, 2009 1:15 AM
    Friday, August 21, 2009 10:23 PM