Monday, 10 October 2016

Unable to read Web.Config file in SharePoint 2013 Event receiver

 Introduction:
Generally, we use web.config file’s app settings section to keep configurable and application wide settings that an application requires in order to perform the task properly. This helps in easy maintenance and deployment of the application, because we can easily update the settings and verify without build and deployment. 


Now we read the key value pairs from web.config file as follow:
ConfigurationManager.AppSettings[key]


I need to use these app settings in Item added Event receiver in SharePoint list. Though, the above code worked fine on local environment, but on production server, it gave exception. 
"Object reference not set to an instance of an object"

In the Event receiver, the above code is unable to find and read the web.config app settings, you would've noticed that although code compiles and runs, this comes back null. Well, the very generic answer to why this is, is because the event handler isn't really running within the same context as a Web site that has direct access to the ConfigurationManager.


Approach/solution:
There is a simple solution for this issue, just change the approach of reading the web.config file. 

Below is the method we have implemented to read the web.config file:

public static string GetAppSettingKeyValue(SPSite siteCollection, string key)
        {
            System.Configuration.Configuration config;
            if (string.IsNullOrEmpty(siteCollection.WebApplication.Name))
                throw new ApplicationException("Web application name is empty!");
            config = WebConfigurationManager.OpenWebConfiguration("/", siteCollection.WebApplication.Name);
            AppSettingsSection appSettings = config.AppSettings;
            if (appSettings == null)
                throw new ApplicationException("Web.config appSettings section cannot be found!");
            if (appSettings.Settings[key] == null || string.IsNullOrEmpty(appSettings.Settings[key].Value))
                throw new ApplicationException("Key value cannot be read from settings of appSettings. Make sure this key and its value exist!");
            return appSettings.Settings[key].Value;
        }


And then we just have to call the above method from event receiver’s events with necessary parameters to read the settings as below:
GetAppSettingKeyValue(new SPSite(properties.SiteId), key);

Here, first parameter is SPSite object and the second parameter is a app settings key, for which we need to read value from web.config file.

Thus all we need to do is, just change the approach of reading web.config file and we are done! Now, we can successfully read the app settings configuration.

PowerShell script to get updated content in whole site between two dates.


Introduction:

As a part of migration of SharePoint site from 2010 to 2013 environment, I need to perform a content migration from 2010 to 2013. As a migration process, content database of 2010 production site was migrated to 2013 environment. Yet the 2010 production site was in use and 2013 site is still doing setups and updates for live production. In this mean time, 2010 site has many content updates going on obviously as site is being used by a giant company. So how to get the contents of site which is updated after content DB backup is taken from 2010?

Solution:

I have prepared two scripts.
  1. First script will read for the Site URL, Start Date and End Date from user and give whole inverntory count of lists/libraries/sites which are updated between two dates. And it will give the csv file with no of items for each object which are update between 2 dates.
  2. This script will read for the Site URL, Start Date and End Date from user and give all items which are updated between two dates from whole site collection. This will also give the csv file with Site URL, List name and, Item URL for each item which are update between 2 dates. 

Thus, I was able to get all the items which are updated after content DB is migrated to 2013 site and updated those items manually.

In addition, we can use this script multiple times till the 2013 site actually goes live and can have up to date content on new live site!!

Thanks for reading this blog !