A quick reminder: Say you have a SharePoint Survey List in a site that you want to populate using a console application. Further, say that said Survey List has an Event Receiver attached to it that needs to pick up settings from the web.config file for the host web app – let’s say a connection string.
You run your console application and notice that nothing is happening – well, something is happening, just not what you intended. A quick look in the Event Log reveals a bizarre error indicating that an object reference isn’t instantiated and it appears to be originating several lines of code before you make a call to the ConfigurationManager thus:
_connectionString = ConfigurationManager.ConnectionStrings[SQL_CONNECTION_STRING_NAME].ConnectionString;
The line that that is generating the error has nothing whatsoever to do with this line. It is a puzzling sort of puzzle.
You decide to check first principles and see if you can trigger the event receiver by hitting the list in the browser. Yup: Works fine. So what gives?
Then you remember: Your console app isn’t running in the same context as the browser app. Of course it can’t find the <connectionStrings> element – it doesn’t exist as far as it’s concerned. The called code in the Event Receiver is running blind!
<foreheadSlap> Simply add a {nameOfConsoleApp}.exe.config file local to the console .exe file and stash the configuration settings from the web.config that the called code needs to find. </foreheadSlap>
I knew this, you know. I was just not thinking clearly while debugging.
22d23f62-dae5-44bd-b8f8-f27b4eedc830|0|.0