locked
SPSite siteUrl = SPContext.Current.Site; is throwing error"The name 'siteUrl' does not exist in the current context" in event receiver? RRS feed

  • Question

  • Hi All,

    I have created an event receiver ItemUpdating and trying to use

    SPSite siteUrl = SPContext.Current.Site; under ItemUpdating method but always its throwing error

    "The name 'siteUrl' does not exist in the current context". But if I am passing static url then its working fine.

    public override void ItemUpdating(SPItemEventProperties properties)

    {

    SPSite siteUrl = SPContext.Current.Site;     //throwing error

    SPWeb oWeb = siteUrl.OpenWeb();  //throwing error

    string siteUrl = "http://james:8787";   //working fine

    SPWeb oWeb = siteUrl.OpenWeb(); //working fine

    try
     {

    SPGroup engineerGroup = oWeb.SiteGroups["TestEngineers"];

    }

    }

    Thanks,

    Vishwa

    Friday, September 21, 2012 9:13 AM

Answers

  • Instead of getting the SPSite object from SPContext object. Use the properties to get these objects

    For SPSite - properties.ListItem.Web.Site

    For SPWeb - properties.ListItem.Web


    Ram Prasad Meenavalli | MCTS SharePoint 2010 | MCPD SharePoint 2010

    Friday, September 21, 2012 9:27 AM

All replies

  • Instead of getting the SPSite object from SPContext object. Use the properties to get these objects

    For SPSite - properties.ListItem.Web.Site

    For SPWeb - properties.ListItem.Web


    Ram Prasad Meenavalli | MCTS SharePoint 2010 | MCPD SharePoint 2010

    Friday, September 21, 2012 9:27 AM
  • Hi,

    You can try below code in your receiver:

    If your feature scope is Site then use
    SPWeb objSPWeb = (properties.Feature.Parent as SPSite).OpenWeb()
    or 
    SPWeb objSPWeb = (properties.Feature.Parent as SPSite).RootWeb
    
    If your scope is web then use
    
    SPWeb objSPWeb = properties.Feature.Parent as SPWeb

    Hope it could help


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"

    Friday, September 21, 2012 10:41 AM
    Moderator