Monday, December 12, 2011

Open PDF files directly in browser SharePoint 2010

Here is how you can tell SharePoint 2010 site to open the PDF files in the browser instead of prompting the user to save or open the PDF. This will be a great option to the most of end users. This needs simple Powershell scripting run on the server. Below is the code we should use:
# <# 
# .DESCRIPTION 
#  This script adds new MIME type to "AllowedInlineDownloadedMimeTypes" property list of defined SharePoint 2010 Web Application.
#
#  Script prompts you for MIME type and Web Application URL.
#
#  Code shall run in context of Farm Administrators group member.
# 
# .NOTES 
#       File Name   : Add_MIME_Type.ps1 
#       Author      : Kamil Jurik, WBI Systems a.s. 
#       Created     : 11/12/2011 
# 

If ( (Get-PSSnapin -Name "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null ) {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
Get-SPWebApplication
$WebApp = Get-SPWebApplication $(Read-Host "Enter Web Application URL")
Write-Host "Mime Type Examples:""application/pdf, text/html, text/xml"

If ($WebApp.AllowedInlineDownloadedMimeTypes -notcontains ($MimeType = Read-Host "Enter a required mime type"))
{
  Write-Host -ForegroundColor White "Adding" $MimeType "MIME Type to defined Web Application"$WebApp.url
  $WebApp.AllowedInlineDownloadedMimeTypes.Add($MimeType)
  $WebApp.Update()
  Write-Host -ForegroundColor Green "The" $MimeType "MIME type has been successfully added."
} Else {
  Write-Host -ForegroundColor Red "The" $MimeType "MIME type has already been added."
}
The powershell script do the magic for us. Below is the explanation.
  1. It is taking the web application url as input
  2. And if the web application is not allowed the the PDF files MIME type to it's allow downloadable mime types collection then it will add it.
So, once you run the code the files will be open up in the browser instead of prompting the user. The server will not allow PDF files by default as it think PDF files are not secure.

Note: The script should run with the account who has FARM administrator level access.
For more details about it: please check technet article.

7 comments:

  1. This is the only solution I have found to work. VERY Well done and many kudos.

    ReplyDelete
  2. This is the only solution I have found to work. Many Many Kudos. Brilliant. Thank you.

    ReplyDelete
  3. This is the only solution I have found to work. Many Many Kudos. Brilliant. Thank you.

    ReplyDelete
  4. Thank you very much...

    Been scratching my head for hours. And your solution works as expected.

    Thumbs up...

    ReplyDelete
  5. Hi Praveen

    Thanks for this nice post. I'm new to SP 2010. Could you please guide me how do I run this script in SP 2010 powershell?

    Many Thanks in Advance

    ReplyDelete
  6. Hi Praveen,
    Thank you very much for this post.

    Do you have any solution for users having only read permission and should not save the pdf file once open in browser.

    cheers..
    Kiran

    ReplyDelete
  7. This is awesome. Was getting a problem to execute directly in PowerShell but then downloaded the direct .ps1 file here: http://gallery.technet.microsoft.com/scriptcenter/Add-new-MIME-type-open-PDF-f6c57c32/file/47247/1/Add_MIME_Type.ps1

    ReplyDelete