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.
Follow these steps to list the device channel master page configurations for each site in a site collection with PowerShell:
Microsoft.SharePoint.dll
and Microsoft.SharePoint.Publishing.dll
assemblies into the PowerShell session.[Reflection.Assembly]::LoadFrom("C:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Publishing.dll")
[Reflection.Assembly]::LoadFrom("C:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.dll")
Get-SPSite
Cmdlet.$site = Get-SPSite http://sharepoint/sitecollection
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")
$consMappingFileParams = ($typeWeb, $typeBool, $typeWeb)
MasterPageMappingsFile
object.$consMappingFile = $typeMappingFile.GetConstructor($consMappingFileParams)
MasterPageMappingsFile
object.$mappingFileParams = [System.Array]::CreateInstance([System.Object], 3) $mappingFileParams[1] = $false $mappingFileParams[2] = $null
foreach
loop to iterate through each SPWeb
in the AllWebs
property of the SPSite
object.foreach ($web in $site.AllWebs)
SPWeb
object to the parameters array and invoke the constructor to create an instance of the MasterPageMappingsFile
object.$mappingFileParams[0] = [Microsoft.SharePoint.SPWeb] $web
$mappingFile = $consMappingFile.Invoke($mappingFileParams)
Write-Host ""
Write-Host "Site: " $web.Url
Write-Host "Device Channel: Default"
Write-Host "Master Page: " $web.CustomMasterUrl
foreach
loop for each device channel key in the Keys
collection of the mapping file.foreach ($key in $mappingFile.Keys)
Write-Host "" Write-Host "Site: " $web.Url Write-Host "Device Channel: " $key Write-Host "Master Page: " $mappingFile[$key].MasterPageUrl
Dispose
method to discard the SPWeb
object.$web.Dispose()
Dispose
method to discard the SPSite
object.$site.Dispose()
PS1
file, for example, getdevicechannels.ps1
../getdevicechannels.ps1
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.
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:
using
statement.using (var site = new SPSite("http://sharepoint/sitecollection"))
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");
MasterPageMappingsFile
object.var consMappingFile = typeMappingFile.GetConstructor(new Type[] {typeof(SPWeb), typeof(bool), typeof(SPWeb)});
foreach
loop to iterate through each site in the AllWebs
property of the site collection.foreach (var web in site.AllWebs)
if (web.Exists)
MasterPageMappingsFile
object.var mappingFile = consMappingFile.Invoke(new object[] { web, false, null });
Console.WriteLine(""); Console.WriteLine("Site: " + web.Url); Console.WriteLine("Device Channel: Default"); Console.WriteLine("Master Page: " + web.CustomMasterUrl);
mappings
field from the mapping file and cast the object as an IDictionary
.var mappings = (IDictionary)typeMappingFile.GetField("mappings", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(mappingFile);
foreach
loop for each device channel key in the Keys
collection of the mappings dictionary.foreach (var key in mappings.Keys)
var mappingObject = mappings[key]; var masterUrl = (string)mappingObject.GetType().GetProperty("MasterPageUrl", BindingFlags.Instance | BindingFlags.Public).GetValue(mappingObject, null);
Console.WriteLine(""); Console.WriteLine("Site: " + web.Url); Console.WriteLine("Device Channel: " + key); Console.WriteLine("Master Page: " + masterUrl);
Dispose
method to discard the SPWeb
object.web.Dispose();
3.15.221.133