Read Web.Config settings while using Delay Activity in SharePoint Visual Studio Workflow

Be careful if you ever have to read a web.config setting from a SharePoint visual studio workflow when you are also using Delay activities.
When a workflow wakes up from a Delay activity, it’s no longer running in an ASP.Net Worker Process. Instead it’s running in the process for SharePoint Workflow Timer service. 
So, the method to read the setting would be different in that case. Your method to read the web.config app settings should be somethink like below:

private string GetWebConfigSetting(string settingKey)
{
    string webconfigSettingValue = String.Empty;

     if (ConfigurationManager.AppSettings[settingKey] != null) //reading app setting from web configuration file
      {
       webconfigSettingValue = ConfigurationManager.AppSettings[settingKey];
      }
           
  if(webconfigSettingValue == String.Empty)
   {
    //if no setting was found, it might be that the code is running in the context of a timer job.

    //in that case, following code will retrieve the setting
    using (SPSite site = new SPSite(this.workflowProperties.SiteId))
    {
     SPWebApplication webApplication = site.WebApplication;
    Configuration config =   System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/", webApplication.Name);
webconfigSettingValue = config.AppSettings.Settings[settingKey].Value;

    }

   }
  return webconfigSettingValue;
}
Note: Basic method of how to read web.config settings from a timer job comes from Praveen Battula’s blog (http://praveenbattula.blogspot.com.au/2009/12/access-webconfig-in-sharepoint-timer.html)

Comments

Popular posts from this blog

PowerApps Form for SharePoint and Access Denied Errors

Prevent site owners from creating subsites in SharePoint 2010

Getting Page Field Value in custom publishing page layout