Thursday, May 23, 2013

Get Current User's Teams in crm 2011 using javascript.


function GetCurrentUserTeams() {

  var user = Xrm.Page.context.getUserId();
  var userId = user.substring(1,37);

    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>" +
    " <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>team</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>teamid</q1:LinkFromAttributeName>" +
    " <q1:LinkFromEntityName>team</q1:LinkFromEntityName>" +
    " <q1:LinkToEntityName>teammembership</q1:LinkToEntityName>" +
    " <q1:LinkToAttributeName>teamid</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>Equal</q1:Operator>" +
    "<q1:Values>" +
    //code to get the owner
    "<q1:Value xsi:type=\"xsd:string\">" + userId + "</q1:Value>" +
    "</q1:Values>" +

    " </q1:Condition>" +
    " </q1:Conditions>" +
    " </q1:LinkCriteria>" +
    " </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;
    //alert(resultXml.xml);

    // Save all entity nodes in an array.
    var entityNodes = resultXml.selectNodes("//RetrieveMultipleResult/BusinessEntities/BusinessEntity");

    var teamnames = new Array();
    var teamids = new Array();

    for (var i = 0; i < entityNodes.length; i++) {

        var entityNode = entityNodes[i];
        var teamidNode = entityNode.selectSingleNode("q1:teamid");
        var teamNode = entityNode.selectSingleNode("q1:name");
        var teamid = (teamidNode == null) ? null : teamidNode.text;
        var team = (teamNode == null) ? null : teamNode.text;

        teamnames[i] = team;
        teamids[i] = teamid;
    }

    alert(teamnames);
}


Above script will not work in CRM 2013. For CRM 2013,the script is as below

function GetCurrentUserTeams()
{
    var teamName="Communities Team";
if (teamName != null && teamName != "") {
// build endpoint URL
var serverUrl = Xrm.Page.context.getServerUrl();
var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/";
// query to get the teams that match the name
oDataEndpointUrl += "TeamSet?$select=Name,TeamId&$filter=Name eq '" + teamName + "'";
var service = GetRequestObject();
if (service != null) {
// execute the request
service.open("GET", oDataEndpointUrl, false);
service.setRequestHeader("X-Requested-Width", "XMLHttpRequest");
service.setRequestHeader("Accept", "application/json,text/javascript, */*");
service.send(null);
// parse the results
var requestResults = eval('(' + service.responseText + ')').d;
if (requestResults != null && requestResults.results.length > 0) {
var teamCounter;
// iterate through all of the matching teams, checking to see if the current user has a membership
for (teamCounter = 0; teamCounter < requestResults.results.length; teamCounter++) {
var team = requestResults.results[teamCounter];
var teamId = team.TeamId;
// get current user teams
var currentUserTeams = getUserTeams(teamId);
// Check whether current user teams matches the target team
if (currentUserTeams != null) {
for (var i = 0; i < currentUserTeams.length; i++) {
var userTeam = currentUserTeams[i];
// check to see if the team guid matches the user team membership id
if (GuidsAreEqual(userTeam.TeamId, teamId)) {
return true;
}
}
}
else {
return false;
}
}
}
else {
alert("Team with name '" + teamName + "' not found");
return false;
}
return false;
}
 }
 else {
 alert("No team name passed");
 return false;
 }
}
function getUserTeams(teamToCheckId) {
 // gets the current users team membership
 var userId = Xrm.Page.context.getUserId().substr(1, 36);
 var serverUrl = Xrm.Page.context.getServerUrl();
 var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/";
 oDataEndpointUrl += "TeamMembershipSet?$filter=SystemUserId eq guid' " + userId + " ' and TeamId eq guid' " + teamToCheckId + " '";
var service = GetRequestObject();
if (service != null) {
 service.open("GET", oDataEndpointUrl, false);
 service.setRequestHeader("X-Requested-Width", "XMLHttpRequest");
 service.setRequestHeader("Accept", "application/json,text/javascript, */*");
 service.send(null);
var requestResults = eval('(' + service.responseText + ')').d;
if (requestResults != null && requestResults.results.length > 0) {
 return requestResults.results;
 }
 }
}
function GetRequestObject() {
if (window.XMLHttpRequest) {
 return new window.XMLHttpRequest;
 } else {
 try {
 return new ActiveXObject("MSXML2.XMLHTTP.3.0");
 } catch (ex) {
 return null;
 }
 }
}
function GuidsAreEqual(guid1, guid2) {
 // compares two guids
 var isEqual = false;
 if (guid1 == null || guid2 == null) {
 isEqual = false;
 } else {
 isEqual = (guid1.replace(/[{}]/g, "").toLowerCase() == guid2.replace(/[{}]/g, "").toLowerCase());
 }
 return isEqual;
}

1 comment: