|
if (typeof
(wod_SDK) == "undefined")
|
{
wod_SDK = { __namespace: true }; }
|
//This will establish a
more unique namespace for functions in this library. This will reduce the
|
// potential for
functions to be overwritten due to a duplicate name when the library is
loaded.
|
wod_SDK.QueuesLib
= {
|
_getServerUrl: function () {
|
///<summary>
|
/// Returns the URL for the SOAP endpoint using
the context information available in the form
|
/// or HTML Web resource.
|
///</summary>
|
var ServicePath = "/XRMServices/2011/Organization.svc/web";
|
var serverUrl = "";
|
if (typeof GetGlobalContext == "function") {
|
var context = GetGlobalContext();
|
serverUrl =
context.getServerUrl();
|
}
|
else {
|
if (typeof Xrm.Page.context == "object") {
|
serverUrl =
Xrm.Page.context.getServerUrl();
|
}
|
else
|
{ throw new Error("Unable to access the server URL"); }
|
}
|
if (serverUrl.match(/\/$/)) {
|
serverUrl =
serverUrl.substring(0, serverUrl.length - 1);
|
}
|
|
return serverUrl + ServicePath;
|
},
|
AddToQueueRequest: function
(prmSourceQueueId, prmDestinationQueueId, prmEntityName, prmEntityId) {
|
var wod_Request = ""
|
wod_Request += "<s:Envelope
xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
|
wod_Request += " <s:Body>";
|
wod_Request += " <Execute
xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\"
xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
|
wod_Request += " <request
i:type=\"b:AddToQueueRequest\"
xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\"
xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
|
wod_Request += " <a:Parameters
xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
|
wod_Request += "
<a:KeyValuePairOfstringanyType>";
|
wod_Request += "
<c:key>Target</c:key>";
|
wod_Request += " <c:value
i:type=\"a:EntityReference\">";
|
wod_Request += " <a:Id>" +
prmEntityId + "</a:Id>";
|
wod_Request += " <a:LogicalName>" +
prmEntityName + "</a:LogicalName>";
|
wod_Request += " <a:Name i:nil=\"true\"
/>";
|
wod_Request += " </c:value>";
|
wod_Request += "
</a:KeyValuePairOfstringanyType>";
|
wod_Request += "
<a:KeyValuePairOfstringanyType>";
|
wod_Request += " <c:key>DestinationQueueId</c:key>";
|
wod_Request += " <c:value
i:type=\"d:guid\"
xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">" +
prmDestinationQueueId + "</c:value>";
|
wod_Request += " </a:KeyValuePairOfstringanyType>";
|
|
if (prmSourceQueueId != null
&& prmSourceQueueId != "") {
|
wod_Request += "
<a:KeyValuePairOfstringanyType>";
|
wod_Request += "
<c:key>SourceQueueId</c:key>";
|
wod_Request += " <c:value
i:type=\"d:guid\"
xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">" +
prmSourceQueueId + "</c:value>";
|
wod_Request += "
</a:KeyValuePairOfstringanyType>";
|
}
|
|
wod_Request += " </a:Parameters>";
|
wod_Request += " <a:RequestId
i:nil=\"true\" />";
|
wod_Request += "
<a:RequestName>AddToQueue</a:RequestName>";
|
wod_Request += " </request>";
|
wod_Request += " </Execute>";
|
wod_Request += " </s:Body>";
|
wod_Request += "</s:Envelope>";
|
|
var req = new XMLHttpRequest();
|
req.open("POST",
wod_SDK.QueuesLib._getServerUrl(), false)
|
req.setRequestHeader("Accept", "application/xml,
text/xml, */*");
|
req.setRequestHeader("Content-Type", "text/xml;
charset=utf-8");
|
req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
|
req.send(wod_Request);
|
|
//Checking if error occurs
|
var errorMessage =
wod_SDK.QueuesLib._getError(req.responseXML);
|
|
if (errorMessage != "") {
|
alert(errorMessage);
|
}
|
},
|
_getError: function (faultXml) {
|
///<summary>
|
/// Parses the WCF fault returned in the event
of an error.
|
///</summary>
|
///<param name="faultXml"
Type="XML">
|
/// The responseXML property of the
XMLHttpRequest response.
|
///</param>
|
var errorMessage = "";
|
if (typeof faultXml == "object") {
|
try {
|
var
bodyNode = faultXml.firstChild.firstChild;
|
//Retrieve the fault node
|
for (var i = 0;
i < bodyNode.childNodes.length; i++) {
|
var node = bodyNode.childNodes[i];
|
|
//NOTE: This comparison
does not handle the case where the XML namespace changes
|
if ("s:Fault" ==
node.nodeName) {
|
for (var j = 0;
j < node.childNodes.length; j++) {
|
var
faultStringNode = node.childNodes[j];
|
if ("faultstring" ==
faultStringNode.nodeName) {
|
errorMessage
= faultStringNode.text;
|
break;
|
}
|
}
|
break;
|
}
|
}
|
}
|
catch (e) { };
|
}
|
return errorMessage;
|
},
|
|
__namespace: true
|
};
|
|
|