Home > Extensions > Web Resources > Scripts > 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>" +
	" &lt;fetch mapping='logical' count='1'&gt;" +
	" &lt;entity name='businessunit'&gt;" +
	" &lt;attribute name='name' /&gt;" +
	" &lt;filter&gt;" +
	" &lt;condition attribute='businessunitid' operator='eq-businessid' /&gt;" +
	" &lt;/filter&gt;" +
	" &lt;/entity&gt;" +
	" &lt;/fetch&gt;" +
	"</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('&lt;', '< ');
    resultSet.replace('&gt;', '>');

    var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    oXmlDoc.async = false;
    oXmlDoc.loadXML(resultSet);

	var result = oXmlDoc.getElementsByTagName('name');  

	return result[0].text;
}

12 Responses to “Get Current User’s Business Unit for Hiding Fields”

Comments (11) Pingbacks (1)
  1. 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…

  2. this script gives all Biz Units stored in system, not for current user.
    Can anybody suggest any way to get current user BU?

  3. YES! Now it works!
    Thanks Carlton for your efforts!

  4. Hello

    Thank you for this great example! It was really helpful.

    Greets
    Lukas

  5. 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

  6. You could do something like:

    if (UserHasBusinessUnit(['BU1'])) {
    //Hide the section…
    crmForm.all.new_fieldinsection_c.parentElement.parentElement.style.display=’none’;
    }

  7. 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.

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© 2011 MSCRM Blogger

This site does not necessarily represent the views of Microsoft. The material posted on this blog are provided AS-IS, and you are to use the content at your own risk. Some of the code and information on this site may use unsupported methods. If you experience any problems using the information provided through this website, please post a comment and we will do our best to assist you, however, we can in no way guarantee support or resolution.

Microsoft Dynamics®, Silverlight®, Visual Studio®, and the corresponding logos are a registered trademarks owned by Microsoft Corporation. MSCRM Blogger has made every effort to supply trademark information about company names, products, and services mentioned on this blog. All third party trademarks are the property of their respective owners. Any rights not expressly granted herein are reserved.