Hoje mostrarei como consulta se o usuário que esta acessando o Dynamics CRM faz parte de um time.

Abaixo segue o código que realiza  a procura:


//Executa o WhoAmIRequest
function GetCurrentUserInfo() {
var SERVER_URL = location.protocol + "//" + location.host;
var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
xmlhttp.open("POST", SERVER_URL + "/mscrmservices/2007/crmservice.asmx", false);
xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlhttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute");

var soapBody = "<soap:Body>" +
"<Execute xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
"<Request xsi:type='WhoAmIRequest' />" +
"</Execute></soap:Body>";

var soapXml = "<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'>";

soapXml += GenerateAuthenticationHeader();
soapXml += soapBody;
soapXml += "</soap:Envelope>";

xmlhttp.send(soapXml);
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(xmlhttp.responseXML.xml);

var usrid = xmlDoc.getElementsByTagName("UserId")[0].childNodes[0].nodeValue;
var buid = xmlDoc.getElementsByTagName("BusinessUnitId")[0].childNodes[0].nodeValue;
var orgid = xmlDoc.getElementsByTagName("OrganizationId")[0].childNodes[0].nodeValue;

var returnValue = [{ userid: usrid, businessunitid: buid, organizationid: orgid}];

return returnValue;

}

/*
Método repassa o ID do usuario atual executado pelo GetCurrentUserInfo().
Busca todos os times do usuário e valida se pertence pelo nome do time
@teamName : Nome do time no qual quer realizar a consulta

*/
function IsUserInTeam(teamName) {
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\">" +
"  <soap:Header>" +
"    <CrmAuthenticationToken xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
"      <AuthenticationType xmlns=\"http://schemas.microsoft.com/crm/2007/CoreTypes\">0</AuthenticationType>" +
"      <OrganizationName xmlns=\"http://schemas.microsoft.com/crm/2007/CoreTypes\">" + ORG_UNIQUE_NAME + "</OrganizationName>" +
"      <CallerId xmlns=\"http://schemas.microsoft.com/crm/2007/CoreTypes\">00000000-0000-0000-0000-000000000000</CallerId>" +
"    </CrmAuthenticationToken>" +
"  </soap:Header>" +
"  <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>teamid</q1:Attribute>" +
"            <q1:Attribute>name</q1:Attribute>" +
"          </q1:Attributes>" +
"        </q1:ColumnSet>" +
"        <q1:Distinct>false</q1:Distinct>" +
"        <q1:PageInfo>" +
"          <q1:PageNumber>1</q1:PageNumber>" +
"          <q1:Count>50</q1:Count>" +
"        </q1:PageInfo>" +
"        <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>Natural</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>" +
"                    <q1:Value xmlns:q2=\"http://microsoft.com/wsdl/types/\" xsi:type=\"q2:guid\">" + GetCurrentUserInfo()[0].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;

//utilizo o JQUERY para localizar os valores no XML de retorno
var teams = $(resultXml.xml).find("name");

if (teams.length > 0) {
for (var i = 0; i < teams.length; i++) {
if (teams[i].outerText.toLowerCase() == teamName.toLowerCase())
return true;
}
}

return false;
}

Agora, basta chamar no formulário:

IsUserInTeam("System Administrator");

Leave a Reply

Your email address will not be published. Required fields are marked *

Protected by WP Anti Spam