Category Archives: ASP.NET

Microsoft Dynamics CRM 2011 JavaScript Development Reference

0
Filed under ASP.NET, customizations, Dynamics CRM2011, JScript, Microsoft

I’ve been working on the development of a CRM 2011 deployment for a few months now and I’ve found myself poking around the web to all kinds of different websites looking for references to different parts of the SDK for JavaScript.

Now that I’ve become more familiar with the MSDN Library I’ve decided to compile all the major functions for the JavaScript reference into one easy reference point with links branching out to the MSDN library. The MSDN library is amazing but can be rough to navigate and find what you really want fast.

Hope others find this cheat sheet useful.

If you have anything to add, feel free to leave a comment.

Xrm.Page.context Methods

getAuthenticationHeader A deprecated method that returns the encoded SOAP header necessary to use Microsoft Dynamics CRM Web service calls using JScript.
getCurrentTheme Returns a string representing the current Microsoft Office Outlook theme chosen by the user.
getOrgLcid Returns the LCID value that represents the Microsoft Dynamics CRM Language Pack that is the base language for the organization.
getOrgUniqueName Returns the unique text value of the organizations name.
getQueryStringParameters Returns an array of key value pairs representing the query string arguments that were passed to the page.
getServerUrl Returns the base server URL. When a user is working offline with Microsoft Dynamics CRM for Microsoft Office Outlook, the URL is to the local Microsoft Dynamics CRM Web services.
getUserId Returns the GUID value of the SystemUser.id value for the current user.
getUserLcid Returns the LCID value that represents the Microsoft Dynamics CRM Language Pack that is the user selected as their preferred language.
getUserRoles Returns an array of strings representing the GUID values of each of the security roles that the user is associated with.
isOutlookClient Returns a Boolean value indicating if the user is using Microsoft Dynamics CRM for Microsoft Office Outlook.
isOutlookOnline Returns a Boolean value indicating whether the user is connected to the Microsoft Dynamics CRM server while using Microsoft Dynamics CRM for Microsoft Office Outlook with Offline Access. When this function returns false, the user is working offline without a connection to the server. They are interacting with an instance of Microsoft Dynamics CRM running on their local computer.
prependOrgName Prepends the organization name to the specified path.

 

Xrm.Page.data.entity Attribute Methods

Method Attribute Types Description
addOnChange All Sets a function to be called when the attribute value is changed.See also removeOnChange
fireOnChange All Causes the OnChange event to occur on the attribute so that any script associated to that event can execute.
getAttributeType All Returns a string value that represents the type of attribute.
getformat All Returns a string value that represents formatting options for the attribute.
getInitialValue boolean, optionset Returns the initial value for Boolean or optionset attributes.
getIsDirty All Returns a Boolean value indicating if there are unsaved changes to the attribute value.
getMax money, decimal, integer, double Returns a number indicating the maximum allowed value for an attribute.
getMaxLength string, memo Returns a number indicating the maximum length of a string or memo attribute.
getMin money, decimal, integer, double Returns a number indicating the minimum allowed value for an attribute.
getName All Returns a string representing the logical name of the attribute.
getOption optionset Returns an option object with the name matching the argument passed to the method.
getOptions optionset Returns an array of option objects representing the valid options for an optionset attribute.
getParent All Returns the entity object that is the parent to the attribute.
getPrecision money, decimal, integer, double Returns the number of digits allowed to the right of the decimal point.
getRequiredLevel All Returns a string value indicating whether a value for the attribute is required or recommended. See also setRequiredLevel
getSelectedOption optionset Returns the option object that is selected in an optionset attribute.
getSubmitMode All Returns a string indicating when data from the attribute will be submitted when the record is saved. See also setSubmitMode
getText optionset Returns a string value of the text for the currently selected option for an optionset attribute.
getUserPrivilege All Returns an array of privileges that contain Boolean values indicating if the user can create, read or update data values for an attribute.
getValue All Retrieves the data value for an attribute. See also setValue.
removeOnChange All Removes a function from the OnChange event hander for an attribute. See also addOnChange
setRequiredLevel All Sets whether data is required or recommended for the attribute before the record can be saved. See also getRequiredLevel.
setSubmitMode All Sets whether data from the attribute will be submitted when the record is saved. See also getSubmitMode.
setValue All Sets the data value for an attribute. See also getValue.

 

Xrm.Page.data.entity Methods

Method Description
addOnSave Sets a function to be called when the record is saved.
getDataXml Returns a string representing the xml that will be sent to the server when the record is saved.
getEntityName Returns a string representing the logical name of the entity for the record.
getId Returns a string representing the GUID id value for the record.
getIsDirty Returns a Boolean value that indicates if any fields in the form have been modified.
removeOnSave Removes a function from the OnSave event hander.
save Saves the record. This method has three possible parameters.

 

Form Event Handler Execution Context Reference

getContext Returns the Xrm.Page.context object.
getDepth Returns a value that indicates the order in which this handler is executed.
getEventArgs Returns an object with methods to manage the Save event.
getEventSource Returns a reference to the object that the event occurred on.
getSharedVariable Retrieves a variable set using setSharedVariable.
setSharedVariable Sets the value of a variable to be used by a handler after the current handler completes.

 

Xrm.Page.ui Methods

Method Description
close Closes the form.
getCurrentControl Returns the control object that currently has focus on the form.
getFormType Indicates the form context for the record.
getViewPortHeight Returns the height of the viewport in pixels.
getViewPortWidth Returns the width of the viewport in pixels.
refreshRibbon Causes the ribbon to re-evaluate data that controls what is displayed in it.

 

Xrm.Page.ui Control Methods

Method Control Types Description
addCustomView Lookup Adds a new view for the lookup dialog.
addOption Option Set Adds an option to an Option set control.
clearOptions Option Set Clears all options from an Option Set control.
getAttribute Standard, Lookup, OptionSet Returns the attribute that the control is bound to.
getControlType All Returns a value that categorizes controls.
getData Silverlight Web resources. Returns the value of the data query string parameter passed to a Silverlight Web resource. For more information, see setData.
getDefaultView Lookup Returns the ID value of the default lookup dialog view. For more information, see setDefaultView.
getDisabled All except SubGrid. Returns a value that indicates whether the control is disabled. For more information, see setDisabled.
getLabel All Returns the label for the control. See also setLabel
getName All Returns the name assigned to the control.
getParent All Returns a reference to the section object that contains the control.
getSrc IFrame, Web Resource Returns the current URL being displayed in an IFRAME. See also setSrc
getInitialUrl IFrame Returns the default URL that an IFrame control is configured to display.
getObject IFrame, Web resource Returns the object in the form representing an IFrame or Web resource.
getVisible All Returns a value that indicates whether the control is currently visible. For more information, see setVisible.
refresh SubGrid Refreshes the data displayed in a Sub-Grid
removeOption Option Sets Removes an option from an Option Set control.
setData Silverlight Web resources. Sets the value of the data query string parameter passed to a Silverlight Web resource. For more information, see getData.
setDefaultView Lookup Sets the default view for the lookup control dialog. For more information, see getDefaultView.
setDisabled All except Web Resources and SubGrid. Sets a value that indicates whether the control is disabled. See also getDisabled
setFocus All Sets the focus on the control.
setLabel All Sets the label for the control. For more information, see getLabel.
setSrc IFrame and Web Resource Sets the URL to be displayed in an IFrame. See also getSrc
setVisible All Sets a value that indicates whether the control is visible. For more information, see getVisible.

 

Xrm.Page.ui.controls Collection Methods

forEach Applies the action contained within a delegate function
get Returns one or more controls depending on the arguments passed.
Argument Return Value Type
None All the controls Array
String The control that matches the name Object
getLength Returns the number of controls in the collection

 

Xrm.Page.ui.navigation.items Collection Methods

forEach Applies the action contained within a delegate function.
get Returns one or more navigation items depending on the arguments passed.
Argument Return Value Type
None All the navigation items Array
String The item that matches the name Object
Number The item that matches the index Object
Delegate function (attribute, index) Any navigation items causing the delegate function to return true Array
getLength Returns the number of navigation items in the collection.

 

Xrm.Page.ui.formSelector.items Collection Methods

forEach Applies the action contained within a delegate function.
get Returns one or more roleForms.
Argument Return Value Type
None All the items Array
String The item that matches the name Object
Number The item that matches the index Object
Delegate function (attribute, index) Any items that cause the delegate function to return true Array
getLength Returns the number of roleForms in the collection.

 

Xrm.Page.ui.formSelector members

Member Type Description
items Collection A Collection of all the form items accessible to the current user.
getCurrentItem Method Returns a reference to the form currently being shown.

 

Xrm.Page.ui.tabs Collection

forEach Applies the action contained within a delegate function.
get Returns one or more tabs.
Argument Return Value Type
None All the tabs Array
String The tab that matches the name Object
Number The tab that matches the index Object
Delegate function (attribute, index) Any tabs that cause the delegate function to return true Array
getLength Returns the number of tabs in the collection.

 

Xrm.Page.data.entity.attributes Collection Methods

forEach Applies the action contained within a delegate function.
get Returns one or more attributes.
Argument Return Value Type
None All the attributes Array
String The attribute that matches the name Object
Number The attribute that matches the index Object
Delegate function (attribute, index) Any attributes that cause the delegate function to return true Array
getLength Returns the number of items in the collection.

 

Adding custom MIME types to web.config in IIS7 with Go Daddy

0
Filed under ASP.NET

For a few months I’ve been having issues with certain files on my Go Daddy web host.  I would upload them and then try to access them and I would get the dreaded default “Oops! Page not found” error.

After searching the Go Daddy community forums and some Microsoft IIS sites I realized that IIS7 does not serve up files with extensions it doesn’t know what to do with.  This is done for security reasons so that people cant hit your web.config file, or maybe an access database file (.mdb).

To get around this you can add custom MIME types to the IIS 7.0 manager but what if you don’t have access to this?  For example if you are on a shared web host account with Go Daddy and all you have is FTP access and web site control panel options which are limited.

The answer is to add some code to your web.config file to allow your site to recognize these different file extensions and pass the proper information in the header to the browser.

The code is very simple, just make sure you put it in the right place or you will break your entire website.  Backup your web.config file first if you have never modified it before.

Here is some sample code for adding the file types .m4v, .ogg, .oga, .ogv, and .webm.

NOTE: When I tried to add .mp4 to my list it caused the server to return an unknown error, I suspect maybe its because Go Daddy has mp4 already included on their list (see link below) I don’t know, but regardless mp4 caused my site to crash, everything else works fine.

<configuration>
    <system.webServer>
        <staticContent>
            <mimeMap fileExtension=".m4v" mimeType="video/m4v" />
            <mimeMap fileExtension=".ogg" mimeType="audio/ogg" />
            <mimeMap fileExtension=".oga" mimeType="audio/ogg" />
            <mimeMap fileExtension=".ogv" mimeType="video/ogg" />
            <mimeMap fileExtension=".webm" mimeType="video/webm"/>
        </staticContent>
    </system.webServer>
</configuration>

If you already have a web.config file you probably already have the system.webServer section. don’t add a second one just put the staticContent section inside the webServer section.

For more information see the IIS blog post and Go Daddy mime type listing url’s I found along the way.

http://blogs.iis.net/bills/archive/2008/03/25/how-to-add-mime-types-with-iis7-web-config.aspx
http://community.godaddy.com/help/article/3821

How do I make code run automatically AFTER my asp.net page has loaded on the screen (Timer control !)

1
Filed under ASP.NET
Tagged as , , , , , , ,

I’ve been working on redesigning a page for a web application that deals with massive number crunching which takes a long time to process.  The calculations and data gathering and all the manipulation of data takes about 6 seconds.  That might not seem like a long time but when you click a link to a page and that page takes 6-7 seconds to load it feels like an eternity and because the code is in the Page_Load method of the page nothing displays on the screen until the processing is complete.

So what do you do when you want a ton of data to process but not block your page from loading but also not require the user to have to press any buttons or interact with the page other than just loading it ?

Here’s the scenario and my result I implemented

About 85,000 rows of data are checked and calculated for company data, and average data.  Another 100,000 records are checked in another table for company hours.  All this data is then calculated against a formula to spit out a specific number to rank companies standings against an average over 7 years on a set of charts that are dynamically generated.

This process originally took about 30-45 seconds and would cause server timeouts and other errors.

After rewriting the SQL query and reducing the number of database calls it was still taking ~6 seconds.

So how to create a solution that would allow the page to load the initial body and then crunch all the data for the user with a nice little loading graphic while the processing happens in the background ?

The first step was to isolate all the code that was processing data and running in Page_Load off to another method.

Once you have that you have 2 options.  You can implement threading and pass the heavy processing off to another thread but that is much more messy and it requires you to know if any certain data relies on other data that might be processing in a separate thread and needs to complete first before other code runs etc etc.  It gets messy and hard to implement especially once you involve charting and some charts depend on data from other charts.

The simpler solution I found was to wrap up all my code that processes my data and draws my charts into its own method and then stick a Timer control on my page.

The timer control is very simple to use it takes an interval parameter for how long to wait before doing something and has an event for Timer_Tick that fires when the interval elapses.

The timer control runs in a loop and fires the Tick event each time it elapses.

<asp:Timer ID=”Timer1″ runat=”server” Interval=”100″ OnTick=”Timer1_Tick”>
</asp:Timer>

The interval is measured in milliseconds so 1000 is 1 second.

If you only want the timer to tick once and run its code in the event and stop then you can disable the timer as soon as it fires the event with Timer1.Enabled = false;

After that just call your method that has all your heavy processing and the timer will run after the elapsed interval time once the page has loaded.

To jazz it up and include an animated loading gif to show your users that data is loading for them you can add a div and put the image in it.  add attributes to the div for id and add runat=”server” to allow you to get a reference to the div from .cs code behind with the id.

EX: <div id=”loadingdiv” runat=”server”>

cs codebehind:
loadingdiv.Visible = false;

Simply show the div with the loading graphic by default when the page loads and when the timer_Tick method completes on the last line hide the div and show your data or charts.

Here’s 2 links to sites that allow you to generate your own custom animated loading .gif files

If there are other ways to entertain partial page rendering after page load with no user interaction I’d like to hear about them !

In what order do pages and events load in ASP.NET pages ?

0
Filed under ASP.NET, Seneca College
Tagged as , , ,

I’ve been working on two different asp.net web applications lately and I’ve been working with web user controls to handle searching so that I can reuse the search capability on multiple pages.

I have the user control show a Gridview with search results and allow the user to select a row to edit.  At this point I store the selected ID of the chosen row in session and have my container page grab the session value and populate a second table with details of the selected record to edit or what have you.

Seems simple enough right ?  Except for the fact that it sometimes wont work depending on how you build your page.  If you use a control to display your details for your selected record chances are it will work ok if it is connected to a DataSource to retrieve the data.

However if you build your own form with just a HTML table and some Textboxes on the page and expect to fill them in you will run into problems.  Most likely what will happen is you will select a record and nothing will happen on the first click.  After that clicking a second record will display the first record.  Clicking a third record will display the results from the second record etc etc.

Whats happening ? well your experiencing a problem with the order that the page is loaded and events fire.

Lets setup a common example:

  • A Master page which contains
    • An ASPX web form page which contains
      • A Web User Control inside the page which contains
        • A button to fire some code in a button_click event

Now lets say you click that button.  You would assume that the button code would run and then the page would post back and the master page code, page load code etc would run.  What really happens is much different.

Here are a few links to documentation on the page load life cycle.

What you will see from the first link provided is that the order is fairly complex.  What concerns us for this example is the Load event, control events, and PreRender event.

With a control like a Gridview, details view,or form view that is connected to a DataSource control you should be ok.  The button code will run first storing the session value and then the PreRender will fire which calls the databind() method for any DataSource controls. This means that the DataSource will grab the session value and update the view correctly.

The other method of trying to manually fill in some Textboxes with textbox1.text = “text” based off data you have in a DataTable from a TableAdapter that runs in the page_load for example will operate differently.  The Load methods are being called and retrieving your data and THEN the control button code is run storing the new data ID in session.

A simple example you can do to test what happens when is to make a page inside your master page and then create a web user control and put it in the new page and follow these steps.

  • Inside the code behind .cs pages add a Trace.Warn(“inside page_load”); line to each Page_load event.
  • Add the Page_Init and Page_PreRender events as well and add similar trace warning messages.
  • Turn on tracing in the ASPX page by adding Trace=”true” to the page directive (the first line of code in your aspx page)
<%@ Page Title=”" Language=”C#” MasterPageFile=”~/myMaster.master” AutoEventWireup=”true” CodeFile=”lifecycle.aspx.cs” Inherits=”_lifecycle” trace=”true”%>

You should end up with tracing information when you load up the page something like below.


aspx.page Begin Init
–> Inside user control Page_Init
–> Inside master page Page_Init
–> Inside lifecycle page Page_Init

aspx.page End Init
aspx.page Begin InitComplete
aspx.page End InitComplete
aspx.page Begin PreLoad
aspx.page End PreLoad
aspx.page Begin Load
–> Inside lifecycle page Page_Load
–> Inside master page Page_Load
–> Inside user control Page_Load

aspx.page End Load
aspx.page Begin LoadComplete
aspx.page End LoadComplete
aspx.page Begin PreRender
–> Inside lifecycle Page_PreRender
–> Inside master page Page_PreRender
–> Inside user control Page_PreRender

aspx.page End PreRender

Hopefully this helps you sort out your page loading problems !