In my previous post about hiding fields and tabs using roles, I outlined how use roles to hide fields, etc. Here is a modification of the method to hide sections by business unit.
The syntax is UserHasBusinessUnit(['BU1','BU2','BU3']);
function UserHasBusinessUnit(businessUnits)
{
var mybu = GetMyBusinessUnit();
for (j = 0; j < businessUnits.length; j++)
{
// If there is a match, return true, found
if (mybu == businessUnits[j]) return true;
}
//otherwise return false
return false;
}
function GetMyBusinessUnit() {
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'>" +
GenerateAuthenticationHeader() +
"<soap:Body>" +
"<Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
"<fetchXml>" +
" <fetch mapping='logical' count='1'>" +
" <entity name='businessunit'>" +
" <attribute name='name' />" +
" <filter>" +
" <condition attribute='businessunitid' operator='eq-businessid' />" +
" </filter>" +
" </entity>" +
" </fetch>" +
"</fetchXml>" +
"</Fetch>" +
"</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/Fetch");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
var resultXml = xmlHttpRequest.responseXML;
var resultSet = resultXml.text;
resultSet.replace('<', '< ');
resultSet.replace('>', '>');
var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
oXmlDoc.async = false;
oXmlDoc.loadXML(resultSet);
var result = oXmlDoc.getElementsByTagName('name');
return result[0].text;
}


Hey Carlton,
We used almost the same request, mine looks like this.
var xml = “” +
“” +
“” +
GenerateAuthenticationHeader() +
” ” +
” ” +
” ” +
” systemuser” +
” ” +
” ” +
” businessunitid” +
” ” +
” ” +
” false” +
” ” +
” And” +
” ” +
” ” +
” systemuserid” +
” Equal” +
” ” +
” “+crmForm.all.ownerid.DataValue[0].id+”" +
” ” +
” ” +
” ” +
” ” +
” ” +
” ” +
” ” +
“” +
“”;
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;
var businessUnitName = resultXml.getElementsByTagName(“q1:businessunitid”)[0].getAttribute(‘name’);
Thanks for sharing Jeroen. I am really interested in finding alternative methods. It looks like you are matching on the owner. I assume you were just using this on the creation of an entity…
this script gives all Biz Units stored in system, not for current user.
Can anybody suggest any way to get current user BU?
I have a fetchxml that can get the business unit, so I will update the code to use that method.
Ok. The updated code should work. Please let me know if it doesn’t. I tested the GetMyBusinessUnit proc and it returns the name of the user’s business unit.
YES! Now it works!
Thanks Carlton for your efforts!
Hello
Thank you for this great example! It was really helpful.
Greets
Lukas
Hi Carlton,
I want a section to be hidden from a specific users belonging to BU1. Where shld I make the modifications in your code to reproduce the workaround?
Regards
Sunny
You could do something like:
if (UserHasBusinessUnit(['BU1'])) {
//Hide the section…
crmForm.all.new_fieldinsection_c.parentElement.parentElement.style.display=’none’;
}
Hi Carlton,
I need to validate phone number fields according to logged users business units.(EX: USA(123-123-123),UK(1233-123-123) Ind(12344 12345)etc. how can I use your code to format phone number fields.
I would use this: http://channel9.msdn.com/posts/Dynamics-CRM-2011-Validation-Framework-Setup-and-Configuration-Walkthrough. It was written by one of my coworkers and I like it.