Answered by:
Reading Web.config appSettings from CRM Form JavaScript

Question
-
Hi,
I wanted to add custom appSettings keys in MS CRM website and read it from the CRM Form JavaScript code.
I added the test keys in Microsoft Dynamics CRM website over the IIS and used this code to extract value in the Form's OnLoad() event.
var v1 = '<%=ConfigurationManager.AppSettings["test"].ToString() %>' alert(v1);
But instead of value it is displaying v1 value as simple string i.e. <%=ConfigurationManager.AppSettings["test"].ToString() %>
The code is working fine in simple asp.net page. What's wrong with this code here in CRM?
Thank you.
Paradise lies at the feet of thy mother. - Prophet Mohammed (PBUH)- Edited by Khadim Ali Monday, February 28, 2011 2:01 PM
Monday, February 28, 2011 1:46 PM
Answers
-
Be aware that web.config is only available server-side, and not from client-side technologies such as Javascript, so the code in earlier posts could never work. The url referenced in the previous post shows how to create a simple server-side ASP.Net page that can read web.config, and then exposes it to client-side code
Microsoft CRM MVP - http://mscrmuk.blogspot.com http://www.excitation.co.uk- Proposed as answer by Daniel Cai - KingswaySoftMVP Monday, February 28, 2011 5:25 PM
- Unproposed as answer by Khadim Ali Tuesday, March 1, 2011 6:45 AM
- Marked as answer by Khadim Ali Wednesday, March 2, 2011 11:18 AM
Monday, February 28, 2011 3:12 PMModerator -
Do a last try by typing the code not copying pasting.
Regards Faisal
Still not working.I have googled the issue and found a discussion that concluding what David said. But I am unable to understand why it is working in normal asp.net application.
It is suggesting few ways to achieve the same:
- to modify global.js file on the CRM Server
- to create a custom webservice that can access ConfigurationManager and call it from form script
- to create an entity to hold default ids. (seeming the most handy to me).
Regards,
Paradise lies at the feet of thy mother. - Prophet Mohammed (PBUH)- Marked as answer by Khadim Ali Wednesday, March 2, 2011 11:18 AM
Tuesday, March 1, 2011 11:34 AM
All replies
-
; is missing in declaration
Regards FaisalMonday, February 28, 2011 2:00 PM -
; is missing in declaration
Regards Faisal
No. Still giving the same result.var v1 = '<%=System.Configuration.ConfigurationManager.AppSettings["pricelevelid"].ToString() %>'; alert(v1);
Paradise lies at the feet of thy mother. - Prophet Mohammed (PBUH)Monday, February 28, 2011 2:05 PM -
Just a try,
remove var while declaring variable. It will become global variable.
Regards FaisalMonday, February 28, 2011 2:15 PM -
Just a try,
remove var while declaring variable. It will become global variable.
Regards FaisalAlthough I was using the alert statement in the very same OnLoad() event where the variable was declared. However, I tried your this suggestion, but obviously without result. :)
Paradise lies at the feet of thy mother. - Prophet Mohammed (PBUH)Monday, February 28, 2011 2:20 PM -
- Proposed as answer by Daniel Cai - KingswaySoftMVP Monday, February 28, 2011 5:25 PM
- Unproposed as answer by Khadim Ali Tuesday, March 1, 2011 6:45 AM
Monday, February 28, 2011 2:42 PM -
Be aware that web.config is only available server-side, and not from client-side technologies such as Javascript, so the code in earlier posts could never work. The url referenced in the previous post shows how to create a simple server-side ASP.Net page that can read web.config, and then exposes it to client-side code
Microsoft CRM MVP - http://mscrmuk.blogspot.com http://www.excitation.co.uk- Proposed as answer by Daniel Cai - KingswaySoftMVP Monday, February 28, 2011 5:25 PM
- Unproposed as answer by Khadim Ali Tuesday, March 1, 2011 6:45 AM
- Marked as answer by Khadim Ali Wednesday, March 2, 2011 11:18 AM
Monday, February 28, 2011 3:12 PMModerator -
Be aware that web.config is only available server-side, and not from client-side technologies such as Javascript, so the code in earlier posts could never work. The url referenced in the previous post shows how to create a simple server-side ASP.Net page that can read web.config, and then exposes it to client-side code
Microsoft CRM MVP - http://mscrmuk.blogspot.com http://www.excitation.co.uk
Sorry, I am not able to understand your point.If we can get web.config elements (e.g. appSettings) from client-side code in a normal aspx page, then why we can't do it in a CRM page which itself is an aspx page?
And if so, then how could we avoid hard coding of values (for e.g. default Guids) in form scripts?
Paradise lies at the feet of thy mother. - Prophet Mohammed (PBUH)Tuesday, March 1, 2011 6:12 AM -
check this:-
Regards Faisal
Faisal,I have tried that code in a normal .aspx page and it is working fine. But not in CRM form. Will you please examine it practically?
Paradise lies at the feet of thy mother. - Prophet Mohammed (PBUH)Tuesday, March 1, 2011 6:13 AM -
Do a last try by typing the code not copying pasting.
Regards FaisalTuesday, March 1, 2011 11:17 AM -
Do a last try by typing the code not copying pasting.
Regards Faisal
Still not working.I have googled the issue and found a discussion that concluding what David said. But I am unable to understand why it is working in normal asp.net application.
It is suggesting few ways to achieve the same:
- to modify global.js file on the CRM Server
- to create a custom webservice that can access ConfigurationManager and call it from form script
- to create an entity to hold default ids. (seeming the most handy to me).
Regards,
Paradise lies at the feet of thy mother. - Prophet Mohammed (PBUH)- Marked as answer by Khadim Ali Wednesday, March 2, 2011 11:18 AM
Tuesday, March 1, 2011 11:34 AM -
Another suggestion is creating custom generic handler (file.ashx) that would expose the configuration values.
Islam Eldemery
http://idemery.net
Tuesday, March 1, 2011 11:49 AM -
Another suggestion is creating custom generic handler (file.ashx) that would expose the configuration values.
Will you please explain the steps?
Islam Eldemery
http://idemery.net
Paradise lies at the feet of thy mother. - Prophet Mohammed (PBUH)Tuesday, March 1, 2011 12:08 PM -
Make a new empty asp.net empty website, right click the project to add a new item, choose generic handler and give it a name.
Navigate to the generic handler code and customize it, the following is quick a sample:
using System; using System.Web; public class Handler : IHttpHandler { public void ProcessRequest (HttpContext context) { HttpResponse response = context.Response; response.ContentType = "text/plain"; string configKey = context.Request.QueryString["key"]; if (string.IsNullOrEmpty(configKey)) { response.StatusCode = 404; return; } string configValue = ConfigurationManager.AppSettings[configKey]; response.Write(configValue); } public bool IsReusable { get { return false; } } }
Now deploy this generic handler and put the .dll in the CRM bin folder, you can call the handler file from html and javascript as follows
function InvokeASHXToGetConfigValue(configKey) { InitXmlHttp(); xmlhttp.onreadystatechange= XMLHttpRequestCompleted; xmlhttp.open("GET", "" + "Handler.ashx?key=configKey", true ); xmlhttp.send(null); } function XMLHttpRequestCompleted() { if (xmlhttp.readyState==4) { try { alert(xmlhttp.responseText);// configValue } catch (e) { } } } function InitXmlHttp() { // Attempt to initialize xmlhttp object try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { // Try to use different activex object try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp = false; } } // If not initialized, create XMLHttpRequest object if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp = new XMLHttpRequest(); } // Define function call for when Request obj state has changed xmlhttp.onreadystatechange=XMLHttpRequestCompleted; }
Islam Eldemery
http://idemery.net
Tuesday, March 1, 2011 12:31 PM -
Oh Islam!
It's a lengthy way. Any way thanks for your answer.
Paradise lies at the feet of thy mother. - Prophet Mohammed (PBUH)Tuesday, March 1, 2011 12:53 PM -
You can try reading the file in javascript and load it in xml.
And read the nodes that you want.
Tuesday, March 1, 2011 11:36 PM -
Just create your variable in .aspx page and use that variable in your JS file
<script language="javascript" type="text/javascript">
v1 = '<%=ConfigurationManager.AppSettings["test"].ToString() %>'
</script>
Tuesday, February 14, 2012 6:25 AM