Thursday, April 12, 2012

How to modify non-customizable entity in MS Dynamics CRM 4.0

In MS CRM 4.0 SQL database there is a table containing settings for all entities present in the system and it is called… Entity. In this table there is a column named IsCustomizable and this is set to 0 (zero) for the PriceList entity.

1 Open SQL Server Management Studio
2 Connect to MSCRM database (not the config one, but the real one with MSCRM tables)
3 Run following update command:

UPDATE ENTITY
SET ISCUSTOMIZABLE = 1
WHERE NAME = 'NonCustomizedEntityname'

Wednesday, April 11, 2012

How to find roles of the logged-in user in MS CRM 2011 using JavaScript

MS CRM 2011 provide one method to find all the roles of logged in user, method is:
Xrm.Page.context.getUserRoles()
but it returns only guid's of all the assigned roles no role name but according my requirement I will show/hide some tabs based on role.
We can do it with two ways:

1) using XML Http request

function OnLoad() {
if (UserHasRole("Customer Service Representative") || UserHasRole("Service Manager")) {
Xrm.Page.ui.tabs.get('details').setVisible(false);
Xrm.Page.ui.tabs.get('administration').setVisible(false);
Xrm.Page.ui.tabs.get('contacts').setVisible(false);
} else {
Xrm.Page.ui.tabs.get('details').setVisible(true);
Xrm.Page.ui.tabs.get('administration').setVisible(true);
Xrm.Page.ui.tabs.get('contacts').setVisible(true);
}
}

function UserHasRole(roleName) {
var serverUrl = Xrm.Page.context.getServerUrl();
var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/";

oDataEndpointUrl += "RoleSet?$top=1&$filter=Name eq '" + roleName + "'";

var service = GetRequestObject();

if (service != null) {
service.open("GET", oDataEndpointUrl, false);
service.setRequestHeader("X-Requested-Width", "XMLHttpRequest");
service.setRequestHeader("Accept", "application/json, text/javascript, */*");
service.send(null);

var requestResults = eval('(' + service.responseText + ')').d;

if (requestResults != null && requestResults.results.length == 1) {
var role = requestResults.results[0];

var id = role.RoleId;

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

for (var i = 0; i < currentUserRoles.length; i++) {
var userRole = currentUserRoles[i];
if (GuidsAreEqual(userRole, id)) {
return true;
}
}
}
}

return false;
}

function GetRequestObject() {
if (window.XMLHttpRequest) {
return new window.XMLHttpRequest;
} else {
try {
return new ActiveXObject("MSXML2.XMLHTTP.3.0");
} catch (ex) {
return null;
}
}
}

function GuidsAreEqual(guid1, guid2) {
var isEqual = false;

if (guid1 == null || guid2 == null) {
isEqual = false;
} else {
isEqual = guid1.replace(/[{}]/g, "").toLowerCase() == guid2.replace(/[{}]/g, "").toLowerCase();
}

return isEqual;
}

2) By Using Soap request

function OnLoad_ShowHideTabRoleWise() {
if (UserHasRoleByRoleId("Customer Service Representative") || UserHasRoleByRoleId("Service Manager")) {
//hide the tabs
Xrm.Page.ui.tabs.get('details').setVisible(false);
Xrm.Page.ui.tabs.get('administration').setVisible(false);
Xrm.Page.ui.tabs.get('contacts').setVisible(false);
} else {
//show the tabs
Xrm.Page.ui.tabs.get('details').setVisible(true);
Xrm.Page.ui.tabs.get('administration').setVisible(true);
Xrm.Page.ui.tabs.get('contacts').setVisible(true);
}
}

function UserHasRoleByRoleId(roleName) {
//return the list of logged in user all roles guid
var currentUserRoles = Xrm.Page.context.getUserRoles();

for (var i = 0; i < currentUserRoles.length; i++) {
var userRole = currentUserRoles[i];
//compare two roles
if (GuidsAreEqual(WSRetrieveMultiple("role","name","roleid",userRole), roleName)) {
return true;
}
}
return false;
}

function GuidsAreEqual(guid1, guid2) {
var isEqual = false;

if (guid1 == null || guid2 == null) {
isEqual = false;
} else {
isEqual = guid1.replace(/[{}]/g, "").toLowerCase() == guid2.replace(/[{}]/g, "").toLowerCase();
}

return isEqual;
}


function WSRetrieveMultiple(crmEntity, resultAttribute, queryAttribute, queryValue) {
var resultXml;
var oXMLSoapMsg = "<?xml version='1.0' encoding='utf-8'?>"+
"<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'"+
" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"+
" xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"+
GenerateAuthenticationHeader().toString()+
"<soap:Body>"+
"<RetrieveMultiple xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
"<query xmlns:q1='http://schemas.microsoft.com/crm/2006/Query'"+
" xsi:type='q1:QueryExpression'>"+
"<q1:EntityName>"+crmEntity+"</q1:EntityName>"+
"<q1:ColumnSet xsi:type='q1:ColumnSet'>"+
"<q1:Attributes>"+
"<q1:Attribute>"+resultAttribute+"</q1:Attribute>"+
"</q1:Attributes>"+
"</q1:ColumnSet>"+
"<q1:Distinct>false</q1:Distinct>"+
"<q1:Criteria>"+
"<q1:FilterOperator>And</q1:FilterOperator>"+
"<q1:Conditions>"+
"<q1:Condition>"+
"<q1:AttributeName>"+queryAttribute+"</q1:AttributeName>"+
"<q1:Operator>Like</q1:Operator>"+
"<q1:Values>"+
"<q1:Value xsi:type='xsd:string'>"+queryValue+"</q1:Value>"+
"</q1:Values>"+
"</q1:Condition>"+
"</q1:Conditions>"+
"</q1:Criteria>"+
"</query>"+
"</RetrieveMultiple>"+
"</soap:Body>"+
"</soap:Envelope>";
var oXMLHTTPReq = new ActiveXObject("Msxml2.XMLHTTP");
oXMLHTTPReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
oXMLHTTPReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
oXMLHTTPReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
oXMLHTTPReq.setRequestHeader("Content-Length", oXMLSoapMsg.length);
oXMLHTTPReq.send(oXMLSoapMsg);
resultXml = oXMLHTTPReq.responseXML;

var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0){
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
alert(msg);
}
else{
var results = resultXml.getElementsByTagName('BusinessEntity');
if (results.length != 0)
{
return resultXml.selectNodes("//BusinessEntity/q1:name")[0].text;
}
}
}

MS CRM 2011 Javascript Syntax

Xrm.Page
Xrm.Page is the JavaScript namespace where you can access CRM form UI Elements and fields.

Returns an attribute on the form:
Xrm.Page.getAttribute("field");

Returns a control on the form:
Xrm.Page.getControl("field");

Xrm.Page.ui
This namespace contains functions for manipulation of the form UI. This includes tabs, sections and fields.

Returns type of form like update, create, etc.:
Xrm.Page.ui.getFormType();

Returns width of view in pixels:
Xrm.Page.ui.getViewPortWidth();

Returns current active control:
Xrm.Page.ui.getCurrentControl();

Returns a control on the form:
Xrm.Page.ui.controls.get("field");

Returns the property of the tab:
Xrm.Page.ui.tabs.get("tabName");

Xrm.Page.context
The context object provides methods to retrieve information specific to an organization, a user, or parameters that were passed to the form in a query string.

Return the GUID of the current user:
Xrm.Page.context.getUserId();

Returns the current user role/roles.:
Xrm.Page.context.getUserRoles();

Returns whether a user is in the Outlook client.:
Xrm.Page.context.isOutlookClient();

To check the outlook client is online:
Xrm.Page.context.isOutlookOnline();

Returns the unique org name for the current active organization:
Xrm.Page.context.getOrgUniqueName();

Returns the url of the server.:
Xrm.Page.context.getServerUrl();

Returns the authentication header required to make SOAP calls.:
Xrm.Page.context.getAuthenticationHeader();

Returns the current Outlook theme chosen by the user
Xrm.Page.context.getCurrentTheme();

Returns the LCID value for the base language of the
Organization
Xrm.Page.context.getOrgLcid();

Returns an array of key value pairs representing the query
string arguments that were passed to the page
Xrm.Page.context.getQueryStringParameters();

Returns the LCID value that the user selected as their
preferred language
Xrm.Page.context.getUserLcid();

Prepends the organization name to the specified path
Xrm.Page.context.prependOrgName();

Xrm.Page.data.entity
Provides methods to retrieve information specific to the record displayed on the page, the save method and a collection of all the attributes included in the form

Returns the current entitys GUID:
Xrm.Page.data.entity.getId();

Returns the current entities schema/logical name.:
Xrm.Page.data.entity.getEntityName();

Forces a manual save of the entity:
Xrm.Page.data.entity.save();
http://www.blogger.com/img/blank.gif
Determines whether or not the form is dirty,
i.e. if there are any unsaved changes.:
Xrm.Page.data.entity.getIsDirty();

Returns the property of the tab:
Xrm.Page.ui.tabs.get("tabName");

For more and other information please browse the link

Monday, April 2, 2012

How to hide show Tab's based on User Roles in MS CRM 2011

Use the below lines of code to achieve your goal.

function OnLoad() {
if (UserHasRole("Customer Service") || UserHasRole("Service Manager")) {
Xrm.Page.ui.tabs.get('details').setVisible(false);
} else {
Xrm.Page.ui.tabs.get('details').setVisible(true);
}
}


function UserHasRole(roleName) {
var serverUrl = Xrm.Page.context.getServerUrl();
var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/";

var org = Xrm.Page.context.getOrgUniqueName()
var oDataEndpointUrl = "/" + org + "/XRMServices/2011/OrganizationData.svc/";

oDataEndpointUrl += "RoleSet?$top=1&$filter=Name eq '" + roleName + "'";

var service = GetRequestObject();

if (service != null) {
service.open("GET", oDataEndpointUrl, false);
service.setRequestHeader("X-Requested-Width", "XMLHttpRequest");
service.setRequestHeader("Accept", "application/json, text/javascript, */*");
service.send(null);

var requestResults = eval('(' + service.responseText + ')').d;

if (requestResults != null && requestResults.results.length == 1) {
var role = requestResults.results[0];

var id = role.RoleId;

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

for (var i = 0; i < currentUserRoles.length; i++) {
var userRole = currentUserRoles[i];
if (GuidsAreEqual(userRole, id)) {
return true;
}
}
}
}

return false;
}

function GetRequestObject() {
if (window.XMLHttpRequest) {
return new window.XMLHttpRequest;
} else {
try {
return new ActiveXObject("MSXML2.XMLHTTP.3.0");
} catch (ex) {
return null;
}
}
}

function GuidsAreEqual(guid1, guid2) {
var isEqual = false;

if (guid1 == null || guid2 == null) {
isEqual = false;
} else {
isEqual = guid1.replace(/[{}]/g, "").toLowerCase() == guid2.replace(/[{}]/g, "").toLowerCase();
}

return isEqual;
}

Split the String values with a special character in MS Flow to convert this into Array

 Many times we have a requirement to prepare the Mailing address for some of the documents, suppose there are Address Line1, Address Line2, ...