crm 2013: javascript get attributes from other entity in Contact Form

Question Status

Suggested Answer
tiagosh asked a question on 22 Oct 2014 10:47 AM

Hi. I'm trying to get specific user information (e.g. Full Name) in the Contact Form. I'm trying to do it as follows:

function getCurrentUserFullName() {
    var serverUrl = "http://server:port/OrgName";
    var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc"; 
    var userRequest = new XMLHttpRequest(); 
    userRequest.open("GET", ODataPath + "/SystemUserSet(guid'" + Xrm.Page.context.getUserId() + "')", false); 
    userRequest.setRequestHeader("Accept", "application/json"); 
    userRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8"); 
    userRequest.send();
    if (userRequest.status === 200) {
        var retrievedUser = JSON.parse(userRequest.responseText).d; 
        var userFullName = retrievedUser.FullName;
        var userOther_custom_attribute = retrievedUser.other_custom_attribute;
        alert(userFullName);
		//do other stuff
    }
    else {
        return "error";
    }
}


The problem seems to be here, as the error shows:

<Function>rror:Failedtoexecute'send'on'XMLHttpRequest':Failedtoload'http://server:port/OrgName/XRMServices/2011/OrganizationData.svc/SystemUserSet(guid'%7CD7F832CC-7008-E203-80E0-11155DABE414%7D')</Function>


The errors at browser are:

XMLHttpRequest cannot load http://server:port/OrgName/XRMServices/2011/OrganizationData.svc/SystemUserSet(guid'%7CD7F832CC-7008-E203-80E0-11155DABE414%7D'). No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://server:port' is therefore not allowed access. The response had HTTP status code 404.

I've read about it and it seems i have to enable CORS at server-side, any idea on how to do that?

Or other solution but that?

I'm using on-premises installation, CRM 2013, updated with Rollup 2.

Any help appreciated,

Thanks

Reply
Suggested Answer
Mahender responded on 22 Oct 2014 11:04 AM

In MS CRM 2013 you can get user name directly using below code, you don't need to write service call

Xrm.Page.context.getUserName();

Reply
Suggested Answer
Chitrarasan Duraisamy responded on 22 Oct 2014 11:08 AM

Try with belwo script

function getCurrentUserFullName() {

   var serverUrl = Xrm.Page.context.getClientUrl();

   var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";

   var userRequest = new XMLHttpRequest();

   userRequest.open("GET", ODataPath + "/SystemUserSet(guid'" + Xrm.Page.context.getUserId() + "')", false);

   userRequest.setRequestHeader("Accept", "application/json");

   userRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");

   userRequest.send();

   if (userRequest.status === 200) {

       var retrievedUser = JSON.parse(userRequest.responseText).d;

       var userFullName = retrievedUser.FullName;

       var userOther_custom_attribute = retrievedUser.other_custom_attribute;

       alert(userFullName);

       //do other stuff

   }

   else {

       return "error";

   }

}

Reply
Suggested Answer
Aileen Gusni responded on 22 Oct 2014 11:11 AM

HI Tiagosh,

CORS is needed if you call js using cross domain call to call another Rest from a form.

See this link:

community.dynamics.com/.../making-cross-domain-ajax-calls-from-crm-form-jscript.aspx

But, now you are trying to retrieve an entity record (system user) from a CRM Form (contact form). Don't think it needs you to have this, you might try another way first.

Actually I would suggest you now to use SDK.REST.js reference from CRM SDK to ease use writing javascript to retrieve from another entity.

See this blog as well:

missdynamicscrm.blogspot.com/.../tips-and-trick-odata-crm-2011-2013.html

The reference script will give easier way which has handled the calling, you just need to pass the query and entity set name you want to query.

Anyway, if you still want to use your script, look at your script, you can change the hardcoded server to URL to this URL:

You can use script to get relative path by your current server URL that you are in.

(Server URL can be from IP, DNS name, hostname, public name, etc)

Which you can access from http://192.168.10.14:5555/orgname or http://myserver:5555 or https://myserverwithhttps:5555 or https://mypubliccrm.crm.com:5555, etc.

But if users login using IP then your code user server name, if you notice, if the users are not being authenticated automatically, it will prompt another pop up to provide credential which means using IP, using server name, they are different and need credential. So if user logins using IP, your code uses server name, the user has provided credential to IP, but he is possible, does not have credential to access using server name, so that he receives 404 error.

You can use this:

var serverUrl = Xrm.Page.context.getServerUrl();

//The OData end-point

var ODATA_ENDPOINT = “/XRMServices/2011/OrganizationData.svc”;

Or you can use prepend org name:

msdn.microsoft.com/.../gg334511.aspx

var ODATA_ENDPOINT = Xrm.Page.context.prependOrgName("/XRMServices/2011/OrganizationData.svc");

Hope this helps!

Thanks.

Reply
Suggested Answer
Ben Hosking responded on 22 Oct 2014 11:44 AM

Where it says server:port e.g. CRMservername:80

this is where you are meant to type in your actual details

e.g. server name should be the bit after the http:// when you connect to crm

http://CRM server name/Org name/main.aspx#

you can find out what the server name is by doing this

Xrm.Page.context.getClientUrl()

and then remove the org from it

But I would advise you use XRM toolkit if you want to do these queries

https://xrmservicetoolkit.codeplex.com/

I agree with Aileen Gusni the best way to retrieve data is using Odata calls, I would use the Odataquerydesigner to work out the correct odata call - https://crm2011odatatool.codeplex.com/

 

 

Reply
Suggested Answer
Mahender responded on 22 Oct 2014 11:04 AM

In MS CRM 2013 you can get user name directly using below code, you don't need to write service call

Xrm.Page.context.getUserName();

Reply
Suggested Answer
Chitrarasan Duraisamy responded on 22 Oct 2014 11:08 AM

Try with belwo script

function getCurrentUserFullName() {

   var serverUrl = Xrm.Page.context.getClientUrl();

   var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";

   var userRequest = new XMLHttpRequest();

   userRequest.open("GET", ODataPath + "/SystemUserSet(guid'" + Xrm.Page.context.getUserId() + "')", false);

   userRequest.setRequestHeader("Accept", "application/json");

   userRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");

   userRequest.send();

   if (userRequest.status === 200) {

       var retrievedUser = JSON.parse(userRequest.responseText).d;

       var userFullName = retrievedUser.FullName;

       var userOther_custom_attribute = retrievedUser.other_custom_attribute;

       alert(userFullName);

       //do other stuff

   }

   else {

       return "error";

   }

}

Reply
Suggested Answer
Aileen Gusni responded on 22 Oct 2014 11:11 AM

HI Tiagosh,

CORS is needed if you call js using cross domain call to call another Rest from a form.

See this link:

community.dynamics.com/.../making-cross-domain-ajax-calls-from-crm-form-jscript.aspx

But, now you are trying to retrieve an entity record (system user) from a CRM Form (contact form). Don't think it needs you to have this, you might try another way first.

Actually I would suggest you now to use SDK.REST.js reference from CRM SDK to ease use writing javascript to retrieve from another entity.

See this blog as well:

missdynamicscrm.blogspot.com/.../tips-and-trick-odata-crm-2011-2013.html

The reference script will give easier way which has handled the calling, you just need to pass the query and entity set name you want to query.

Anyway, if you still want to use your script, look at your script, you can change the hardcoded server to URL to this URL:

You can use script to get relative path by your current server URL that you are in.

(Server URL can be from IP, DNS name, hostname, public name, etc)

Which you can access from http://192.168.10.14:5555/orgname or http://myserver:5555 or https://myserverwithhttps:5555 or https://mypubliccrm.crm.com:5555, etc.

But if users login using IP then your code user server name, if you notice, if the users are not being authenticated automatically, it will prompt another pop up to provide credential which means using IP, using server name, they are different and need credential. So if user logins using IP, your code uses server name, the user has provided credential to IP, but he is possible, does not have credential to access using server name, so that he receives 404 error.

You can use this:

var serverUrl = Xrm.Page.context.getServerUrl();

//The OData end-point

var ODATA_ENDPOINT = “/XRMServices/2011/OrganizationData.svc”;

Or you can use prepend org name:

msdn.microsoft.com/.../gg334511.aspx

var ODATA_ENDPOINT = Xrm.Page.context.prependOrgName("/XRMServices/2011/OrganizationData.svc");

Hope this helps!

Thanks.

Reply
Suggested Answer
Ben Hosking responded on 22 Oct 2014 11:44 AM

Where it says server:port e.g. CRMservername:80

this is where you are meant to type in your actual details

e.g. server name should be the bit after the http:// when you connect to crm

http://CRM server name/Org name/main.aspx#

you can find out what the server name is by doing this

Xrm.Page.context.getClientUrl()

and then remove the org from it

But I would advise you use XRM toolkit if you want to do these queries

https://xrmservicetoolkit.codeplex.com/

I agree with Aileen Gusni the best way to retrieve data is using Odata calls, I would use the Odataquerydesigner to work out the correct odata call - https://crm2011odatatool.codeplex.com/

 

 

Reply