CRM read-only fields don't get saved in the database
If you have some fields that are only informative fields that you populate yourself by JavaScript, normally you will set them to be readonly because you don't want your users to be able to edit the value. If you do so, your value will no be saved in the database. The main reason for this is because when you post a form in an HTML page with disabled fields, the disabled fields do not get posted. If you want their values to be posted, you need to enable them.
CRM offers you the ability to save the values of the disabled fields. There is a boolean property named ForceSubmit on each field of the form. You can set this property to true if you want its value to be saved.
crmForm.all.fieldname.ForceSubmit = true;
How to know if a user is in a specified role in Microsoft Dynamics CRM 4.0 (JavaScript)
In CRM 4.0, when you want to know if the currently logged in user is in a certain security role with JavaScript, you need
to issue a request to the server. Yes, this is the officially supported way to do this, but i'll explain to you a good solution
to avoid doing a lot of code to call the server with AJAX.
The solution I've developped is a lot more performant because you don't need to call (AJAXly) the server, but it is officially
unsupported because you have to inect some code in the CRM pages at runtime.
HttpModules...
For a lot of things in CRM we've used HttpModules to inject some JavaScript code onto the CRM pages (in the <head> tag).
In your HttpModule you simply have to call the CrmService web reference to get all the roles of the currently logged in user.
After you get this, you build a string that correspond to a JavaScript Array of "typed" objects.
Yeah, in JavaScript everything is untyped; you all declare variables with the "var" keyword. But, you can build custom objects
and set your own properties on it.
Lets say you do this :
var objRole = new Object();
objRole.GUID = [GUID of the CRM security role];
objRole.Name = [Name of the CRM security role];
You get an object representing your security role with all the informations you need.
So, you have to build (with a StringBuilder) the JavaScript string that will outputed to the CRM page at runtime.
Let's do something like this in code :
StringBuilder strJS = new StringBuilder();
strJS.AppendLine("var SECURITY_ROLES = new Array();");
foreach (CrmService.role objRole in securityRoles)
{
strJS.AppendLine("SECURITY_ROLES[SECURITY_ROLES.length] = new Object();");
strJS.AppendLine(String.Format("SECURITY_ROLES[SECURITY_ROLES.length].Name = '{0}';", objRole.name));
strJS.AppendLine(String.Format("SECURITY_ROLES[SECURITY_ROLES.length].GUID = '{0}';", objRole.roleid));
}
string javaScriptString = strJS.ToString();
Then, you just inject thoses lines of JS code : [HeadTag].Append(javaScriptString);
The [HeadTag] is a class that we developped to encapsulate the stream property named "Filter" of the HttpResponse object, accessible
on the HttpModule. This property contains the HTML to is about to be sent to the client so we use this to inject our JavaScript code.
With this solution, in each page in CRM you have a JavaScript array called SECURITY_ROLES containing all the roles of the current user.
So if you want to check if the user is part of a group, you can check that array instead of issuing a lot of AJAX request to the server at runtime.
How to get the username client-side in Microsoft Dynamics CRM 4.0
In CRM 4.0, when you want to know to NT username of the currently logged in user with JavaScript, you need
to issue a request to the server. This sounds (and IS) ridiculous and also no performant.
The solution I've developped is a lot more performant because you don't need to call (AJAXly) the server
to get the username of the current user.
HttpModules...
For a lot of things in CRM we've used HttpModules to inject some JavaScript code onto the CRM pages (in the <head> tag).
For the currently logged in user we just inject this line of .Net code : [HeadTag].AppendFormat("var USERNAME='{0}';", My.User.Name);
The [HeadTag] is a class that we developped to encapsulate the stream property named "Filter" of the HttpResponse object, accessible
on the HttpModule. This property contains the HTML to is about to be sent to the client so we use this to inject our JavaScript code.
With this solution, in each page in CRM you have a JavaScript variable called USERNAME that you can use to have the username !
You can also do the same thing to have the current computer name.