Thursday 7 June 2012

Add Event Receivers to List Item in Sharepoint 2010


List Item Events--> Site Collection Administrators only Perform Add/Update/Delete other users can't.


Step 1) Create  Empty SharePoint 2010 project using .NET 3.5
                 Enter site URL  and select  "Deploy as Farm Solution"

Step 2)   Select  Project --> Right click on it--> Select "Add New Item"-->following dialog box will appear.
  in that select "Event Receiver"  and name it as "ListEvt1"
as shown below

Click on "Add"

Step 3)    It will show following screen
               select  1. List Item Events
                          2.Custom List
                          3.User cannot add items,update items & delete items.

as shown below


Click on FINSIH

Step 4)  The following class(ListEvt1  derived from SPItemReceiver) will be Generated,.  with ItemAdding, itemUpdating,itemDeleting


       public override void ItemAdding(SPItemEventProperties properties)
       {
           web = properties.OpenWeb();
           if (properties.List.Title == "purchaseOrder")
           {
               if (!web.UserIsSiteAdmin)
                   properties.Cancel = true;
           }
       }
       public override void ItemUpdating(SPItemEventProperties properties)
       {
           web = properties.OpenWeb();
           if (properties.List.Title == "purchaseOrder")
           {
               if (!web.UserIsSiteAdmin)
                   properties.Cancel = true;
           }
           
       }
       public override void ItemDeleting(SPItemEventProperties properties)
       {
           web = properties.OpenWeb();
           if (properties.List.Title == "purchaseOrder")
           {
               if (!web.UserIsSiteAdmin)
                   properties.Cancel = true;
           }

       }


Step 5)  Deploy the Solution.

Note: If User is  Site Collection Administrator,  Then only user can add/update/delete records, for other users the following error message will be displayed,

No comments:

Post a Comment