Creating a device channel for mobile devices

One of the most common scenarios for using device channels is to identify the tablet and smartphone browsers. Applying a mobile-specific master page, when appropriate, can provide the users with a design that is more touch friendly and is laid out in a specific manner for smaller screens. In this recipe, we are going to create a device channel that will identify Android, iOS, BlackBerry, WebOS, and Windows mobile devices. There are hundreds of mobile-specific browsers that we can detect with the user agent. However, for this recipe we are going to keep it simple.

Getting ready

In order to view and modify the device channels for a SharePoint site, the SharePoint Server Publishing Infrastructure site collection feature and SharePoint Server Publishing site feature must be activated.

How to do it...

Follow these steps to create a device channel for mobile devices:

  1. Navigate to the site in your preferred web browser.
  2. Select Site settings from the Settings menu.
  3. Select Device Channels from the Look and Feel section.
    How to do it...

    You can also navigate to the Device Channels page from the Design Manager page.

  4. Select New Item.
  5. Provide a Name, Description, and Alias for the device channel.

    Tip

    The Alias field specified will be used when specifying which master page to use with the device channel in the device channel mappings file. We will learn about this in the next recipe, Applying a master page to a device channel.

  6. Specify the Device Inclusion Rules to be included in the device channel.

    Android

    iPad

    iPod

    iPhone

    BlackBerry

    IEMobile

    WebOS

  7. When using multiple device inclusion rules, place each string on a new line to match the user agent. Device Inclusion Rules are simply strings that are looked for in the user agent of incoming web requests.
    How to do it...
  8. Mark the Active checkbox and click on Save.

How it works...

Device channels are created and stored in the /DeviceChannels SharePoint list in the root site of a site collection. When an incoming browser request is received, SharePoint checks whether the incoming user agent matches any of the Device Inclusion Rules before selecting the master page to use.

How it works...

Many web browsers have developer tools that allow changing the user agent reported by the browser. Switching the user agent is one way in which we can test to ensure our device channels are working correctly. Internet Explorer 11, for instance, includes this option in the Emulation section of the F12 Developer Tools.

How it works...

There's more...

A device channel may also be created with PowerShell or with code using the server-side object model.

Creating a device channel for mobile devices using PowerShell

Follow these steps to create a device channel for mobile devices using PowerShell:

  1. Get the site using the Get-SPWeb Cmdlet.
    $web = Get-SPWeb http://sharepoint/site
    
  2. Get the DeviceChannels list.
    $list = $web.Lists["Device Channels"]
    
  3. Add a new SPListItem item to the Items collection of the list.
    $item = $list.Items.Add()
    
  4. Assign the values to each of the properties on the SPListItem item.
    $item["Name"] = "PowerShell"
    
    $item["Alias"] = "PowerShell"
    
    $item["Description"] = "PowerShell Channel"
    
    $item["Device Inclusion Rules"] = "Android`niPad`niPod`niPhone`nBlackBerry`nIEMobile`nWebOS"
    
    $item["Active"] = $true
    

    Tip

    When a line break is required within a string, in PowerShell, an escape character can be used. Escape characters in PowerShell use the tilde character. For example, a new line is represented by `n.

  5. Call the Update method on the list to update the Items collection.
    $item.Update()
    
  6. Use the Dispose method to discard the SPWeb object.
    $web.Dispose()
    

Creating a device channel for mobile devices with code using the server-side object model

Follow these steps to create a device channel for mobile devices with code using the server-side object model:

  1. Open the site collection containing the site in a using statement.
    using (var site = new SPSite("http://sharepoint/site"))
  2. Open the site in a using statement.
    using (var web = site.OpenWeb())
  3. Get the DeviceChannels list.
    var list = web.Lists["Device Channels"];
  4. Add a new SPListItem item to the Items collection of the list.
    var item = list.Items.Add();
  5. Assign the values to each of the properties on the SPListItem item.
    item["Name"] = "Code";
    
    item["Alias"] = "Code ";
    
    item["Description"] = "Code Channel";
    
    item["Device Inclusion Rules"] = "Android\niPad\niPod\niPhone\nBlackBerry\nIEMobile\nWebOS";
    
    item["Active"] = true;

    Tip

    When a line break is required within a string in C#, an escape character can be used. Escape characters in C# use the backslash character. For example, a new line is represented by \n.

  6. Call the Update method on the list to update the Items collection.
    item.Update();

See also