Listing the device channel master pages
Identifying the master pages used by each device channel for each site in a SharePoint farm can be cumbersome. Using PowerShell, the administrators are able to quickly iterate through each site to accomplish this. In this recipe, we are going to use a PowerShell script (PS1) to output the device channels and master pages configured for each site in a site collection.
How to do it...
Follow these steps to list the device channel master page configurations for each site in a site collection with PowerShell:
- Open your preferred text editor to create the PS1 script file.
- Load the
Microsoft.SharePoint.dll
andMicrosoft.SharePoint.Publishing.dll
assemblies into the PowerShell session.[Reflection.Assembly]::LoadFrom("C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Publishing.dll")
[Reflection.Assembly]::LoadFrom("C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.dll")
- Get the site collection using the
Get-SPSite
Cmdlet.$site = Get-SPSite http://sharepoint/sitecollection
- Get the object types for the parameters that will be used when getting the class constructor for the
MasterPageMappingsFile
object and later instantiating the object.$typeWeb = [Microsoft.SharePoint.SPWeb] $typeBool = [System.Boolean] $typeMappingFile = [System.Type]::GetType("Microsoft.SharePoint.Publishing.Mobile.MasterPageMappingsFile, Microsoft.SharePoint.Publishing, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
- Create an array of the object types.
$consMappingFileParams = ($typeWeb, $typeBool, $typeWeb)
- Get the class constructor for the
MasterPageMappingsFile
object.$consMappingFile = $typeMappingFile.GetConstructor($consMappingFileParams)
- Create an array of the default parameters required to instantiate the
MasterPageMappingsFile
object.$mappingFileParams = [System.Array]::CreateInstance([System.Object], 3) $mappingFileParams[1] = $false $mappingFileParams[2] = $null
- Use a
foreach
loop to iterate through eachSPWeb
in theAllWebs
property of theSPSite
object.foreach ($web in $site.AllWebs)
- Add the
SPWeb
object to the parameters array and invoke the constructor to create an instance of theMasterPageMappingsFile
object.$mappingFileParams[0] = [Microsoft.SharePoint.SPWeb] $web
$mappingFile = $consMappingFile.Invoke($mappingFileParams)
- Output the master page settings for the default channel.
Write-Host ""
Write-Host "Site: " $web.Url
Write-Host "Device Channel: Default"
Write-Host "Master Page: " $web.CustomMasterUrl
- Use a
foreach
loop for each device channel key in theKeys
collection of the mapping file.foreach ($key in $mappingFile.Keys)
- Output the master page settings for the device channel.
Write-Host "" Write-Host "Site: " $web.Url Write-Host "Device Channel: " $key Write-Host "Master Page: " $mappingFile[$key].MasterPageUrl
- Use the
Dispose
method to discard theSPWeb
object.$web.Dispose()
- Use the
Dispose
method to discard theSPSite
object.$site.Dispose()
- Save the file as a
PS1
file, for example,getdevicechannels.ps1
. - Execute the script in the PowerShell session.
./getdevicechannels.ps1
How it works...
Using .NET reflection we are able to interact with the private methods and classes in the SharePoint assemblies that provide the mapping information of the device channel. In this recipe, we used .NET reflection to instantiate the MasterPageMappingsFile
object for each site in the AllWebs
property of the site collection we obtained with the Get-SPSite
Cmdlet. From the MasterPageMappingsFile
object, we were able to output the master page configured for each device channel. In addition, we output the default master page configured for each site.
There's more...
This recipe may also be accomplished with code using the server-side object model.
Follow these steps to list the device channel master page configurations for each site in a site collection using the server-side object model:
- Get the site collection with a
using
statement.using (var site = new SPSite("http://sharepoint/sitecollection"))
- Get the object type that will be used when getting the class constructor for the
MasterPageMappingsFile
object and later instantiating the object.var typeMappingFile = Type.GetType("Microsoft.SharePoint.Publishing.Mobile.MasterPageMappingsFile, Microsoft.SharePoint.Publishing, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c");
- Get the class constructor for the
MasterPageMappingsFile
object.var consMappingFile = typeMappingFile.GetConstructor(new Type[] {typeof(SPWeb), typeof(bool), typeof(SPWeb)});
- Use a
foreach
loop to iterate through each site in theAllWebs
property of the site collection.foreach (var web in site.AllWebs)
- Ensure that the site exists.
if (web.Exists)
- Invoke the constructor to create an instance of the
MasterPageMappingsFile
object.var mappingFile = consMappingFile.Invoke(new object[] { web, false, null });
- Output the master page settings for the default channel.
Console.WriteLine(""); Console.WriteLine("Site: " + web.Url); Console.WriteLine("Device Channel: Default"); Console.WriteLine("Master Page: " + web.CustomMasterUrl);
- Get the
mappings
field from the mapping file and cast the object as anIDictionary
.var mappings = (IDictionary)typeMappingFile.GetField("mappings", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(mappingFile);
- Use a
foreach
loop for each device channel key in theKeys
collection of the mappings dictionary.foreach (var key in mappings.Keys)
- Get the master page URL from the mappings dictionary.
var mappingObject = mappings[key]; var masterUrl = (string)mappingObject.GetType().GetProperty("MasterPageUrl", BindingFlags.Instance | BindingFlags.Public).GetValue(mappingObject, null);
- Output the master page settings for the device channel.
Console.WriteLine(""); Console.WriteLine("Site: " + web.Url); Console.WriteLine("Device Channel: " + key); Console.WriteLine("Master Page: " + masterUrl);
- Use the
Dispose
method to discard theSPWeb
object.web.Dispose();
See also
- The SharePoint 2013 Design Manager device channels article on MSDN at http://msdn.microsoft.com/en-us/library/jj862343.aspx
- The Reflection in the .NET Framework article on MSDN at http://msdn.microsoft.com/en-us/library/f7ykdhsy.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-SPSite topic on TechNet at http://technet.microsoft.com/en-us/library/ff607950.aspx