Showing posts with label Web.Config. Show all posts
Showing posts with label Web.Config. Show all posts

September 20, 2017

SharePoint Anonymous access issues with Mobile devices for Public-facing website

Problem Statement:
We implemented a public-facing website in SharePoint 2013. We have had anonymous access enabled for the web application. The anonymous access was working fine in the Desktop View, but we faced an issue with accessibility of the site in Mobile View. SharePoint site was prompting for authentication while using in Mobile view. It was working fine while using on desktop view.

Cause of the Error:
If you notice the browser's console window, you will find some JavaScript files are not accessible as an anonymous user, it throws 401 error. SharePoint automatically detects that a user is connected with a mobile browser, and redirects them to a mobile view of the page. However, by default anonymous users don’t have access to this page. In this situation, the likely goal is to not redirect the users to the mobile page and display the desktop responsive version page.

Resolution:
To fix this issue follow the certain steps (If it is multi-tiered SharePoint farm, these steps need to be followed on all the servers in the farm):
  1. Open the IIS Manager.
  2. Find the SharePoint Web Application where you found these issue.
  3. Explore the Web Application in File Explorer.
  4. Edit web.config file in a text editor.
  5. In <configuration> find <system .web>.
  6. Add or modify below code section.
  7. <browserCaps> <result type=”System.Web.Mobile.MobileCapabilities, System.Web.Mobile, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”/> <filter>isMobileDevice=false</filter> </browserCaps> 
  8. Save and browse the site, the issue should have been addressed.

Reference:
https://social.technet.microsoft.com/Forums/office/en-US/d1e58832-391b-42ba-a21b-6ef40d1c9acb/anonymous-access-for-mobile-users?forum=sharepointadminprevious

If you have any questions you can reach out our SharePoint Consulting team here.

June 2, 2016

SharePoint 2013 - Unable to read Web.Config file in 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.

If you have any questions you can reach out our SharePoint Consulting team here.