Thursday, March 12, 2009

Controlling CRM window sizes


A while back I posted a simple script for changing the window size. Although ms current window settings fit most CRM forms there are situations were a form contains only a few fields and it only seem right to fit the window to its content.

Following is a simple and generic solution which can be used on all entities. The idea is to create an aspx page under the ISV folder that accepts an entity name and returns the script which moves and resizes the window.

The WindowSize.aspx page functions as a repository or a configuration page and does not require compilation. Simply Add the entity names to the Settings Dictionary and you’re done.

The script that calls this page should reside in each entity form (onload event). You may also put the script inside the global.js (not supported) if you don’t want to repeat this action more then once.

WindowSize.aspx code


<%@ Page Language="C#" %>
<%@ IMPORT NAMESPACE="System.Collections.Generic" %>
<script runat="server">
public static class Entity
{
public static Dictionary<String,String> Settings;

static Entity()
{
Settings = new Dictionary<String,String>();
//Parameters Order: Width,Height,Center,posX,posY
Settings.Add("account", "800,600,true");
//Add more entity configuration
Settings.Add("contact", "850,550,false,10,10");
}

public static String Script
{
get
{
return @"function adjWin(width,height,center,posX,posY){if(center==true){posX=(screen.availWidth-width)/2;posY=(screen.availHeight-height)/2;};window.resizeTo(width,height);window.moveTo(posX,posY);};";
}
}
}

public void Page_Load( object sender , EventArgs e)
{
string entityName = Request.QueryString["etn"] + "";
if (Entity.Settings.ContainsKey(entityName))
{
Response.Write(
String.Format("adjWin({0});{1}",
Entity.Settings[entityName],
Entity.Script)
);
}
}


</script>


Entity onload script / global.js


if (crmForm)
{
var wsScript = document.createElement("SCRIPT");
wsScript.src = SERVER_URL + "/ISV/WindowSize.aspx?etn=" + crmForm.ObjectTypeName
document.documentElement.childNodes[0].appendChild(wsScript);
}

5 comments:

Harsa said...

Adi,
I am trying to attach onLoad event in Global.js. I am not able to access the crmForm since it is not available still.

Appreciate your inputs.

Adi Katz said...

You need to wrap the code that loads the external reference in a function that is called when the page loads e.g.

window.attachEvent(“onload”,OnCrmPageLoad);
function OnCrmPageLoad()
{
if (crmForm)
{
var wsScript = document.createElement("SCRIPT");
wsScript.src = SERVER_URL + "/ISV/WindowSize.aspx?etn=" + crmForm.ObjectTypeName
document.documentElement.childNodes[0].appendChild(wsScript);
}
}

Anonymous said...

Adi, I'm using CRM 4. Where's Global.js script located?

Adi Katz said...

Under the _static/common folder. All static (_static) files such as script files are located under this folder.

Chalky said...

You should move before you resize. Resize can fail if right or bottom edge are beyond edge of screen (e.g. when enlarging window).