logo
search
Permission & Access Issues

How to Securely Embed a SharePoint File or PDF in an iframe

Maira MehtabMaira Mehtab Sep 21, 2026 868 views

Question details

The user needs to embed a SharePoint file or PDF into a website or React application using an iframe, but encounters a Content Security Policy (CSP) frame-ancestors error.

Product
SharePoint Online
Device & OS
not provided
Scenario
Developing a website or React application that needs to display a shared or public SharePoint file or PDF to specific audiences.
Observed behavior
The SharePoint file URL opens correctly in a standard web browser but fails to load inside an iframe due to CSP restrictions.
Before you start

Ensure you have site owner or admin privileges for the SharePoint site hosting the file, and that you have access to modify the source code of your website or React application.

Solution 1Recommended

Use SharePoint's Built-in Embed Feature

Direct file URLs usually trigger CSP errors in iframes. Using Microsoft's official embed code feature generates a URL specifically designed for external embedding.

SharePoint Online enforces a strict Content Security Policy (CSP) that includes a 'frame-ancestors' directive. This intentionally blocks external domains from rendering its pages or files in standard iframes to prevent clickjacking attacks.

1
Open the File Online

Navigate to your file in SharePoint or OneDrive for Business and open it in the web browser (e.g., Word for the Web or the built-in PDF viewer).

2
Access the Embed Option

Click on 'File' in the top-left menu, select 'Share', and then choose 'Embed'.

3
Copy the Generated Code

A sidebar will appear with an automatically generated HTML iframe snippet. Copy this code.

4
Paste into Your Application

Paste this generated snippet directly into your website's HTML or adapt it for your React component instead of manually constructing the iframe URL.

Permission Requirements: The embedded file still respects SharePoint permissions. Users viewing your website must be logged into a Microsoft account with access to the file unless an anonymous 'Anyone with the link' URL was used.
Free Microsoft Office alternative

Easily Manage and Share PDF Documents with WPS Office

Tired of dealing with complex SharePoint embedding errors and strict Content Security Policies? WPS Office provides a lightweight, user-friendly alternative for creating, managing, and sharing Office and PDF files. You can effortlessly generate web-friendly sharing links that avoid strict iframe restrictions.

  1. 1. Download and Install: Get WPS Office from the official website and install it on your Windows, Mac, or Linux device.
  2. 2. Open Your File: Launch the application and open the PDF or Office document you need to distribute.
  3. 3. Share via Link: Click the 'Share' button in the top right corner to instantly generate a secure, easy-to-access link for your users.
Highly compatible with all major Microsoft Office formats (Word, Excel, PowerPoint).Built-in robust PDF editor for creating, annotating, and sharing documents.Generate straightforward public or restricted sharing links directly from the application.Free, lightweight, and features an intuitive tabbed interface for seamless workflows.
microsoft office alternative - wps office

Frequently Asked Questions

Why do I get a 'refused to display' error when embedding a SharePoint PDF?

This happens because Microsoft SharePoint sets a Content Security Policy (CSP) header with a 'frame-ancestors' directive. This security measure intentionally blocks unauthorized external websites from rendering the file in an iframe to prevent malicious clickjacking attacks.

Can I embed a SharePoint file for users who do not have Microsoft accounts?

Yes, but only if your organization's SharePoint sharing policies permit 'Anyone with the link' (anonymous sharing). You must generate an anonymous link and use that specific URL within your embed code snippet.

Is there a React package to bypass SharePoint iframe restrictions?

No client-side NPM package can bypass the server-side CSP headers sent by Microsoft. You must either use the official SharePoint generated embed code, fetch the file securely via the Microsoft Graph API, or adjust your SharePoint domain HTML Field Security settings.

How do I natively display a PDF from SharePoint without the standard web viewer?

Direct iframe embedding of the raw PDF file stream is generally blocked. To display the PDF natively, you will need to authenticate and download the file via Microsoft Graph API on your server backend, then serve that file directly to the frontend React component or website.