Get Current User’s Business Unit for Hiding Fields
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;
}
10 Responses to “Get Current User’s Business Unit for Hiding Fields”
Comment from Carlton Colter
Time September 30, 2009 at 9:11 pm
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…
Pingback from Set a default email template for new emails on cases. | MSCRM Blogger
Time October 2, 2009 at 5:32 pm
[...] Get Current User’s Business Unit for Hiding Fields [...]
Comment from Pavel
Time October 27, 2009 at 10:32 am
this script gives all Biz Units stored in system, not for current user.
Can anybody suggest any way to get current user BU?
Comment from Carlton Colter
Time October 28, 2009 at 5:56 am
I have a fetchxml that can get the business unit, so I will update the code to use that method.
Comment from Carlton Colter
Time October 28, 2009 at 6:11 am
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.
Comment from Pavel
Time October 29, 2009 at 2:05 am
YES! Now it works!
Thanks Carlton for your efforts!
Comment from Lukas
Time February 2, 2010 at 8:39 am
Hello
Thank you for this great example! It was really helpful.
Greets
Lukas
Comment from sunny
Time July 19, 2010 at 8:37 am
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
Comment from Carlton Colter
Time August 11, 2010 at 11:51 am
You could do something like:
if (UserHasBusinessUnit(['BU1'])) {
//Hide the section…
crmForm.all.new_fieldinsection_c.parentElement.parentElement.style.display=’none’;
}
Comment from Jeroen van Heel
Time September 30, 2009 at 9:21 am
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’);