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:

  1. Open your preferred text editor to create the PS1 script file.
  2. Load the 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")
    
  3. Get the site collection using the Get-SPSite Cmdlet.
    $site = Get-SPSite http://sharepoint/sitecollection
    
  4. 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")
    
  5. Create an array of the object types.
    $consMappingFileParams = ($typeWeb, $typeBool, $typeWeb)
    
  6. Get the class constructor for the MasterPageMappingsFile object.
    $consMappingFile = $typeMappingFile.GetConstructor($consMappingFileParams)
    
  7. 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
    
  8. Use a foreach loop to iterate through each SPWeb in the AllWebs property of the SPSite object.
    foreach ($web in $site.AllWebs)
    
  9. Add the 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)
    
  10. 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
    
  11. Use a foreach loop for each device channel key in the Keys collection of the mapping file.
    foreach ($key in $mappingFile.Keys)
    
  12. 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
    
  13. Use the Dispose method to discard the SPWeb object.
    $web.Dispose()
    
  14. Use the Dispose method to discard the SPSite object.
    $site.Dispose()
    
  15. Save the file as a PS1 file, for example, getdevicechannels.ps1.
  16. Execute the script in the PowerShell session.
    ./getdevicechannels.ps1
    
    How to do it...

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.

Note

A reference to the Microsoft.SharePoint.Publishing.dll assembly is required for this recipe.

Follow these steps to list the device channel master page configurations for each site in a site collection using the server-side object model:

  1. Get the site collection with a using statement.
    using (var site = new SPSite("http://sharepoint/sitecollection"))
  2. 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");
  3. Get the class constructor for the MasterPageMappingsFile object.
    var consMappingFile = typeMappingFile.GetConstructor(new Type[] {typeof(SPWeb), typeof(bool), typeof(SPWeb)});
  4. Use a foreach loop to iterate through each site in the AllWebs property of the site collection.
    foreach (var web in site.AllWebs)
  5. Ensure that the site exists.
    if (web.Exists)
  6. Invoke the constructor to create an instance of the MasterPageMappingsFile object.
    var mappingFile = consMappingFile.Invoke(new object[] { web, false, null });
  7. 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);
  8. Get the 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);
  9. Use a foreach loop for each device channel key in the Keys collection of the mappings dictionary.
    foreach (var key in mappings.Keys)
  10. 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);
  11. 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);
  12. Use the Dispose method to discard the SPWeb object.
    web.Dispose();

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.221.133