I am trying to know if a user has a special privilege "prvOverridePriceEngineQuote" but I don't get it done.
My code is looking like:
var userid = Xrm.Page.context.getUserId();
var userhasPrivilege;
var quoteFetchXML = "<fetch version=\"1.0\" output-format=\"xml-platform\" mapping=\"logical\" distinct=\"false\">" +
"<entity name=\"privilege\">" +
"<attribute name=\"name\" />" +
"<filter>" +
"<condition attribute=\"name\" operator=\"eq\" value=\"prvOverridePriceEngineQuote\" />" +
"</filter>" +
"<link-entity name=\"roleprivileges\" from=\"privilegeid\" to=\"privilegeid\">" +
"<link-entity name=\"role\" from=\"roleid\" to=\"roleid\">" +
"<link-entity name=\"systemuserroles\" from=\"roleid\" to=\"roleid\">" +
"<link-entity name=\"systemuser\" from=\"systemuserid\" to=\"systemuserid\">" +
"<filter>" +
"<condition attribute=\"systemuserid\" operator=\"eq\" value=\"" + userid + "\" />" +
"</filter>" +
"</link-entity>" +
"</link-entity>" +
"</link-entity>" +
"</link-entity>" +
"</entity>" +
"</fetch>";
var encodedFetchXML = encodeURIComponent(quoteFetchXML);
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/accounts?fetchXml=" + encodedFetchXML, true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
}
}
};
req.send();
It is working right when I test it in XRMToolBox but not when I run the code with javascript in Dynamics crm.
I get this error:
"Could not find a property named 'privilegeid' on type 'Microsoft.Dynamics.CRM.account'"
"{\r\n \"error\":{\r\n \"code\":\"\",\"message\":\"Could not find a property named 'privilegeid' on type 'Microsoft.Dynamics.CRM.account'.\",\"innererror\":{\r\n \"message\":\"Could not find a property named 'privilegeid' on type 'Microsoft.Dynamics.CRM.account'.\",\"type\":\"Microsoft.OData.Core.ODataException\",\"stacktrace\":\" at Microsoft.OData.Core.UriParser.Parsers.SelectPathSegmentTokenBinder.ConvertNonTypeTokenToSegment(PathSegmentToken tokenIn, IEdmModel model, IEdmStructuredType edmType, ODataUriResolver resolver)\r\n at Microsoft.OData.Core.UriParser.Visitors.SelectPropertyVisitor.ProcessTokenAsPath(NonSystemToken tokenIn)\r\n at Microsoft.OData.Core.UriParser.Visitors.SelectPropertyVisitor.Visit(NonSystemToken tokenIn)\r\n at Microsoft.OData.Core.UriParser.Parsers.SelectBinder.Bind(SelectToken tokenIn)\r\n at Microsoft.OData.Core.UriParser.Parsers.SelectExpandBinder.Bind(ExpandToken tokenIn)\r\n at Microsoft.OData.Core.UriParser.Parsers.SelectExpandSemanticBinder.Bind(IEdmStructuredType elementType, IEdmNavigationSource navigationSource, ExpandToken expandToken, SelectToken selectToken, ODataUriParserConfiguration configuration)\r\n at Microsoft.OData.Core.UriParser.ODataQueryOptionParser.ParseSelectAndExpandImplementation(String select, String expand, ODataUriParserConfiguration configuration, IEdmStructuredType elementType, IEdmNavigationSource navigationSource)\r\n at Microsoft.OData.Core.UriParser.ODataQueryOptionParser.ParseSelectAndExpand()\r\n at Microsoft.Crm.Extensibility.OData.EntityController.GetEntitySet(String entitySetName)\r\n at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.b__9(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()\"\r\n }\r\n }\r\n}"
Do you have an idea what I am doing wrong or how I could do it?
The error is caused because you're executing the FetchXML on the Account entity instead of the Privilege one.
You need to change this line:
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/accounts?fetchXml="
By this other one:
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/privileges?fetchXml="