Tuesday, July 10, 2012

Opening .Msg and .Pdf files in Browser


Challenge
We have a problem in our environment that .MSG file and .PDF documents will be prompt to download before you can open them…  Here are the screen shots that made the users mad! 


Option 1
The common solution is changing the Browser file handling from "strict" to "permissive"…
  • Go to Central Administration > Manage Web Applications > Your WebApp > General Settings
  • Change Browser file handling from "strict" to "permissive"

Unfortunately, it does not work for me.

Option 2
So I tried second approach, add the correct MIME type to IIS for both .msg files and .PDF files.
  • Start Internet Information Manager
  • Select the server node in the left pane, pick your web application.
  • in Right Panel, Open in the MIME Types settings, click add.
  • Enter .msg for file name extension and application/vnd.ms-outlook
  • Repeat for all front end servers


Then run the following script

##============================================================================##
## Allowed Inline Downloaded MimeType for .Msg (Outlook) and .PDF in IIS
## Version: 1.0
## Last updated: 7 July, 2012
## Description: This script add value in AllowedInlineDownloadedMimeTypes for
## .msg files and PDF files
## Reference: http://ianankers.wordpress.com/author/ianankers/page/2/
##==============================================================================#

## Add .Msg (Outlook file type)
$webApp = Get-SPWebApplication http://yourspweburl
If ($webApp.AllowedInlineDownloadedMimeTypes -notcontains "application/vnd.ms-outlook")
{
  $webApp.AllowedInlineDownloadedMimeTypes.Add("application/vnd.ms-outlook")
  $webApp.Update()
  Write-Host "application/vnd.ms-outlook added to AllowedInlineDownloadedMimeTypes"
}

## Add .Pdf
If ($webApp.AllowedInlineDownloadedMimeTypes -notcontains "application/pdf")
{
  $webApp.AllowedInlineDownloadedMimeTypes.Add("application/pdf")
  $webApp.Update()
  Write-Host "application/pdf added to AllowedInlineDownloadedMimeTypes"
}

Well I have tried it and it does not work for me again…

Option 3 (Finally – This work!)
Third tried - Further investigation, it turned out that the web site was created using the previously saved template; the template was created when the web application had 'Browser File Handling' set to 'Strict'. So changing the 'Browser File Handling' in Web Application will have no effect on these sub sites.

The situation has pointed out exactly as Craig Lussier mentioned in his response in the Forums

So here is powershell script that should rescue you from the situation.
##============================================================================##
## PowerShell to Set the 'Browser File Handling' setting to 'Permissive'
## Version: 1.0
## Last updated: 7 July, 2012
## Description: This script will set BrowserFileHandling to "Permissive" for
## specific a document library
## Reference: http://ianankers.wordpress.com/author/ianankers/page/2/
##==============================================================================#

#Get Web
$web = Get-SPWeb "http://yourspweburl"

#Get Document Library
$docLib = $web.lists["Document Library"]

#View all properties/methods of the Document Library and you'll see that BrowserFileHandling is a property
$docLib | Get-Member

#See the current BrowserFileHandling setting for the Document Library
$docLib.BrowserFileHandling

#If you need to change it from Strict to Permissive
$docLib.BrowserFileHandling = "Permissive"
$docLib.Update()

After the executed the script, I clicked on the .Msg file, Voilà! The "Open" button shows up, 

and when I clicked on the PDF, the browser simply open the PDF without a prompt.


Update: 12 Nov 2012, 
Just stumbled to NICO's blog, where he created a nice script and log the changes as well.
http://gallery.technet.microsoft.com/office/Change-Browser-File-e60b46c9

Here is the script

param  

[string] $URL, 
[boolean] $writeToFile = $true 

#Change Browser File Handling for all lists and libraries in SharePoint 2010 farm 
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

#Counter variables 
$webcount = 0 
$listcount = 0 

if($writeToFile -eq $true) 

$outputPath = Read-Host "Outputpath (e.g. C:\directory\filename.txt)" 


if(!$URL) 

#Grab all webs 
$webs = (Get-SPSite -limit all | Get-SPWeb -Limit all -ErrorAction SilentlyContinue) 

else 

$webs = Get-SPWeb $URL 

     
if($webs.count -ge 1 -OR $webs.count -eq $null) 

    foreach($web in $webs) 
    { 
    #Grab all lists in the current web that have the "Strict" Browser file handling 
    $lists = $web.Lists | ?{$_.BrowserFileHandling -eq "Strict"} 
    #If there are lists that have the strict browserfilehandling 
        if(-not(!$lists)) 
        { 
        Write-Host "Website"$web.url -ForegroundColor Green   
        if($writeToFile -eq $true){Add-Content -Path $outputPath -Value "Website $($web.url)"} 
            foreach($list in $lists) 
            {  
            #Change the browser file handling to permissive 
            $list.BrowserFileHandling = "Permissive"  
            $list.Update() 
            $listcount +=1 
            Write-Host " - "$list.Title updated           
            if($WriteToFile -eq $true){Add-Content -Path $outputPath -Value " - $($list.Title) updated"} 
            } 
    $webcount +=1 
    $web.Dispose() 
        } 
    } 
#Show total counter for checked webs & lists 
Write-Host "Amount of webs that were updated:"$webcount 
Write-Host "Amount of lists updated:"$listcount 
if($WriteToFile -eq $true){Add-Content -Path $outputPath -Value "Amount of webs updated:$($webcount)"; Add-Content -Path $outputPath -Value "Amount of lists updated: $($listcount)"} 

else 

Write-Host "No webs retrieved, please check your permissions" -ForegroundColor Red -BackgroundColor Black 
}

Well hope this help! Thanks!

3 comments:

  1. Option #2, without the script, works for straight HTML (non-SharePoint) sites. Just set the MIME type for the server node in IIS 7. Great post, saved me a lot of work and allowed us to link to some e-mails that were needed to be accessed.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. While marketing can be tricky, it is the content that makes the site important. If you provide informative content that serves the needs of your visitors, word will spread.
    usps address change

    ReplyDelete