Skip to main content

JQuery: Play video attachment of notes in Dynamics CRM 2013

When you click on any attachment file of notes in Dynamics CRM, by default the download process will start and save the file into your local computer.




But you can restrict this behavior with jquery and you can open the attachments in your browser window - No need to download.

Save the below code in a .js file and upload into web resources area in dynamics crm and register at form on-load event.




The below code will generate links to all the Mp3, Mp4..Etc format files, and once you clicked on the links the media will open in a popup window.

function updateTabEvent() {

    $(".tabLink[tabid='NotesTab']").click(function () {

        setTimeout(function () {

            updateLinks();

        }, 500);

    });

}



function updateLinks() {

    var linksLoaded = 0;

    $("a.attachmentFileName").each(function (index) {

        if ($(this).prop("href") && $(this).prop("href") != '') {

            linksLoaded = 1;



            var title = $(this).prop("title");

            var link = $(this).prop("href");



            var Type = title.split(".");



            if (Type[Type.length-1] == "mp4" || Type[Type.length-1] == "MP4") {

                $(this).parent().html("<span onclick='displayVideo(\"" + link + "\");'>" + title + "</span>");

            }

            if (Type[Type.length-1] == "mp3" || Type[Type.length-1] == "MP3") {

                $(this).parent().html("<span onclick='PlayAudio(\"" + link + "\");'>" + title + "</span>");

            }

        }

    });

    if (linksLoaded == 0) {

        setTimeout(function () {

            updateLinks();

        }, 500);

    }

}



function displayVideo(link) {



    if ($('#videoDiv').length > 0) {

        videoClose();

    }



    if ($('#videoDiv1').length > 0) {

        videoClose();

    }

    var left = ($(window).width() - 640) / 2;

    var top = ($(window).height() - 480) / 2;
    var $div2 = $("<div id='videoDiv1' onclick='videoClose()' style='width: 100%; height: 100%; position:absolute; z-index: 2000;'></div><div id='videoDiv' border  style='width: 640px; height: 480px; position:relative; z-index: 2002; left:" + left + "px; top: " + top + "px'><video id='video'  style='width: 640px; height: 480px;  z-index: 2001; position:absolute;' controls><source src='" + link + "' type='video/mp4'></video></div>").appendTo("body");
    $("#videoDiv").focus();





}



function PlayAudio(link) {



    if ($('#audioDiv').length > 0) {

        videoClose();

    }



    if ($('#audioDiv1').length > 0) {

        videoClose();

    }

    var left = ($(window).width() - 640) / 2;

    var top = ($(window).height() - 280) / 2;
    var $div2 = $("<div id='audioDiv1' onclick='audioClose()' style='width: 100%; height: 100%; position:absolute; z-index: 2000;'></div><div id='audioDiv' border  style='position:relative; z-index: 2002; left:" + left + "px; top: " + top + "px'><audio id='audio'  style='width: 640px; height: 100px;  z-index: 2001; position:absolute;' controls><source src='" + link + "' type='audio/mpeg'></audio></div>").appendTo("body");
    $("#audioDiv").focus();



}

function videoClose() {

    $("#videoDiv1").remove();

    $("#videoDiv").remove();



}



function audioClose() {

    $("#audioDiv1").remove();

    $("#audioDiv").remove();



}

If you click on .MP3 attachment file, it will play in a popup window like below.
If you click on .MP4 attachment file, one popup window will open and play the video like below.


Note that the above code has to register on Form Onload event.

Comments