Posts

Showing posts from 2012

Hiding Left Navigation Menu Items in an Entity Form using Javascript in MS CRM 2011

In the form designer, you can remove the items from the left navigation. If you remove all the items in the navigation area, the group header will be hidden as well except for some navigation areas like Common, Marketing and Processes.

But if you're looking for a solution to hide the navigation items based on the condition, values, etc. you can use the following JavaScript to hide them.

var navitem = Xrm.Page.ui.navigation.items.get("nav_new_test_new_testchild") // for e.g "navContacts"; if (navitem != null) { navitem.setVisible(false); }
Normally, the name of the navigation is

"nav" + Entityname in plural for the system entities or"nav_" + relationship name between these 2 entities
but there are some exception cases for the system entities like "navOpps" so that if it's not working just by guessing, you can use IE Developer Toolbar to find the name of the navigation item.

Hiding the navigation items with Jav…

Referencing Multiple Javascript Libraries in Homepage Ribbon Buttons

When we develop JavaScript functions for the Actions and CustomRules of the Ribbon buttons in CRM 2011, we may need to reference the common JavaScript library or other javascript libraries. For the ribbon buttons on the form, we can simply add those additional libraries in the Form Libraries and call the functions in those libraries but for Homepage ribbon buttons, we need to try a different approach to achieve it.

<Actions> <JavaScriptFunction Library="$webresource:new_MainEntityLibrary" FunctionName="Test" /> <JavaScriptFunction Library="$webresource:new_CommonLibrary" FunctionName="isNaN" /> //This is the additional library </Actions>
The FunctionName of the JavaScriptFunction action tag is required so that putting isNaN as a value will do the trick instead of calling the dummy function with no action.
This method can also be used for Sub-grid ribbon buttons if the sub-grid is displayed is multiple forms …

Retrieve Label/Values of Option Set/Two Options Attributes using SQL Script

When you want to retrieve the list of  Label and Values of Option Set and Two Options attributes for SSRS report parameter, documentation, development or whatever purpose, you can query by using the following SQL Script.
You will have to remove/modify the two lines of conditions ("new_testentity" and "new_testoptionsetattribute") accordingly to your requirement.

SELECT Value AS [Text], AttributeValue AS [Value] FROM FilteredStringMap WHERE FilteredViewName = 'Filterednew_testentity' AND AttributeName = 'new_testoptionsetattribute' AND LangId = 1033 -- Language Code, English U.S is 1033 ORDER BY DisplayOrder
This is the alternative query over Schema tables but that won't work in SSRS.

SELECT DISTINCT Entity.LogicalName AS EntitySchemaName , Attribute.LogicalName AS AttributeSchemaName , OptionSet.Name AS OptionSetName , AttributePicklistValue.Value AS OptionValue , LocalizedLabel.L…

Populate Regarding Attribute in New Email Form

Image
When we created a new email for a record by Clicking “New Activity” button in the “Activities” associated view, it will prompt us to select the activity type.

And when the user select E-mail and click OK, the new email form will is shown with the “From” attribute populated with the current user and the “Regarding” attribute populated with the record from which we clicked “New Activity” and everything is fine.


According to the users’ requirement, there’s a case that we have to show the Subgrid of email related to the record on the form. And that’s where the problem started. When the user click “Add New E-mail” button from the Subgrid and the new Email form is loaded, the record is not populated in the regarding attribute. So, the user demands for the same functionality as the new Activity since it would be a hassle to select the Regarding attribute manually.


So, my solution was writing a JavaScript code at the onLoad of the Email form which will auto populate the “Regarding” attribute…

ForceSubmit vs setSubmitMode in CRM 2011 Javascript

Previously, we used
crmForm.all.[fieldname].ForceSubmit = true; in CRM 4.0 to force the CRM system to submit the form data on disabled fields.
But in CRM 2011, the method is changed to
Xrm.Page.getAttribute([fieldname]).setSubmitMode("always"); but the old CRM 4.0 object model is still supported for backward compatibility so that setting true flag to ForceSubmit property seems to be working. But actually, there is a difference between using ForceSubmit and setSubmitMode to save the data in the disabled fields in CRM 2011.
In CRM 2011, there's a Xrm.Page.data.entity.getIsDirty() method, which returns a Boolean value that indicates if any fields in the form have been modified. When we set true flag to ForceSubmit property of the disabled field, the IsDirty flag of the entity won't be changed when the value of that disabled field is updated from the JavaScript code. But when we use setSubmitMode, the IsDirty flag of the entity will be changed when the value of tha…

The type or namespace name 'Xrm' does not exist in the namespace

Image
When we start learning about server side extension in CRM 2011, we typically start building a console application and try out CRUD operations consuming CRM web services by referencing sample codes from CRM SDK. So, we normally create a console application, reference the required DLLs from the SDK\bin folder and use those CRM namespace in the code file.
When you try to build the solution, you may get the following error if you don't leave all the settings by default since the console project has been created.

The type or namespace name 'Xrm' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)


All you have to solve this problem is right click the project, select Properties and change the Target Framework from “.NET Framework 4 Client Profile” to “.NET Framework 4”.

Now, you can start coding and carry on your console application.

Preventing Caching of JavaScript files in IIS7

Image
When there are a lot of client side scripting involved in the MS CRM project, I normally load external JavaScript files for faster development. Opening external js code files in Notepad++ is readable and looks so much better than CRM web resource text editor and we can save a few seconds of publishing web resource after every modification of the code. But we need to disable caching for JavaScript files in IIS or else we have to delete temporary internet files in Internet Explorer to load the updated script file after each modification of the code.

In order to prevent caching of JavaScript files in IIS7, the Output Caching setting of the MS CRM site in ISS Manager need to be changed.

We need to add one entry in the Output Caching with the File extension name ".js" and enable both User-mode caching and Kernel-mode caching and choose Using file change notifications radio button for both sections. Then, click Advanced button and enable Headers checkbox and enter "Cache-Cont…

Bypassing Prompt and Refresh the Form

Image
In some scenarios of client site scripting in MS CRM 2011 (mostly in the custom ribbon button actions), we might have to force the form to refresh in order to show the updated values or refresh the Enable/Disable Show/Hide status of the ribbon buttons.
But sometimes, just triggering the window.location.reload cause the prompt and prevent the form from refreshing if the user has changed any of the values on the form before triggering the ribbon button event.



In order to avoid such prompt, we can check the Dirty state of the form and force to save if the user has updated any field. And simply refresh the form if there is no change done.

function ForceRefreshForm() { if(Xrm.Page.data.entity.getIsDirty()) Xrm.Page.data.entity.save(); else window.location.reload(true); }