Disable Mobile Redirection for a Web Application via PowerShell

So I’ve got a SharePoint site that is optimised for viewing in mobile browsers, problem is that OOTB SharePoint is trying to show the fugly “mobile view”. Now Waldek has a post about just this problem, in my case I’ve decided that his first offered work around is perfectly acceptable for my scenario.

Now if you haven’t worked it out yet I also have a requirement that my deployments must be 100% repeatable and driven my PowerShell. So I need to modify the web.config file via PowerShell, enter a helpful post from the Script Guy blogs.

From there it wasn’t too hard to create my own script:

#Disable Mobile Redirection for all browsers for a given web application

param( [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)] 
			[Microsoft.SharePoint.PowerShell.SPWebApplicationPipeBind]
			$WebApplication)
	
# SharePoint cmdlets
Add-PSSnapin Microsoft.SharePoint.PowerShell
if([Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")-eq$null){throw "Unable to load Microsoft.SharePoint.dll";}

$WebApp = $WebApplication.Read()

$configMod1 = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
$configMod1.Path = "configuration/system.web"
$configMod1.Name = "browserCaps"
$configMod1.Value = '<browserCaps><result type="System.Web.Mobile.MobileCapabilities, System.Web.Mobile, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /><filter>isMobileDevice=false</filter></browserCaps>'
$configMod1.Sequence = 0
$configMod1.Owner = "contoso\administrator"
## SPWebConfigModificationType.EnsureChildNode -> 0
$configMod1.Type = 0

$WebApp.WebConfigModifications.Add( $configMod1 )

$WebApp.Update()

$WebApp.Parent.ApplyWebConfigModifications() 
	
Remove-PsSnapin Microsoft.SharePoint.PowerShell
	

I highly recommend that you read both the blogs I linked above, it’s articles like those that enable me to stand on the shoulders of giants to achieve my ends

Advertisement
This entry was posted in Deployment, Development, PowerShell, SharePoint. Bookmark the permalink.

7 Responses to Disable Mobile Redirection for a Web Application via PowerShell

  1. Pingback: Switch Off Default Mobile Experience in SharePoint 2010 « Sladescross's Blog

  2. Don says:

    Great script! One issue though every time we deploy a new WSP to the farm,duplicate entries get added to each WebApp web.config file this was ran against, thus breaking the site. Any advice on reversing this and removing the persistent entry in the config db?

    Don

    • gavinbarron says:

      Yup, pretty simple really, the WebModifications property on which .Add method is called is just a collection and it has a .Contains method, so just add a check that the key isn’t already in the WebModificatons collection, something like this untested powershell implementation:

      if($WebApp.WebConfigModifications.Contains( $configMod1 ) -eq $false)
      {
      $WebApp.WebConfigModifications.Add( $configMod1 )
      }

  3. Pingback: How to add configuration settings using SPWebConfigModification and PowerShell Script « YBBEST

  4. Niels O. Jensen says:

    The .Name-line should read:
    $configMod1.Name = “browserCaps”

    – otherwise, running a .Remove on the SPWebConfigModification won’t remove the lines from the web.config-files, and re-adding the SPWebConfigModification will result in duplicate entries in the web.config-file.

    If you have more entries than the above under browserCaps, you may need to refine the .Name further.

    Unfortunately a .Name is not just a .Name in SPWebConfigModifications.

  5. Pingback: Disable mobile browsing for a single website in SharePoint 2010 - DL-UAT

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.