Thursday, January 15, 2015

Crm 2011 Soap Examples

RetrieveUserPrivilegesRequest = function (userId, successCallback, errorCallback) {
var requestMain = "";
requestMain += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
requestMain += "  <s:Body>";
requestMain += "    <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
requestMain += "      <request i:type=\"b:RetrieveUserPrivilegesRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
requestMain += "        <a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
requestMain += "          <a:KeyValuePairOfstringanyType>";
requestMain += "            <c:key>UserId</c:key>";
requestMain += "            <c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">" + userId + "</c:value>";
requestMain += "          </a:KeyValuePairOfstringanyType>";
requestMain += "        </a:Parameters>";
requestMain += "        <a:RequestId i:nil=\"true\" />";
requestMain += "        <a:RequestName>RetrieveUserPrivileges</a:RequestName>";
requestMain += "      </request>";
requestMain += "    </Execute>";
requestMain += "  </s:Body>";
requestMain += "</s:Envelope>";

var req = new XMLHttpRequest();
req.open("POST", JCL._getServerUrl(), true);

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.onreadystatechange = function () {
RetrieveUserPrivilegesResponse(req, successCallback, errorCallback);
};
req.send(requestMain);
};

RetrieveUserPrivilegesResponse = function (req, successCallback, errorCallback) {
///<summary>
/// Recieves the user privileges response
///</summary>
///<param name="req" Type="XMLHttpRequest">
/// The XMLHttpRequest response
///</param>
///<param name="successCallback" Type="Function">
/// The function to perform when an successfult response is returned.
/// For this message no data is returned so a success callback is not really necessary.
///</param>
///<param name="errorCallback" Type="Function">
/// The function to perform when an error is returned.
/// This function accepts a JScript error returned by the _getError function
///</param>
if (req.readyState == 4) {
if (req.status == 200) {
alert(req.responseXML.xml);
if (successCallback !== null) {
successCallback();
}
} else {
errorCallback(JCL._getError(req.responseXML));
}
}
};

getXhr = function () {
///<summary>
/// Get an instance of XMLHttpRequest for all browsers
///</summary>
if (XMLHttpRequest) {
// Chrome, Firefox, IE7+, Opera, Safari
// ReSharper disable InconsistentNaming
return new XMLHttpRequest();
// ReSharper restore InconsistentNaming
}
// IE6
try {
// The latest stable version. It has the best security, performance,
// reliability, and W3C conformance. Ships with Vista, and available
// with other OS's via downloads and updates.
return new ActiveXObject('MSXML2.XMLHTTP.6.0');
} catch (e) {
try {
// The fallback.
return new ActiveXObject('MSXML2.XMLHTTP.3.0');
} catch (e2) {
alert('This browser is not AJAX enabled.');
return null;
}
}
};

errorHandler = function (req) {
///<summary>
/// Private function return an Error object to the errorCallback
///</summary>
///<param name="req" type="XMLHttpRequest">
/// The XMLHttpRequest response that returned an error.
///</param>
///<returns>Error</returns>

var errorMessage = '';
if (req.responseText !== "") {
errorMessage = JSON.parse(req.responseText).error.message.value;
}

return new Error("Error : " + req.status + ": " + req.statusText + ": " + errorMessage);
};

No comments:

Post a Comment