0

Forcing a Device Channel

Device channels are a way of using different master pages for different devices. MSDN describes the functionality as followed:

 

“Browsing the web on a mobile device is now so common that a SharePoint site must be optimized for readability and ease of use on smartphones and other mobile devices such as tablets. With device channels in SharePoint 2013, you can render a single publishing site in multiple ways by using different designs that target different devices. This article can help you plan for using the device channels feature in SharePoint 2013. It provides a detailed overview of the feature itself, and provides the necessary information for creating a device channel. Also, after reading this article, you’ll know what device channels you need to implement, and how to implement those channels.”

 

Using the out of the box settings you can set the master page by information that is in the user agent string. The information in this string will give SharePoint the information to select the right master page. How the out of the box functionality works can be found here:

 

https://msdn.microsoft.com/en-us/library/office/jj862343.aspx

 

This functionality can also be used for other scenarios for example in a CMS setup. When you want to apply this to other scenarios you will have to force a device channel. Forcing device channels can be done using two options:

 

  • Query String: Append the “DeviceChannel” query string to your URL and set the value to the “Alias” of your device channel.
  • Cookie: You can set a cookie named “DeviceChannel”  and set the value of the cookie to the name of your “Alias”.

 

You can set this cookie in code in order to persist the use of the master page.

 

if (HttpContext.Current.Request.Cookies["DeviceChannel"] == null) {

    string url = HttpContext.Current.Request.Url.ToString();
    using (SPSite site = new SPSite(url)) {
        string authUrl = site.GetProperty(PropertyBagKeys.AuthoringSiteKey, string.Empty);
        string devChannel = site.GetProperty(PropertyBagKeys.AuthDeviceChannel, string.Empty);

        if (!string.IsNullOrEmpty(authUrl)) {
            if (url.StartsWith(authUrl)) {
                HttpCookie cookie = new HttpCookie("DeviceChannel");
                cookie.Value = string.IsNullOrEmpty(devChannel) ? "Default" : devChannel;
                HttpContext.Current.Request.Cookies.Add(cookie);
            } else {
                HttpContext.Current.Request.Cookies.Remove("DeviceChannel");
            }
        }
    }               
}

Related Posts

Starting a Workflow with CSOM There are situations were you would like to start a workflow by using  code. In one of my last projects there was that kind of situation, in that proj...
Offline Installation SharePoint 2013 In many situation you would like to do a offline installation of SharePoint 2013 or you are required to do a offline installation because you do not h...
User automatically removed from SharePoint Group During my current project we received an access request from a user. We did what we normally do, we checked the request and added the user in the appr...
Unable to start User Profile Synchronization Service A few days ago I wrote a post about loading modules in PowerShell by default: “Load modules by default when opening PowerShell”. Today we had to setup...
Filtering SharePoint Search Results by Content Source Working for one my projects we were looking for a solution to only retrieve search results for a specific Content Source. In our scenario we had a Ext...
PowerShell Scripts During my current project we are using a lot of PowerShell scripts to administrate the environment and make development easier for us. In this post...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.