1. Setting up our project
Launch Visual Studio 2005 and create a new Class Library project.
File > New Project > Class Library >> DeletingEventHandler in the Name box, and then click OK.
>> In Solution Explorer, select DeletingEventHandler, and click Add Reference on the Project menu.
>> In the Add Reference dialog box, select Microsoft.SharePoint on the .NET tab and then click OK.
2. Writing our class with our custom code
>> In the Code Editor, import the Microsoft.SharePoint namespace as follows.
using Microsoft.SharePoint;
>> Change the name of the class to DeletingAction and make it inherit from the SPItemEventReceiver class, as follows.
>>public class DeletingAction : SPItemEventReceiver
>> Add the following code within the class to override the ItemDeleting method.
public override void ItemDeleting(SPItemEventProperties properties)
{
properties.Cancel = true;
properties.ErrorMessage = "Deleting items from " + properties.RelativeWebUrl + " is not supported.";
}
3. Signing and Installing our assembly into the GAC
>> In Solution Explorer, right-click the DeletingEventHandler node, and then click Properties.
>> In the Properties dialog box, click the Signing tab, select Sign the asembly, select Choose a strong name key file, and then click
>> In the Create Strong Name Key dialog box, type DeletingEventHandler.snk in the Key file name box, optionally specify a password for the key, and then click OK.
>> To build the project, click Build Solution on the Build menu, or press CTRL+SHIFT+B.
>> To Generate a DLL, use WSP Builder >> Copy to GAC option. When its done you can see view the addsebly in C:\Windows\Assembly and dll will be DeletingEventHandler.dll
4. Define our resource files
SharePoint Features require not just a compiled dll, but also a set of corresponding files. These files are stored in the "12" HIVE on the SharePoint server. The file structure already exists and we will simply be adding files that are used by our feature to it.
Create a folder structure as shown below in your Solutions Explorer:
12 >> TEMPLATE >> FEATURES >> DeletingEventHandler and create 2 new xml files
elements.xml and feature.xml
Open the feature.xml file and replace any code with the following:
>> Feature.XML
We will need to fill in a valid GUID for the Id (left intentionally blank).
In VS2005, click on Tools > Create GUID
Click on Copy to copy the string and place it in the Id attribute value in your feature.xml file, leaving out the curly brackets!
>> Elements.xml
5. Installing the SharePoint Feature
>> Use WSP Bulider to copy 12 Hive files with "Copy to 12 hive option"
>> In your Solutions Explorer, double-click on Project and click on the Build Events SideTab. In your event text box, you should have one single line of code, which we wrote in our previous steps. (Post build Events)
--------------------------------
cd "$(ProjectDir)"
xcopy "12" "%CommonProgramFiles%\Microsoft Shared\web server extensions\12\" /ys
cd "%CommonProgramFiles%\Microsoft Shared\web server extensions\12\BIN"
stsadm -o installfeature -name DeletingEventHandler -force
iisreset
--------------------------------
Before you build, ensure that SharePoint is working and navigate to the Site Features page. (Home> Site Settings > Site Features)
And click "Acivate".
6. Testing our feature
http://vm1f2909/sites/hol/Lists/Announcements/AllItems.aspx and right click "Delete option". It wont work for ob reasons and showing this:
-----
Error
Deleting items from /sites/hol is not supported.
Troubleshoot issues with Windows SharePoint Services.
------
7. For updating the code:
>> VS.NET 2008 >> Sol Explorer >> Properties >> Build events >> Post built and UNINSTALL feature:
cd "$(ProjectDir)"
xcopy "12" "%CommonProgramFiles%\Microsoft Shared\web server extensions\12\" /ys
cd "%CommonProgramFiles%\Microsoft Shared\web server extensions\12\BIN"
stsadm -o uninstallfeature -name DeletingEventHandler -force
iisreset
>> Under C:\Windows\Assembly, uninstall DeleteEventHandler.dll
>> VS.NET 2008 >> Sol Explorer >> Properties >> Build events >> Post built and remove this code:
cd "$(ProjectDir)"
xcopy "12" "%CommonProgramFiles%\Microsoft Shared\web server extensions\12\" /ys
cd "%CommonProgramFiles%\Microsoft Shared\web server extensions\12\BIN"
stsadm -o installfeature -name DeletingEventHandler -force
iisreset
>> update tthe code.
>> Build (for new dll)
>> WSPBuilder >> Copy to GAC
>> Sol Explorer >> Properties >> Build events >> Post built and remove this code:
Restore the code:
cd "$(ProjectDir)"
xcopy "12" "%CommonProgramFiles%\Microsoft Shared\web server extensions\12\" /ys
cd "%CommonProgramFiles%\Microsoft Shared\web server extensions\12\BIN"
stsadm -o installfeature -name DeletingEventHandler -force
iisreset
>> Feature updated.
------------------------------------------------------------
HttpContext is available only in synchronous event like ItemAdding and ItemUpdating.
en -Ing events (asynchronous and synchronous events)
Event names ending on –ed like ItemUpdated, ItemDeleted and so on are called asynchronous events as they will execute after the action has been completed. At this stage you can no longer prevent something from being save or deleted.
The events ending in “ing” like ItemUpdating, ItemInserting and so on fire before the action is completed and are therefore called synchronous. In this case you can still intervene before the action is final.
The full list of available methods that can be overridden for SPItemEventReceiver can be found in the following MSDN article: http://msdn2.microsoft.com/en-us/library/ms437502.aspx
DisableEventFiring()
If you need to do an update to an item from within your event receiver you might end up in a recurring event being fired for the updates. To prevent this you can call DisableEventFiring() and then EnableEventFiring(). A small example for this:
public override void ItemUpdated(SPItemEventProperties properties)
{
//some handling here
ListItem item = properties.ListItem;
item["someproperty"] = "some value";
this.DisableEventFiring();
//save changes
addedItem.Update();
this.EnableEventFiring();
}
When calling the Update method you might also consider using SystemUpdate() method which will do a SharePoint database updating without updating the time modified or versions of the item.
http://www.katriendg.com/aboutdotnet/2007-6-event-handler-cancel-update.aspx
----------------------
http://www.katriendg.com/aboutdotnet/2007-6-event-handler-cancel-update.aspx
Redirection from event handler
Scenario:
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.
Default behavior.
• 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)
Roadblocks
• 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.
Solution
• 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.
DisableEventFiring();
SPListItem itemToAdd = objlist.Items.Add();
//add item to list
itemToAdd.Update();
EnableEventFiring();
// provision sub site and perform required action
//redirect it to your new destination like newly provisioned sub site or any other page you want.
SPUtility.Redirect(strNewresiractionUrl, SPRedirectFlags.Trusted,current);
}
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.
No comments:
Post a Comment