- SharePoint 2013 WCM Advanced Cookbook
- John Chapman
- 787字
- 2021-07-19 18:36:42
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:
- Navigate to the site in your preferred web browser.
- Select Site settings from the Settings menu.
- Select Device Channels from the Look and Feel section.
You can also navigate to the Device Channels page from the Design Manager page.
- Select New Item.
- Provide a Name, Description, and Alias for the device channel.
- Specify the Device Inclusion Rules to be included in the device channel.
Android
iPad
iPod
iPhone
BlackBerry
IEMobile
WebOS
- 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.
- 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.
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.
There's more...
A device channel may also be created with PowerShell or with code using the server-side object model.
Follow these steps to create a device channel for mobile devices using PowerShell:
- Get the site using the
Get-SPWeb
Cmdlet.$web = Get-SPWeb http://sharepoint/site
- Get the
DeviceChannels
list.$list = $web.Lists["Device Channels"]
- Add a new
SPListItem
item to theItems
collection of the list.$item = $list.Items.Add()
- 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
- Call the
Update
method on the list to update theItems
collection.$item.Update()
- Use the
Dispose
method to discard theSPWeb
object.$web.Dispose()
Follow these steps to create a device channel for mobile devices with code using the server-side object model:
- Open the site collection containing the site in a
using
statement.using (var site = new SPSite("http://sharepoint/site"))
- Open the site in a
using
statement.using (var web = site.OpenWeb())
- Get the
DeviceChannels
list.var list = web.Lists["Device Channels"];
- Add a new
SPListItem
item to theItems
collection of the list.var item = list.Items.Add();
- 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;
- Call the
Update
method on the list to update theItems
collection.item.Update();
See also
- The SharePoint 2013 Design Manager device channels article on MSDN at http://msdn.microsoft.com/en-us/library/jj862343.aspx
- The How to: Add or Delete List Items topic on MSDN at http://msdn.microsoft.com/en-us/library/ms467435(v=office.14).aspx
- The SPWeb class topic on MSDN at http://msdn.microsoft.com/en-us/library/Microsoft.SharePoint.SPWeb.aspx
- The SPSite class topic on MSDN at http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsite.aspx
- The Get-SPWeb article on TechNet at http://technet.microsoft.com/en-us/library/ff607807.aspx