only My site

Tuesday, May 29, 2012

Event Receiver : Access Site content using Privileged user account and user token

The below code is from Event receiver. The important parts are



  • 1. Properties.ListTitle
  • 2. Properties.AfterProperties("Title").ToString()
  • 3. objSPUser.UserAccount
  • 4. New SPSite(properties.Web.Url, privilegedToken)
  • 5. Properties.Status = SPEventReceiverStatus.CancelWithError
  • 6. Properties.ErrorMessage
  • 7. Properties.Cancel
  • Note : SPEventReceiverStatus is an ENUMerator with the following options "Continue, CancelNoError, CancelWithError, CancelWithRedirectUrl"

    Sample for CancelWithRedirectUrl



  • string errorMessage = string.Format ("ZIP Code '{0}' is Invalid!", zipCode);
  • properties.Cancel = true;
  • properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
  • properties.RedirectUrl = "/_layouts/EventReceiverProject1/CustomErrorPage.aspx?Error=" + errorMessage;



  • Sharepoint 2010 provides support for many new events, in both synchronous and asynchronous. The new events are


    1. onCreate events for lists and Web sites

    2. add and delete events on list

    3. add events on websites

    4. after events can now be synchronous or asynchronous
     
    To trigger a Event only for a instance of a list, we have to replace the ListTemplateId to ListUrl in the Elements.XML.
    - This refers the Announcement List definition.


    - This refers the instance of the Announce list instance. TestAnnouncement is the list created using Announcement list definition

    Impersonation for Event receivers Sharepoint 2010


    SharePoint runs your events in the context of the user who triggered the event. Generally, this is okay, but there may be certain times when you want to let a user perform actions on lists, libraries, or the system that the current user does not have permissions to do. In most cases, you would use SPSecurity’s RunwithElevatedPrivileges method. However, you may want to revert to the originating user on some operations. With SharePoint 2010 you get a property bag that contains the OriginatingUserToken,UserDisplayName, and UserLoginName which you can use to revert to the original user.

    Types  of Event receivers

    1. Site Events
    2. Web Events
    3. List Events
    4. List Field Events
    5. Item Events

    No comments: