Tuesday 15 May 2012

Fillter or Act based on User role in Dynamic CRM 2011

situation: if we want filter optional set based on user roles or disable enable a field based on user role
for this we need to write a javascript code as below.


function currentuserroles()
{

   var xml = "" +
  "<?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\">" +
  Xrm.Page.context.getAuthenticationHeader() +
  " <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>role</q1:EntityName>" +
  " <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
  " <q1:Attributes>" +
  " <q1:Attribute>name</q1:Attribute>" +
  " </q1:Attributes>" +
  " </q1:ColumnSet>" +
  " <q1:Distinct>false</q1:Distinct>" +
  " <q1:LinkEntities>" +
  " <q1:LinkEntity>" +
  " <q1:LinkFromAttributeName>roleid</q1:LinkFromAttributeName>" +
  " <q1:LinkFromEntityName>role</q1:LinkFromEntityName>" +
  " <q1:LinkToEntityName>systemuserroles</q1:LinkToEntityName>" +
  " <q1:LinkToAttributeName>roleid</q1:LinkToAttributeName>" +
  " <q1:JoinOperator>Inner</q1:JoinOperator>" +
  " <q1:LinkEntities>" +
  " <q1:LinkEntity>" +
  " <q1:LinkFromAttributeName>systemuserid</q1:LinkFromAttributeName>" +
  " <q1:LinkFromEntityName>systemuserroles</q1:LinkFromEntityName>" +
  " <q1:LinkToEntityName>systemuser</q1:LinkToEntityName>" +
  " <q1:LinkToAttributeName>systemuserid</q1:LinkToAttributeName>" +
  " <q1:JoinOperator>Inner</q1:JoinOperator>" +
  " <q1:LinkCriteria>" +
  " <q1:FilterOperator>And</q1:FilterOperator>" +
  " <q1:Conditions>" +
  " <q1:Condition>" +
  " <q1:AttributeName>systemuserid</q1:AttributeName>" +
  " <q1:Operator>EqualUserId</q1:Operator>" +
  " </q1:Condition>" +
  " </q1:Conditions>" +
  " </q1:LinkCriteria>" +
  " </q1:LinkEntity>" +
  " </q1:LinkEntities>" +
  " </q1:LinkEntity>" +
  " </q1:LinkEntities>" +
  " </query>" +
  " </RetrieveMultiple>" +
  " </soap:Body>" +
  "</soap:Envelope>" +
  "";

  var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

  xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
  xmlHttpRequest.setRequestHeader("SOAPAction"," http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
  xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
  xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
  xmlHttpRequest.send(xml);

  var resultXml = xmlHttpRequest.responseXML;
  //return(resultXml);

//alert(resultXml);

    //get Current User Roles, oXml is an object
    if (resultXml != null) {
        //select the node text
        var roles = resultXml.selectNodes("//BusinessEntity/q1:name");
//alert(roles);
//alert(roles.length);
        if (roles != null) {
            for (i = 0; i < roles.length; i++) {
           var rolename=roles[i].text;
//alert(rolename);
     
       if((rolename=="System Customizer")||(rolename=="System Administrator"))
     {
//Xrm.Page.getControl("tra_typeofcustomer").removeOption(167490004);
Xrm.Page.getControl("statuscode").removeOption(2);
Xrm.Page.getControl("statuscode").removeOption(3);
Xrm.Page.getControl("statuscode").removeOption(4);
     }


      }

        }
    }

}



here we need to change the roles accordingly.  but copy and paste full length of the code.
in bottom we have the options to custamize out code.



No comments:

Post a Comment