Friday, July 29, 2011

Code snippets for future Reference

Hi,
Below are some of the code snippets for CRM 2011, i wanted to write in my blog for future reference or if someone needs they can also refer.

Below are some of the commonly used JavaScript code snippets. I have taken them from this blog

Get the value from a CRM field
var varMyValue = Xrm.Page.getAttribute(“CRMFieldSchemaName”).getValue() ;

Set the value of a CRM field
Xrm.Page.getAttribute(“CRMFieldSchemaName”).setValue(‘My New Value’);

Hide/Show a tab/section
Xrm.Page.ui.tabs.get(5).SetVisible(false);
Xrm.Page.ui.tabs.get(5).SetVisible(true);

Call the onchange event of a field
Xrm.Page.getAttribute(“CRMFieldSchemaName”).fireOnChange();

Get the selected value of picklist
Xrm.Page.getAttribute(“CRMFieldSchemaName”).getSelectedOption().text;

Set the requirement level
Xrm.Page.getAttribute(“CRMFieldSchemaName”).setRequiredLevel(“none”);
Xrm.Page.getAttribute(“CRMFieldSchemaName”).setRequiredLevel(“required”);
Xrm.Page.getAttribute(“CRMFieldSchemaName”).setRequiredLevel(“recommended”);

Set the focus to a field
Xrm.Page.getControl(“CRMFieldSchemaName”).setFocus(true);

Stop an on save event
event.returnValue = false;

Return array of strings of users security role GUIDs:
Xrm.Page.context.getUserRoles()

Hide/Show Tabs and Sections

function setVisibleTabSection(tabname, sectionname, show) {
var tab = Xrm.Page.ui.tabs.get(tabname);
if (tab != null) {
if (sectionname == null)
tab.setVisible(show);
else {
var section = tab.sections.get(sectionname);
if (section != null) {
section.setVisible(show);
if (show)
tab.setVisible(show);
}
}
}
}


Code to check whether the currently logged in User is a System Administrator

if (typeof (SDK) == "undefined")
{ SDK = {}; }
// Create Namespace container for functions in this library;
SDK.ContextSamples = {
isUserSysAdmin: function ()
{
var serverUrl = Xrm.Page.context.getServerUrl();
var query = "/XRMServices/2011/OrganizationData.svc/RoleSet?$top=1&$filter=Name eq

'System Administrator'&$select=RoleId";
var retrieveRoleRequest = new XMLHttpRequest();
retrieveRoleRequest.open("GET", serverUrl + query, true);
retrieveRoleRequest.onreadystatechange = function ()
{
SDK.ContextSamples.retrieveRoleResponse(this);
};
retrieveRoleRequest.send();
},
retrieveRoleResponse: function (retrieveRoleRequest)
{
if (retrieveRoleRequest.readyState == 4)
{
if (retrieveRoleRequest.status == 200)
{
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.6.0")
xmlDoc.async = false;
xmlDoc.loadXML(retrieveRoleRequest.responseText);
var sysAdminRoleId = xmlDoc.selectSingleNode("//*[local-name() =

'RoleId']").text

var currentUserRoles = Xrm.Page.context.getUserRoles();

for (var i = 0; i < currentUserRoles.length; i++) { var userRole = currentUserRoles[i]; if (userRole == sysAdminRoleId) { alert("The current user has the 'System Administrator' role."); return; } } alert("The current user does not have the 'System Administrator' role."); } else { //handle error alert("Error retrieving user Roles"); } } }, };

No comments:

Post a Comment