locked
EventReceivers RRS feed

  • Question

  • Hi

    I want to setup Events even when site is loaded by Anonymous user

    When site opens it popups authentication dialog if used by Anonymous

     

    Using this code will not help ....

     

    SPSecurity.RunWithElevatedPrivileges(delegate()

    {

    SPList LeftBarMenuList = SPContext.Current.Web.Lists["LBL"];

    LeftBarMenuList.EventReceivers.Add(SPEventReceiverType.ItemAdded, AssemblyName, EventClassName);

    });

     

     

     

    suggest how to avoid Authentication Dialog

    and setup Event with Anonymous


    Please mark this as answer if it helps.
    Microsoft Certified Trainer
    Microsoft Certified Professional Developer
    BizTalk blog: http://www.EnterpriseApplicationsDevelopment.com/
    Wednesday, December 22, 2010 2:47 PM

Answers

  • I would avoid adding an event reciever in this way.  You only need to add the eventreceive one time for the list anyways. 

    Why not use a delegate control, that runs with elevated priv's to record the things you want to capture?

    BTW, you should not use SPContext inside of the SPSecurity.RunWithElevatedPrivileges.

    string SiteUrl = SPContext.Current.Site.Url;

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {

     using (SPSite site = new SPSite(SiteUrl)
     {
      using (SPWeb web = site.OpenWeb())
      {
        // record your info, but do not attach the event receiver to the list here... do that in a feature, etc.
        SPList leftBarList = web.Lists["YourListTitle"];
      ...
      ...
      }
     }
    });


    -Dan Keeling Blog (http://dankeeling.blogspot.com/) or Follow me on Twitter @DanKeeling
    Thursday, December 23, 2010 5:26 AM