In this blog, I have explained how to show the documents in thumbnail view in SharePoint document libraries using JSLink.
Open a SharePoint site.
Create a Document library named "Thumbnail demo".
Click "Create" and upload images to it.
Let's build the code.
Create a JavaScript source file to inject into the SharePoint document library.
Declare the CSS from the reference
- var cssId = 'myCss';
- if (!document.getElementById(cssId)) {
- var head = document.getElementsByTagName('head')[0];
- var link = document.createElement('link');
- link.id = cssId;
- link.rel = 'stylesheet';
- link.type = 'text/css';
- link.href = 'https://sharepointtechie.sharepoint.com/sites/appnfc/SiteAssets/design.css';
- link.media = 'all';
- head.appendChild(link);
- }
Create a function to override the document library template
- (function() {
-
- var overrideCtx = {};
-
- overrideCtx.Templates = {}
-
- overrideCtx.Templates.Header = "<div>";
-
- overrideCtx.Templates.Item = thumbnailtemplate;
-
- overrideCtx.Templates.Footer = "<div>";
-
- SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
- })();
- This
- function provides the rendering logic
-
- function thumbnailtemplate(ctx) {
- console.log(ctx);
-
- var fileUrl = ctx.CurrentItem.FileRef;
-
- var titleInfo = ctx.currentItem.Title;
-
- var html = "<div class='responsive'><div class='gallery'><a target='_blank' href='" + fileUrl + "'><img src='" + fileUrl + "' width='300' height='200'></a><div class='desc'>" + titleInfo + "</div> </div></div>";
- return html;
- }
Full code
-
- var cssId = 'myCss';
- if (!document.getElementById(cssId)) {
- var head = document.getElementsByTagName('head')[0];
- var link = document.createElement('link');
- link.id = cssId;
- link.rel = 'stylesheet';
- link.type = 'text/css';
- link.href = 'https://sharepointtechie.sharepoint.com/sites/appnfc/SiteAssets/design.css';
- link.media = 'all';
- head.appendChild(link);
- }
- (function() {
- var overrideCtx = {};
- overrideCtx.Templates = {}
- overrideCtx.Templates.Header = "<div>";
- overrideCtx.Templates.Item = thumbnailtemplate;
- overrideCtx.Templates.Footer = "<div>";
- SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
- })();
-
- function thumbnailtemplate(ctx) {
- console.log(ctx);
- var fileUrl = ctx.CurrentItem.FileRef;
- var titleInfo = ctx.CurrentItem.Title;
-
- var html = "<div class='responsive'><div class='gallery'><a target='_blank' href='" + fileUrl + "'><img src='" + fileUrl + "' width='300' height='200'></a><div class='desc'>" + titleInfo + "</div> </div></div>";
- return html;
- }
Finally, the document library looks like this.
After injecting JSLink into a document library.
Click on the image to preview.
Happy SharePointing!....