Showing posts with label Dynamics CRM. Show all posts
Showing posts with label Dynamics CRM. Show all posts

1/30/14

MSCRM Get User Information with JavaScript

When working with Dynamics CRM forms, we can use the XRM client object model to query the fields and controls on the page. A common field on every form is the current user id. This however only provides the internal system id or GUID. When we need more information about the user, we can use the ODATA web services to get additional information.

This script provides us with a helper function to enable us to find additional information about the current logged on user.

if (typeof (OGBIT) == "undefined") { OGBIT = {}; }
OGBIT.UserInfo = {   
    Get: function () {

        var userInfo = {};
        if (self.userName == '') {
        var serverUrl = window.location.protocol + "//" + window.location.host + "/" + Xrm.Page.context.getOrgUniqueName();
           
            var userId = Xrm.Page.context.getUserId();
            var req = serverUrl + "/xrmservices/2011/OrganizationData.svc/SystemUserSet(guid'{0}')?$select=EmployeeId,FullName,InternalEMailAddress,SystemUserId,DomainName".replace('{0}', userId);
            try {
                $.ajax({
                    type: "GET", contentType: "application/json; charset=utf-8", datatype:"json",async:false,
                    url: req,
                    beforeSend: function (XMLHttpRequest) {
                        XMLHttpRequest.setRequestHeader("Accept", "application/json");
                    },
                    success: function (data, textStatus, XmlHttpRequest) {
                        userInfo = data.d;
                        userInfo.status = true; //dynamic property
                    },
                    error: function (XmlHttpRequest, textStatus, errorObject) {
                        userInfo.status = false;
                        //add error handling

                    }
                });
            } catch (e) { }
        }
        return userInfo;
    }
}

The script first calls the getUserId method of the page context. This returns the system id which we can then use to make an ODATA request to get the additional user information. The ODATA query to get the user information has this format:

SystemUserSet(guid'{0}')?$select=EmployeeId,FullName,InternalEMailAddress,SystemUserId,DomainName

The SystemUserSet allows us to query the System User entity.  We query for the current user with the guid’{0}’ parameter. The $select query option is used to indicate what additional fields should be returned. Those fields are listed below:

Parameters:
Field Name
Description
EmployeeId
employee id
FullName
First and last name
InternalEMailAddress
Email
SystemUserId
Internal ID or GUID
DomainName
Username with this format:
DomainName\Username

The following snippet can be used to test the script. With this function, we just call the Get method and assign the returning information into local variables.

function GetUserInfo() {
    if (typeof (OGBIT.UserInfo) != 'undefined') {
        var info = OGBIT.UserInfo.Get();
        if (info.status) {
            var empId = info.EmployeeId;
            var name = info.FullName;
            var email = info.InternalEMailAddress;
            var userName = info.DomainName;
            var id = info.SystemUserId;
        } else {
            alert('Unable to find user');
        }
    }
}


I hope this Script helps you get more understanding on how to interact with MS CRM.

8/25/13

Dynamics CRM How to Access Page Controls with JavaScript

When working with Dynamic CRM custom solutions, I often have to implement some UI control behaviors with JavaScript.  The table below shows some of the common tasks that can be done when using the XRM JavaScript framework that comes with the Dynamics platform.

Task
XRM JavaScript Framework API
Show or Hide a control
setVisible(cmd)

cmd bool  value:  
true  to show
false to hide

Disable or enable a control
setDisabled(cmd)

cmd bool  value:
true = to disable
false to enable
Focus
setFocus()

This table shows the steps to access a control and set several properties:

Tasks
Script
Get a control reference
var control = Xrm.Page.getControl(name.toLowerCase());

Note:  Controls on the web form are set with lowercase letters.

Disable a control
control.setDisabled(true);

Enable a control
control.setDisabled(false);

Show a control
control.setVisible(true);

Hide a control
control.setVisible(false);

Set the focus
control.setFocus();



I hope you enjoy this quick reference.

6/29/13

Dynamics CRM Set IFrame to a Blank URL

When adding an IFrame to an entity form, Dynamics CRM requires that a default URL be defined. When we do not know the URL at design time, we commonly would just set the URL to the string about:blank. When we type this on a browser, this basically tells the browser to load an empty HTML page which is the ideal approach when the URL should be defined dynamically.

The problem is that when we attempt to do this on Dynamics CRM, we get this error:

Invalid Protocol. Only HTTP, HTTPS, FTP protocols are allowed on this field.

This new constraint was added on the Update Rollup 12 for Microsoft Dynamics CRM. A way to overcome this is to just to set the URL to this:

http://about:blank


This allows the validation of the form to pass, and it set the URL to a blank page as well.