recently we have a requirement, for that we need to change default behavior of SharePoint.
Here is the requirement.
• While adding Item in the list, create sub site regarding that item.
• Redirect to the newly created site.
• Same case while updating item also.
• After adding or updating item your SharePoint will redirect you to the page from where you come.
• Like in list if you adding item by clicking “New Item” from “alltems.aspx” SharePoint will redirect you back to the same page “allitems.aspx”.
How to do it?• Create an event receiver like ItemAdding and ItemUpdating for that List or library.
• In that event receiver, provision site programmatically.
• After provisioning new site, redirect user to newly created sub site using SPUtility.Redirect method. (need to include Microsoft.SharePoint.Utilities)
• My first road block is that HttpContext is not available in event handler.
• To resolve this problem Check this
• Now after finding a way to get HttpContext we can use SPUtility.Redirect method but if we use it item is not added and your thread will redirect.
• And you’re other events (Asynchronous) like ItemAdded and ItemUpdated will not fire.
• To resolve this problem please check Code snippet below
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.
SPListItem itemToAdd = objlist.Items.Add();
//add item to list
// provision sub site and perform required action
//redirect it to your new destination like newly provisioned sub site or any other page you want.
I hope you got my problem and solution. With the help of this you can find your solution according to your requirement.
And thank you very much to Eric Bartels for his superb post which was helpful to resolve this problem.