C# – SPItemEventReceiver Context SiteId/WebUrl Incorrect

cevent-receiveriissharepointwss-3.0

I have created a SharePoint Event Receiver, that fires on Item Update.
The receiver needs access AfterProperties and ListItem.

When firing the event receiver from a SharePoint web application using an IP address (http://10.0.4.50/sites/), it throws a FileNotFoundException, when accessing SiteId And WebUrl in SPItemEventProperties.
The Web application at http://10.0.4.50/sites/companyName could not be found. Verify that you have typed the URL correctly.

When firing the event receiver using the hostname, specified at setup of Site Collection http://computerhostname/sites/companyName. This works fine, no exception is thrown.

//Combine Both AfterProperties And ListItem
var ListProperties = new List<KeyValuePair<string, object>>();
ListProperties.AddRange(properties.AfterProperties.ToKeyValue());
ListProperties.AddRange(properties.ListItem.ToKeyValue(p => !p.Sealed));
var AvaliableProperties = ListProperties.Distinct(new KeyValueComparer<object>());

Best Answer

This isn't necessarily a SharePoint problem, but is most likely down to the way that IIS is configured on the host. A particular site can be "bound" to an IP address and a host name, and if the host name isn't present in the HTTP GET sent by the browser then IIS will return a 404 NOT FOUND.

You can verify this by using a browser to see whether you can access the site by IP address.

You can add additional bindings if necessary, but there can only be one "default" binding per IP address, so only one site can be present at (in your case) 10.0.4.50.

A possible way around the situation where multiple sites are present but you can't use a hostname might be to add a binding to a port other than port 80 for this IP, so your URL would become something like http://10.0.4.50:8080/sites/companyName.

Related Topic