Web Services - How Can I Alert The Current System User in CRM 4.0?
-
19. maj 2008 07:12
How would you make the following code below grab the systemuser name using a Web Service and alert it using CRM 4.0? (MORE INFORMATION: The example below works in 3.0 to get the value of the current systemuser and alert it to the screen during an OnSave event)
Your help would be greatly appreciated. Thank you!
var xml = "" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap:Envelope xmlns
oap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
" <soap:Body>" +
" <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\" xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
" <q1:EntityName>systemuser</q1:EntityName>" +
" <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
" <q1:Attributes>" +
" <q1:Attribute>systemuserid</q1:Attribute>" +
" <q1:Attribute>fullname</q1:Attribute>" +
" </q1:Attributes>" +
" </q1:ColumnSet>" +
" <q1
istinct>false</q1
istinct>" +
" <q1:Criteria>" +
" <q1:FilterOperator>And</q1:FilterOperator>" +
" <q1:Conditions>" +
" <q1:Condition>" +
" <q1:AttributeName>systemuserid</q1:AttributeName>" +
" <q1
perator>EqualUserId</q1
perator>" +
" </q1:Condition>" +
" </q1:Conditions>" +
" </q1:Criteria>" +
" </query>" +
" </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 doc = xmlHttpRequest.responseXML;
var user = doc.selectSingleNode("//BusinessEntity");
var userId = user.selectSingleNode("systemuserid").text;
var userName = user.selectSingleNode("fullname").text;
alert(userName);
Alle besvarelser
-
19. maj 2008 08:44Redaktør
Hi,
I think that the problem is that you haven't included the Authentication Header (new in CRM 4.0). Take a look to this section of the SDK wich explains how to use it http://msdn.microsoft.com/en-us/library/cc150838.aspx
Marco
-
19. maj 2008 11:02Redaktør
see this code , it will give you right idea for using authentication token
http://microsoftcrm3.blogspot.com/2008/01/hide-tab-button-base-on-security-role.html
Regards,
Imran
http://microsoftcrm3.blogspot.com
-
19. maj 2008 14:54
Thanks for your post response. However, I've tried a numerous amount of combinations using the authentication header with no sound results
.. May you have another suggestion or am I missing something..? Thank you.- Foreslået som svar af sctodd 17. december 2009 16:21
-
19. maj 2008 15:49
To be more specific on my issue.
I receive the error as follows when I save the form (SOAP request code is placed in the onSave event):
-------------------------------------------------------------------------------
There was an error with this field's customized event.
Field: crmForm
Event: onsave
Error:'null' is null or not an object.
-------------------------------------------------------------------------------
With the code below I recieve an alert error instead of grabbing the systemuser name.
var xml = "" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap:Envelope xmlns
oap=\"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>" +
" <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\" xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
" <q1:EntityName>systemuser</q1:EntityName>" +
" <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
" <q1:Attributes>" +
" <q1:Attribute>systemuserid</q1:Attribute>" +
" <q1:Attribute>fullname</q1:Attribute>" +
" </q1:Attributes>" +
" </q1:ColumnSet>" +
" <q1
istinct>false</q1
istinct>" +
" <q1:Criteria>" +
" <q1:FilterOperator>And</q1:FilterOperator>" +
" <q1:Conditions>" +
" <q1:Condition>" +
" <q1:AttributeName>systemuserid</q1:AttributeName>" +
" <q1
perator>EqualUserId</q1
perator>" +
" </q1:Condition>" +
" </q1:Conditions>" +
" </q1:Criteria>" +
" </query>" +
" </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 doc = xmlHttpRequest.responseXML;
var user = doc.selectSingleNode("//BusinessEntity");
var userId = user.selectSingleNode("systemuserid").text;
var userName = user.selectSingleNode("fullname").text;
alert(userName);Any suggestions????
Thanks!!
-
20. maj 2008 02:23
I discovered the solution! Thank you for all of your help to those who contributed! Secondly, props Michael from Stunnware for a point in the right direction. I hope that somone can benefit from this code!
/*****************************************************************/
/* Program Narrative: This onSave OR onLoad (take your pick) JavaScript will display the current system
/* user and their GUID. I've seen it for CRM 3.0 but never for 4.0. Enjoy!
/* Developer: Jeremiah Jordan Isaacson
/* CRM Version: Titan 4.0
/*****************************************************************/
var xml = "" +
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap:Envelope xmlns
oap=\"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>systemuser</q1:EntityName>" +
" <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
" <q1:Attributes>" +
" <q1:Attribute>systemuserid</q1:Attribute>" +
" <q1:Attribute>fullname</q1:Attribute>" +
" </q1:Attributes>" +
" </q1:ColumnSet>" +
" <q1
istinct>false</q1
istinct>" +
" <q1:Criteria>" +
" <q1:FilterOperator>And</q1:FilterOperator>" +
" <q1:Conditions>" +
" <q1:Condition>" +
" <q1:AttributeName>systemuserid</q1:AttributeName>" +
" <q1
perator>EqualUserId</q1
perator>" +
" </q1:Condition>" +
" </q1:Conditions>" +
" </q1:Criteria>" +
" </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. Each result is returned in a BusinessEntity node.
// All BusinessEntity nodes are contained in a single BusinessEntities node.
// The BusinessEntities node in contained in a RetrieveMultipleResult node
// You could also use the XPath //BusinessEntities/BusinessEntity or //BusinessEntity
// "//" tells the XML parser to find all occurrences in the document starting with the
// supplied path, so it would find a/b/c/BusinessEntity as well as x/BusinessEntity.
var entityNodes = resultXml.selectNodes("//RetrieveMultipleResult/BusinessEntities/BusinessEntity");// Loop through the collection of returned entities.
// Note that the query above limits the result to a single entity, so you will only find one
// node. To be more specific, it could be 0 nodes as well, if you don't have access to the systemuser table
// or your system is empty.
for (var i = 0; i < entityNodes.length; i++) {// Access the current array element
var entityNode = entityNodes
; // Attributes are child nodes of the BusinessEntity node. Use selectSingleNode to return
// the first occurrence of a named node. The selectNodes method we used before returns all
// matching nodes, but as an attribute name only occurs once, selectSingleNode is easier to use.
// Use the same namespace alias (q1) you have specified in the query.
var systemuseridNode = entityNode.selectSingleNode("q1
ystemuserid");
var fullnameNode = entityNode.selectSingleNode("q1:fullname");// Always check for null values. If an attribute is set to null, it is not contained in the
// resulting XML. And accessing systemuseridNode.text when systemuseridNode is null leads to
// a runtime error.
var systemuserid = (systemuseridNode == null) ? null : systemuseridNode.text;
var fullname = (fullnameNode == null) ? null : fullnameNode.text;// finally display the values.
alert(fullname + ", " + systemuserid);
} -
5. juni 2008 19:51
This is a great weblog for getting this information. I have searched the web extensively to find something for 3.0 that pulls the BusinessUnitId so that I can customize CRM 3.0 by Business Unit instead of user. This is what I came up with:
/********************************************************************/
/* GET CURRENT USER BUSINESS UNIT
/* Written by Chrisotpher Berry 6-3-08 for case #02387
/********************************************************************/
var xml = "" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap:Envelope xmlns
oap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
" <soap:Body>" +
" <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\" xmlns=\"http://schemas.microsoft.com/crm/2006/WebServices\">" +
" <q1:EntityName>systemuser</q1:EntityName>" +
" <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
" <q1:Attributes>" +
" <q1:Attribute>systemuserid</q1:Attribute>" +
" <q1:Attribute>fullname</q1:Attribute>" +
" <q1:Attribute>businessunitid</q1:Attribute>" +
" </q1:Attributes>" +
" </q1:ColumnSet>" +
" <q1
istinct>false</q1
istinct>" +
" <q1:Criteria>" +
" <q1:FilterOperator>And</q1:FilterOperator>" +
" <q1:Conditions>" +
" <q1:Condition>" +
" <q1:AttributeName>systemuserid</q1:AttributeName>" +
" <q1:AttributeName>businessunitid</q1:AttributeName>" +
" <q1
perator>EqualUserId</q1
perator>" +
" </q1:Condition>" +
" </q1:Conditions>" +
" </q1:Criteria>" +
" </query>" +
" </soap:Body>" +
"</soap:Envelope>" + "";var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2006/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2006/WebServices/RetrieveMultiple");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
var doc = xmlHttpRequest.responseXML;
var user = doc.selectSingleNode("//BusinessEntity");
var userId = user.selectSingleNode("systemuserid").text;
var userName = user.selectSingleNode("fullname").text;
var buid = user.selectSingleNode("businessunitid").text;if(buid == "{4973240C-7182-DB11-86E8-00188B2E04EE}")//this ID will vary, but is easily queried
{
alert(buid);
}
-
9. juni 2008 21:32
Hello,
I have tried your code Jeremiah, but I receive a prompt for authentication and I can't seem to resolve it. I am wondering if there is something configured incorrectly on my server... Any thoughts?
Thanks,
Jim
-
10. juni 2008 20:11
Nevermind. When I copied and pasted the code the emoticons had messed up one section and I figured it out.
Thanks - the code is a great example. Now I just need to see if this would work offline. I am guessing it doesn't as it would not have in 3.0... but here's hoping.
Jim