Context is Null in ItemAdded Receiver

Aug 15, 2010 at 1:06 AM

Hi all

I use Sharepoint 2007 and I just try to use the Configuration Manager in a ItemAdded event that is attached to a list. I run this code:

 IServiceLocator serviceLocator = SharePointServiceLocator.Current;
        IHierarchicalConfig _config = serviceLocator.GetInstance<IHierarchicalConfig>();         
        string test =_config.GetByKey<string>("MyKey", ConfigLevel.CurrentSPWebApplication);

And on the last line I receive an error that the SPContext is null. The error come from the constructor of SPWebAppPropertyBag class:

public SPWebAppPropertyBag()
{
 this.webApplication = SPContext.Current.Site.WebApplication;
}

'Object reference not set to an instance of an object.'

In fact, the SPContext.Current is null... but in the ItemAdded function the context is not null... how to pass it to the constructor ?

Thanks a lot....

Oct 25, 2011 at 8:38 AM

I think you can use the parameter of SPItemEventProperties from ItemAdded handler:

 

SPListItem curItem = properties.ListItem;

SPWeb web = curItem.Web;

 

HTH

Oct 25, 2011 at 12:32 PM

THere isn't a way to do this with the 2007 release.  We enhanced the release for 2010 to support setting the web on the configuration manager for situations like this where context is null, but we did not update that feature in the 2007 release.