web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / HIMBAP / Image Gallery Utility for D...

Image Gallery Utility for Dynamics CRM

Mahendar Pal Profile Picture Mahendar Pal 45,095

In our earlier article we have demonstrated how we can fetch images attached to notes, working on the same requirement we have enhanced our examples to develop an image gallery utility to see all the images attached to notes. Here is the complete code for this utility:

<!--This is a Image Gallery unity, which can be used with any entity which have
relationship with Notes, this web resource will show all the images attached to notes -->
    < html > < head >
    < style type = "text/css" >

    .left - div {
        display: inline - block;
        cursor: pointer;
    }
    .right - div {
        cursor: pointer;
        display: inline - block;
    } < /style>  < meta charset = "utf-8" >
    < /head><body style="-ms-zoom: 1;">  
    < script src = "../../ClientGlobalContext.js.aspx" > < /script> 
    < script src = "../Script/SDK.REST.js" type = "text/javascript" > < /script> < script type = "text/javascript" >
//variables
var currentImage = 0;
var img = document.createElement("IMG");
var images = new Array;
var TotalImages = 0;
var SectionName;
var TabName;
document.onreadystatechange = function() {
        if (document.readyState == "complete") {
            //get query parameters
            var queryParam = GetGlobalContext().getQueryStringParameters().data;
            var fields = queryParam.split(",");
            TabName = fields[0];
            SectionName = fields[1];
            //hide control in case form create
            if (isformtypeCreate())
                showImageGalery(TabName, SectionName, false)
            else {
                showImageGalery(TabName, SectionName, true)
                getnotesImages();
            }
        }
    }
    //check for form create mode
function isformtypeCreate() {
    if (window.parent.Xrm.Page.ui.getFormType() == 1) //create mode
        return true;
    else
        return false;
}
//hide/show image gallery
function showImageGalery(tabname, sectionname, flag) {
    window.parent.Xrm.Page.ui.tabs.get(tabname).sections.get(sectionname).setVisible(flag);
}
//get images from notes
function getnotesImages() {
    var regardingObjectId = window.parent.Xrm.Page.data.entity.getId();
    var entitySchemaName = "Annotation";
    var odataQuery = "?$select=AnnotationId,DocumentBody,MimeType&" +
        "$filter=ObjectId/Id eq guid'" + regardingObjectId +
        "' and IsDocument eq true and startswith(MimeType,'image/') ";
    if (typeof(SDK) != "undefined") {
        SDK.REST.retrieveMultipleRecords(entitySchemaName, odataQuery, getnotesImagesCallback, function(error) {
            alert(error.message);
        }, function() {});
    } else {
        alert("REST.SDK library is not available");
    }
}
//callback method
function getnotesImagesCallback(resultSet) {
    if (resultSet.length > 0) {
        TotalImages = resultSet.length;
        for (i = 0; i < resultSet.length; i++) {
            var mimeType = resultSet[i].MimeType;
            var body = resultSet[i].DocumentBody;
            images[i] = "data:" + mimeType + ";base64," + body;
        }
        changeImage(0);

    } else
        showImageGalery(TabName, SectionName, false);
}
//change image 
function changeImage(counter) {
      currentImage += counter;
    if (currentImage != TotalImages) {
        if (currentImage < 0)
            currentImage = 0;
    } else {
        currentImage = 0;
    }
    img.src = images[currentImage];
    document.getElementById('imageGallery').appendChild(img);
}

< /script> < div >


< input onclick = "getnotesImages()"
alt = "Reload"
type = "image"
src = "../Image/Refresh.png" >
    < input class = "left-div"
alt = "Go Next"
onclick = "changeImage(-1);"
type = "image"
src = "../Image/Back.png" >
    < input class = "right-div"
alt = "Go Back"
onclick = "changeImage(1);"
type = "image"
src = "../Image/Next.png" >
    < /div>  < div id = "imageGallery"
style = "width: 100%; overflow: hidden;" >
    < /div></body >
    < /html>

You can download complete utility from GitHub https://github.com/himbap/ClientSideUtilities/releases

Following are the steps to use this Gallery
1. Download Solution from https://github.com/himbap/ClientSideUtilities/releases
2. Import this solution to your CRM environment (We can place this web resource to any entity which has relationship with notes)
3. Let’s add this to account form, Open account entity form editor, and add a new section to existing tab or add a new tab and section to form.
4. Double click on tab and make note of tab name, follow similar steps for section and make note of section name, we need to pass these to web resource.
5. Select your section and click on Web Resource by navigating to insert tab under ribbon bar.
6. Select web resource properties like following:
Imagegallery1
7. Click on OK and save, publish your changes.
8. Open any account record and attach image using notes.

Imgegallery3

9. Refresh your record first time to get image gallery, and you should be able to see it like below:

Imagegallery2

HIMBAP | Need any help in Microsoft CRM Contact US !!


This was originally posted here.

Comments

*This post is locked for comments