views:

51

answers:

1

In the following code,

// class overrides SPItemEventreceiver    
public override void ItemAdding(SPItemEventProperties properties)
{    
  using (var site = new SPSite(properties.SiteId)) //SiteId is GUID <<corrected
  {
      ...
  }
}

The following exception is thrown:

System.IO.FileNotFoundException: The Web application at http://URL could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.

One way to work around this is to hard-code (or configure) the URL specified in Alternate Access Mappings. Putting the correct URL in Alternate Access Mappings is ultimately the correct solution, but if possible, I need a work-around that doesn't require configuration.

+1  A: 

SiteId should not be an integer - SPSite ctor only accepts URLs or Guids. Given that it is an GUID, I don't see how AAM plays a part here. An alternate approach might be to use:

properties.OpenWeb().Site

Also, since you are in a synchronous event handler you should have access to SPContext.Current.Site (unless you're trapping events in a document library - a long standing sharepoint bug means there is no context in sync events for doclibs - shitty)

-Oisin

x0n
My bad... corrected the int thing.
Greg Ogle
The ultimate answer is the right answer. The other methods of opening the site or web from SPItemEventProperties simply don't work.. i.e. properties.OpenWeb() and new SPSite(properties.SiteId). I did not try accessing SPContext, perhaps that would have worked, but the Alternate Access Methods were corrected and the standard ways of opening the site are working. thanks, for your input.
Greg Ogle