Tuesday, April 3, 2012

Dynamic Filtered Views in CRM2011

If you want to create a dynamic filterd lookup in crm2011

Ex:- If you have a account lookup & a contact lookup in a form & you want to filter & view the only contacts who's parentcustomer is equal to the selected account.

You can use the following

var defaultViewId;


// FUNCTION: formOnLoad
function formOnLoad() {

var lookupFieldName = 'new_contactid';
var accountFieldName = 'new_accountid';

defaultViewId = Xrm.Page.getControl(accountFieldName).getDefaultView();
setLookup(accountFieldName, lookupFieldName, false);

}

// FUNCTION: setLookup
function setLookup(accountFieldName, lookupFieldName, resetSelection) {

// Get the selected Account Id in the [accountFieldName] indicated control
var account = Xrm.Page.getAttribute(accountFieldName).getValue();

if (account != null) {
var accountid = account[0].id;
var accountname = account[0].name;

if (resetSelection == true) {
// reset old selection for Contact
Xrm.Page.getAttribute(lookupFieldName).setValue(null);
}

// use randomly generated GUID Id for our new view
var viewId = "{"+ guidGenerator() +"}";
var entityName = "contact";

// give the custom view a name
var viewDisplayName = "Active Contacts for " + accountname + "";

// find all contacts where [Parent Customer] = [Account indicated by AccountId]
// AND where [Statecode] = Active
var fetchXml = "" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"";

// build Grid Layout
var layoutXml = "
"object='1' " +
"jump='contactid' " +
"select='1' " +
"icon='1' " +
"preview='1'>" +
"
"id='contactid'>" +
"
"width='200' />" +
"
"width='250' />" +
"
"width='250' />" +
"
" +

"
";


// add the Custom View to the indicated [lookupFieldName] Control
Xrm.Page.getControl(lookupFieldName).addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
}
else {
// no Account selected, reset Contact Lookup View to the default view such that all Contacts are displayed for selection
Xrm.Page.getControl(lookupFieldName).setDefaultView(defaultViewId);
}
}

function guidGenerator() {
var S4 = function () {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
};
return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}

Object type DataMembers in WCF DataContract

When we want to create a datacontract with a DataMember of the type 'object' then we have to add the KnownType Attribute for that DataContract so that the serializer will know the types allowed for that generic type.

[KnownType(typeof(string))]
[KnownType(typeof(int))]
[KnownType(typeof(decimal))]
[KnownType(typeof(float))]
[KnownType(typeof(double))]
[KnownType(typeof(bool))]
[KnownType(typeof(char))]
[KnownType(typeof(DateTime))]
[KnownType(typeof(Guid))]
public class RMEntity
{
public ConcurrentDictionary attributes = new ConcurrentDictionary();


[DataMember]
public ConcurrentDictionary Attributes
{
get
{
return attributes;
}

set
{
attributes = value;
}
}



}

Create OrganizationServiceProxy in CRM2011 IFD

Creating OrganizationServiceProxy in crm2011 IFD vs AD different when it comes to setting credentials of the service proxy.

In AD :-

ClientCredentials clientCredentials=new ClientCredentials();

clientCredentials.Windows.ClientCredential = new NetworkCredential(userName, password,domain);

In IFD :-
clientCredentials.UserName.UserName =domain + @"\" + userName;
clientCredentials.UserName.Password = password;


OrganizationServiceProxy service = new Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy(organizationUri, HomeRealmUri, credentials, null);