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

Tuesday, November 2, 2010

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

Function returns all the roles of the Current Logged-In User.

function GetCurrentUserRoles()
{
 var xml = "" +
 "" +
 "<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() +
 " <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);
}

Function which calls the above function and search for indivisual role:

function UserHasRole(roleName)
{
 //get Current User Roles, oXml is an object
 var oXml = GetCurrentUserRoles();
 if(oXml != null)
 {
  //select the node text
  var roles = oXml.selectNodes("//BusinessEntity/q1:name");
  if(roles != null)
  {
   for( i = 0; i < roles.length; i++)
   {
    if(roles[i].text == roleName)
    {
     //return true if user has this role
     return true;
    }
   }
  }
 }
 //otherwise return false
 return false;                                              
}

How to use UserHasRole function:

if(UserHasRole(“System Administrator”) == true)
{
           alert(‘User has admin role’);
}
else
{
           alert(‘User does not have admin role’);
}

Friday, August 6, 2010

Hiding Navigation Links & Controls in CRM

Hiding Navigation Links & Controls in CRM

Quick JavaScript solution for hiding both controls on a CRM page and links in the left-hand navigation page on an Entity Form.

The code below provides three functions that can be used to show/hide specific items on a CRM form:

var HIDE = 'none';
var SHOW = 'block';

// Function to show/hide CRM controls on a CRM form
// such as text boxes, lookups, pick-lists etc.
function SetCrmControlVisible(elementName, visibility)
{
SetElementVisible(elementName + '_c', visibility);
SetElementVisible(elementName + '_d', visibility);
}

// Fuction to show/hide specific elements on a CRM form
// such as the left-hand link items (More Addresses, Workflows
// and even custom ISV links
function SetElementVisible(elementId, visibility)
{
var elem = document.getElementById(elementId);
if (elem != null)
{
elem.style.display = visibility;
}
}

// Function to show/hide Navigation "Sections" in the left-hand
// links (Such as Sales, Marketing and Service)
function SetParentElementVisible(elementId, visibility)
{
var elem = document.getElementById(elementId);
if (elem != null && elem.parentElement != null)
{
elem.parentElement.style.display = visibility;
}
}

Add the above code to the Entity OnLoad (and ensure that you enable the event) and after that you are ready to show/hide elements on the CRM form.

By using the IE Developer toolbar, you can retrieve the id of the element to be hidden (note in IE 8 this come built in and is not a seperate download). Open an entity record (in this case a Contact), press CTRL+N to open it in a new window, and then select IE Developer Toolbar.

SetElementVisible('navOpps', HIDE);

To hide the entire “Sales” section from the left-hand navigation pane, I can once again find the id, but this time is of the parent item which would result in the following call to hide it:

SetParentElementVisible('_NA_SFA', HIDE);

Lastly, to hide an element on the form such as the “CRMAttribute” field
      
      SetCrmControlVisible('CRMAttribute', HIDE);

Thursday, August 5, 2010

IntelliSense for Sitemap and ISV.Config Editing in Visual Studio

IntelliSense for Sitemap and ISV.Config Editing in Visual Studio

In CRM we needs to edit Sitemap & ISV.config files most often.
To edit these files easily, visual studio providing intelligence for these XML files.
We have to configure intelligence for the visual studio......


Download the CRM 4.0 SDK. The files you will need are located in the schemas folder of the SDK.
Open the xml(sitemap/isv.config) file in Visual Studio.
Press F4 to bring up the properties dialog.



Copy the following lines into the Schemas value. Keep all as one line and be sure to update your paths to where your files are located:
"D:\CRMSDK 4.0\server\schemas\_root\SiteMapType.xsd"
"D:\CRMSDK 4.0\server\schemas\_resources\customizations.xsd"
"D:\CRMSDK 4.0\server\schemas\_resources\isv.config.xsd"


Now, when you start to edit your file, you will see the IntelliSense for your nodes and any malformed nodes will be highlighted.


Enjoy Editing Sitemap & ISV.config files using intelligence....

Tuesday, August 3, 2010

Set Requirement Level for CRM Attributes at Run-time using Java script

Set Requirement Level for CRM Attributes at Run-time using Java script


// Set field to not required
crmForm.SetFieldReqLevel("fieldname", 0);

// Set field to business required
crmForm.SetFieldReqLevel("fieldname", 1);

// Set field to business recommended
crmForm.SetFieldReqLevel("fieldname", 2);