How to Add Smart Previews to Web Pages Outside of SmartHub
Smart Previews for SmartHub can be used within a custom search page without loading the SmartHub UI framework.
To load Smart Previews as a stand-alone component, you must first load a JavaScript file, as described in the topics below.
How to Load the SmartHub Smart Previews API
Use the steps below to load the SmartHub Smart Previews API:
- Locate the SmartPreviewsAPI.js file.
- Find it at <SmartHub_Folder_Install_Dir>/modules/SmartPreviews/SmartPreviewsAPI.js
- Load the SmartPreviewsAPI.js file in your custom page.
- (Optional) Load the file SmartHubFolderInstallLocation/modules/SmartPreviews/SmartPreviews.css
- This is required only if you want to show the document preview in a custom container.
- Check the BAI.SmartPreviews.ShowDocumentPreview function definition and the sample integration example below for more details.
SmartHub Smart Previews API Functions and Parameters
- BAI.SmartPreviewAddress
- This string represents the Smart Previews Web Resources site address.
- This string represents the Smart Previews Web Resources site address.
- BAI.SmartPreviews.LoadSmartPreviews
- This method loads the required SmartPreviews resources.
- This method must be called on each page load.
- Parameters:
- address:
- This is the SmartPreviews Web Resources site address.
- Example: https://smartpreviewswebresources.bainsight.com
- securityTokenFunction:
- A function that provides a new token when the requests are failing with the unauthorized exception.
Note: If after 5 attempts the requests still fail, a warning message will be displayed in the console and the unauthorized error will be displayed in the preview viewer.
- A function that provides a new token when the requests are failing with the unauthorized exception.
- callback:
- Callback function that will be called when the resource loading is complete.
Note: You must wait for the callback function to be called before trying to show previews.
- Callback function that will be called when the resource loading is complete.
- address:
- BAI.SmartPreviews.SetSecurityToken
- This function sets the internal authentication token that SmartPreviews uses.
- It must be called on each page load and every time the token expires.
- Parameters:
- token: OAuth2 compliant JWT token.
- Token must be valid (not expired) and contain the "upn" claim.
- Token must be valid (not expired) and contain the "upn" claim.
- token: OAuth2 compliant JWT token.
- BAI.SmartPreviews.CheckForPreviews
- This function checks the database for the preview generation status for documents.
- SmartHub uses this function to decide if documents support Smart Previews and adds the binoculars icon.
- Parameters:
- documentDefinition
Array of objects which define document definitions.
Each object must have the following structure (note that the URL property must be URL encoded):
Copyvar itemDefinition = {
Url: encodeURIComponent(<document_url>),
Title: <document_title>,
Extension: <document_extension>,
LastModified: <document_lastModifiedDate>,
FileSize: <document_size>
}
- callback
- Callback function that is called when the response is ready.
- The response is a JSON string so you need to parse it before using it.
- It contains pairs of document URLs and preview status codes:
- Previews status codes:
- 0 - Document preview is generated in the database
- 1 - Document is not eligible for on-demand preview generation
- 2 - Document is eligible for on-demand preview generation
- 3 - Document preview generation is in progress (status will become 0)
- Previews status codes:
- documentDefinition
- BAI.SmartPreviews.ShowDocumentPreview
- This function shows the document preview
- Parameters
- documentUri
- Document URL which must be encoded.
- documentOriginalPath
- The original URL of the document, not the escbasecrawlurl, which must be encoded.
- documentTitle
- The document title that will appear at the top of the Document Viewer.
- Default value: "Preview".
- keywords
- String containing the words that should be highlighted in the document preview.
- options
- OpenSearchPanelByDefault: Set to true to open the search inside panel, by default, when opening the document viewer.
OpenBookmarksPanelByDefault: Set to true to open the bookmarks panel, by default, when opening the document viewer.
Limitation: You cannot have both
OpenSearchPanelByDefault
andOpenBookmarksPanelByDefault
set to true at the same time.- ShowDescriptiveText: Set to true to display a descriptive text to help users understand what they see in the document viewer.
WidgetsToLoadForCustomViewer: Specify what widgets to load when the preview is displayed in a custom viewer:
- htmlPreview (Required): Displays the preview content.
- docNav (Required): Displays the navigation bar for pagination.
- zoomControl (Optional): Displays the zoom control.
searchToolbar (Optional): Displays the button that shows/hides the "Search inside" panel.
If the
OpenSearchPanelByDefault
setting is set to true, the searchToolbar widget becomes required.- docActions (Optional): Displays the preview actions button.
- bookmarksToolbar (Optional): Displays the button that shows/hides the bookmarks panel.
- UITimeoutSeconds:The number of seconds after which the TimeoutWarningMessage is displayed.
- Note: If you do not want to display a message, set it to -1.
- TimeoutWarningMessage: The message that will be displayed when the online previews generation takes too long.
- Default value:"Generating preview for this document is taking longer than %time% seconds. Please check back in a few minutes."
%time%will be replaced with the actual number specified in the UITimeoutSeconds setting.
Limitation:
UITimeoutSeconds
UITimeoutSeconds
andTimeoutWarningMessage
settings work only with SmartPreviews 3.3 or newer versions.
- parentContainerSelector (Optional) - Use this if you want to show the document preview in a custom page container.
- If left unspecified then the preview will be shown in the Document Viewer (right side of screen).
- If you want to use a custom container then you must ensure container CSS plays well with the default SmartPreviews CSS.
- A good starting point would be to inject the SmartPreviews.css file.
- Check the "Sample integration in a new HTML page" section below.
- documentUri
- BAI.SmartPreviews.RemoveAllViewers
- Function that closes all the open Document Viewers
- Function that closes all the open Document Viewers
- BAI.SmartPreviews.GetPreviewVersion
- This function returns the major version of the installed Smart Previews version.
Sample Integration in a New HTML Page
The following HTML page does the following, in order:
- Loads the SmartHub SmartPreviews API via the "/modules/SmartPreviews/SmartPreviewsAPI.js" file.
- Load the custom SmartPreviews CSS via the "/modules/SmartPreviews/SmartPreviews.css" file.
- Loads the SmartPreviews resources by calling the BAI.SmartPreviews.LoadSmartPreviews function.
- Sets the security token.
- Defines a sample document URL.
- Checks the preview status for the document by calling the BAI.SmartPreviews.CheckForPreviews function.
- Logs the response in the browser console.
- Shows document preview in the Document Viewer via the call to BAI.SmartPreviews.ShowDocumentPreview(encodeURIComponent(itemUrl), encodeURIComponent(originalItemUrl), "Document preview", "query here");
- Shows document preview in the
<div>
withid="previewContainer"
via the call to BAI.SmartPreviews.ShowDocumentPreview(encodeURIComponent(itemUrl), encodeURIComponent(originalItemUrl), "Document preview", "query here", undefined, "#previewContainer");
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="http://localhost:5598/modules/SmartPreviews/SmartPreviews.css">
<script type="text/javascript" src="http://localhost:5598/modules/SmartPreviews/SmartPreviewsAPI.js"></script>
<script>
var smartPreviewsAddress = "https://smpwebresources.bainsight.com";
BAI.SmartPreviews.LoadSmartPreviews(smartPreviewsAddress, function () {
var token = "eyJ0eXAiOiJKV1...";
BAI.SmartPreviews.SetSecurityToken(token);
var itemUrl = "https://tenant.sharepoint.com/sites/MySite/Documents/sample document.pdf"; //url must be decoded
BAI.SmartPreviews.CheckForPreviews([itemUrl], function (response) {
console.log(response);
});
BAI.SmartPreviews.ShowDocumentPreview(encodeURIComponent(itemUrl), encodeURIComponent(originalItemUrl), "Document preview", "query here", { OpenSearchPanelByDefault: true });
// BAI.SmartPreviews.ShowDocumentPreview(encodeURIComponent(itemUrl), encodeURIComponent(originalItemUrl), "Document preview", "query here", { OpenSearchPanelByDefault: true, WidgetsToLoadForCustomViewer: ["htmlPreview", "docNav", "searchToolbar"] }, "#previewContainer");
});
</script>
</head>
<body>
<div id="previewContainer" class="bai-document-viewer-container"></div>
</body>
</html>