Skip to content

Instantly share code, notes, and snippets.

@marineko
Last active December 10, 2019 03:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marineko/41c6b2f91d7870d2c70e434dc657e861 to your computer and use it in GitHub Desktop.
Save marineko/41c6b2f91d7870d2c70e434dc657e861 to your computer and use it in GitHub Desktop.
Open Office document or PDF in new tab via file type icon click on SharePoint library view.
SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() {
function getBaseHtml(ctx) {
return SPClientTemplates["_defaultTemplates"].Fields.default.all.all[ctx.CurrentFieldSchema.FieldType][ctx.BaseViewID](ctx);
}
function init() {
var overrideCtx = {};
overrideCtx.Templates = {};
overrideCtx.Templates.Fields = {
'DocIcon': {
'View': function CustomIcon(ctx, field, listItem, listSchema) {
var str = "";
var itemApp = ctx.CurrentItem["File_x0020_Type.mapapp"];
if (ctx.CurrentItem.FSObjType == '1'|| itemApp == '' && ctx.CurrentItem.File_x0020_Type != "pdf") {
str = (ComputedFieldWorker.DocIcon(ctx, field, listItem, listSchema));
} else if(itemApp != ""){
str = '<a href="' + ctx.CurrentItem.FileRef + '" onmousedown="return VerifyHref(this,event,\'' + listSchema.DefaultItemOpen + '\',\'' + listItem["HTML_x0020_File_x0020_Type.File_x0020_Type.mapcon"] + '\',\'' + listItem["serverurl.progid"] + '\')" target="_blank">' + ComputedFieldWorker.DocIcon(ctx, field, listItem, listSchema) + '</a>';
} else {
str = '<a href="' + ctx.CurrentItem.FileRef + '" onmousedown="return VerifyHref(this,event,\''+ listSchema.DefaultItemOpen + '\',\'\',\'\')" target="_blank">' + ComputedFieldWorker.DocIcon(ctx, field, listItem, listSchema) + '</a>'
}
return str;
}
}
};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
}
RegisterModuleInit(SPClientTemplates.Utility.ReplaceUrlTokens("~siteCollection/SiteAssets/OpenFileViaIcon.js"), init);
init();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment