4

I applied javascripting to document library to be able to allow users to open the document by clicking the icon button. But when they click on the icon it opens to the display view. I added the "source""...etc but that didn't work either. Can someone help to see if my script is off or I have the document library settings off? Thanks for your help

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script/>
<script type="text/javascript">
$(document).ready(function() {
    $('.ms-draggable').each(function() {
        var alt = $(this).attr('alt');
        alt = "http://google/sites/test/Shared Documents/Forms/DispForm.aspx?ID=/" + alt;
        var a = $('<a/>').attr('href', alt);   
        $(this).wrap(a);
    });
});
</script>
6

You are creating an <a>-tag with href pointing to DispForm.aspx but not to the document itself. A better approach would be to utilize CSR via JSLink.

Here is some CSR code that should get you started:

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 = ""
                    if (ctx.CurrentItem.FSObjType == '1') {
                        str = (ComputedFieldWorker.DocIcon(ctx, field, listItem, listSchema));
                    } else {
                        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"] + "')'>" + ComputedFieldWorker.DocIcon(ctx, field, listItem, listSchema) + "</a>"
                    }
                    return str;
                }
            }
        };
        SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
    }
    RegisterModuleInit(SPClientTemplates.Utility.ReplaceUrlTokens("~siteCollection/Style Library/OpenDocumentViaIcon.js"), init);
    init();
});

Create a file called OpenDocumentViaIcon.js with this code and upload it to the Style Library. Then copy ~siteCollection/Style Library/OpenDocumentViaIcon.js into the JSLink property field of your List View Webpart.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.