How to retrieve Business Unit Id of user using javascript odata in MS CRM 2011?
-
Wednesday, November 02, 2011 6:09 AM
How to retrieve Business Unit Id of user using javascript odata in MS CRM 2011?
Please explain in detail
All Replies
-
Wednesday, November 02, 2011 6:25 AM
The following code is for retrieve all users and alert every user's businessunit's id and name
function getRESTUrl() { //...return the Organization Data Service Url, such as "http://<ServerName>:<Port>/<OrgName>/XrmServices/2011/OrganizationData.svc" } function loadUsersRequest() { var retrieveusersReq = new XMLHttpRequest(); retrieveusersReq.open("GET", getRESTUrl() + "/SystemUserSet?$select=FullName,SystemUserId&$filter=FullName ne 'INTEGRATION' and FullName ne 'SYSTEM'", true); retrieveusersReq.setRequestHeader("Accept", "application/json"); retrieveusersReq.setRequestHeader("Content-Type", "application/json; charset=utf-8"); retrieveusersReq.onreadystatechange = function () { loadUsersResponse(retrieveusersReq); }; retrieveusersReq.send(); } function loadUsersResponse(retrieveusersReq) { if (retrieveusersReq.readyState == 4) { if (retrieveusersReq.status == 200) { var retrievedUsers = JSON.parse(retrieveusersReq.responseText).d.results; for (var i = 0; i < retrievedUsers.length; i++) { var user = retrievedUsers[i]; alert(user.BusinessUnitId.Id); alert(user.BusinessUnitId.Name); } } else { errorHandler(retrieveusersReq); } } } function errorHandler(retrieveusersReq) { //show error message to end user }
daemon lin- Proposed As Answer by Makarand Keer Wednesday, November 02, 2011 6:35 AM
-
Wednesday, June 13, 2012 2:59 PM
Hi,
I tried this code by calling the loadUsersRequest() function at the onLoad event of a form, but nothing happens.
I also added the correct URL in the getRESTUrl() function. But the function does nothing no alerts or errors.
MCC, MCT, MCP, MCTS
If you find this post helpful then please "Vote as Helpful". If I helped you with an answer to a question then please "Mark As Answer". -
Friday, March 29, 2013 10:24 AM
function getSvrUrl() {
var serverUrl = context.getServerUrl();
if (serverUrl.match(/\/$/))
serverUrl = serverUrl.substring(0, serverUrl.length - 1);
return serverUrl;
}
function getuser() {
var retrieveReq = new XMLHttpRequest();
retrieveReq.open("GET", ODataPath + "/SystemUserSet?$select=FullName" ", true);
retrieveReq.setRequestHeader("Accept", "application/json");
retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
retrieveReq.onreadystatechange = function () {
retrievegetuser(this);
};
retrieveReq.send();}
function retrievegetuser(retrieveReq) {
if (retrieveReq.readyState == 4 /* complete */) {
if (retrieveReq.status == 200) //Success
{
var responseData = JSON.parse(retrieveReq.responseText).d;
if (responseData != null && responseData.results != null && responseData.results.length > 0){alert(responseData.results[0].FullName);
}
} }}
var JSON; if (!JSON) JSON = {}; (function () {
"use strict"; function c(a) { return a < 10 ? "0" + a : a } if (typeof Date.prototype.toJSON !== "function") {
Date.prototype.toJSON = function () {
return isFinite(this.valueOf()) ? this.getUTCFullYear() + "-" + c(this.getUTCMonth() + 1) + "-" + c(this.getUTCDate())
+ "T" + c(this.getUTCHours()) + ":" + c(this.getUTCMinutes()) + ":" + c(this.getUTCSeconds())
+ "Z" : null
}; String.prototype.toJSON = Number.prototype.toJSON = Boolean.prototype.toJSON = function () { return this.valueOf() }
}
var h = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
f = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, a, d, i =
{ "\b": "\\b", "\t": "\\t", "\n": "\\n", "\f": "\\f", "\r": "\\r", '"': '\\"', "\\": "\\\\" }, b; function g(a) {
f.lastIndex = 0; return f.test(a) ? '"'
+ a.replace(f, function (a) {
var b = i[a];
return typeof b === "string" ? b : "\\u" + ("0000" + a.charCodeAt(0).toString(16)).slice(-4)
}) + '"' : '"' + a + '"'
}
function e(m, n) {
var h, j, i, k, l = a, f, c = n[m]; if (c && typeof c === "object" && typeof c.toJSON === "function") c = c.toJSON(m);
if (typeof b === "function") c = b.call(n, m, c); switch (typeof c) {
case "string": return g(c); case "number": return isFinite(c) ? String(c) : "null";
case "boolean": case "null": return String(c); case "object": if (!c) return "null"; a += d; f = [];
if (Object.prototype.toString.apply(c) === "[object Array]") {
k = c.length; for (h = 0; h < k; h += 1) f[h] = e(h, c) || "null";
i = f.length === 0 ? "[]" : a ? "[\n" + a + f.join(",\n" + a) + "\n" + l + "]" : "[" + f.join(",") + "]"; a = l; return i
}
if (b && typeof b === "object") {
k = b.length; for (h = 0; h < k; h += 1) if (typeof b[h] === "string") {
j = b[h]; i = e(j, c);
i && f.push(g(j) + (a ? ": " : ":") + i)
}
} else for (j in c) if (Object.prototype.hasOwnProperty.call(c, j)) {
i = e(j, c);
i && f.push(g(j) + (a ? ": " : ":") + i)
} i = f.length === 0 ? "{}" : a ? "{\n" + a + f.join(",\n" + a) + "\n" + l + "}" : "{" + f.join(",") + "}";
a = l; return i
}
} if (typeof JSON.stringify !== "function") JSON.stringify = function (h, c, f) {
var g; a = ""; d = "";
if (typeof f === "number") for (g = 0; g < f; g += 1) d += " "; else if (typeof f === "string") d = f; b = c;
if (c && typeof c !== "function" && (typeof c !== "object" || typeof c.length !== "number")) throw new Error("JSON.stringify");
return e("", { "": h })
}; if (typeof JSON.parse !== "function") JSON.parse = function (a, c) {
var b; function d(f, g) {
var b, e, a = f[g];
if (a && typeof a === "object") for (b in a) if (Object.prototype.hasOwnProperty.call(a, b)) {
e = d(a, b); if (e !== undefined) a[b] = e;
else delete a[b]
} return c.call(f, g, a)
} a = String(a); h.lastIndex = 0;
if (h.test(a)) a = a.replace(h, function (a)
{ return "\\u" + ("0000" + a.charCodeAt(0).toString(16)).slice(-4) });
if (/^[\],:{}\s]*$/.test(a.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]").
replace(/(?:^|:|,)(?:\s*\[)+/g, ""))) {
b = eval("(" + a + ")");
return typeof c === "function" ? d({ "": b }, "") : b
} throw new SyntaxError("JSON.parse");
}
})()
Hope this helps. ----------------------------------------------------------------------- Santosh Bhagat If this post answers your question, please click "Mark As Answer" on the post and "Vote as Helpful"
- Proposed As Answer by Santosh Bhagat_2012 Friday, March 29, 2013 10:24 AM
- Edited by Santosh Bhagat_2012 Friday, March 29, 2013 10:25 AM