Advertisements

Archive

Archive for March, 2012

Associating entities with n:n relationship using Jscript

March 29, 2012 1 comment

Hi,

Below is the Jscript to associate two entity records, which are having (N:N) relationship.

function AssociateEntities(relationshipSchemaName, entity1Name, entity1Id, entity2Name, entity2Id) {

var xmlSoapBody = “<Execute xmlns=’http://schemas.microsoft.com/crm/2007/WebServices’>&#8221;

+ ”  <Request xsi:type=’AssociateEntitiesRequest’>”

+ ”    <Moniker1>”

+ ”      <Name xmlns=’http://schemas.microsoft.com/crm/2006/CoreTypes’>&#8221; + entity1Name + “</Name>”

+ ”      <Id xmlns=’http://schemas.microsoft.com/crm/2006/CoreTypes’>&#8221; + entity1Id + “</Id>”

+ ”    </Moniker1>”

+ ”    <Moniker2>”

+ ”      <Name xmlns=’http://schemas.microsoft.com/crm/2006/CoreTypes’>&#8221; + entity2Name + “</Name>”

+ ”      <Id xmlns=’http://schemas.microsoft.com/crm/2006/CoreTypes’>&#8221; + entity2Id + “</Id>”

+ ”    </Moniker2>”

+ ”    <RelationshipName>” + relationshipSchemaName + “</RelationshipName>”

+ ”  </Request>”

+ “</Execute>”;

ExecuteSOAP(“/MSCRMServices/2007/CrmService.asmx”, xmlSoapBody, “http://schemas.microsoft.com/crm/2007/WebServices/Execute&#8221;, false);

}

 function ExecuteSOAP(serviceUrl, xmlSoapBody, soapActionHeader) {

var xmlReq = “<?xml version=’1.0′ encoding=’utf-8′?>”

+ “<soap:Envelope xmlns:soap=’http://schemas.xmlsoap.org/soap/envelope/'&#8221;

+ ”  xmlns:xsi=’http://www.w3.org/2001/XMLSchema-instance'&#8221;

+ ”  xmlns:xsd=’http://www.w3.org/2001/XMLSchema’>&#8221;

+ GenerateAuthenticationHeader()

+ ”  <soap:Body>”

+ xmlSoapBody

+ ”  </soap:Body>”

+ “</soap:Envelope>”;

var httpObj = new ActiveXObject(“Msxml2.XMLHTTP”);

httpObj.open(“POST”, serviceUrl, false);

httpObj.setRequestHeader(“SOAPAction”, soapActionHeader);

httpObj.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8”);

httpObj.setRequestHeader(“Content-Length”, xmlReq.length);

httpObj.send(xmlReq);

var resultXml = httpObj.responseXML;

var errorCount = resultXml.selectNodes(“//error”).length;

if (errorCount != 0) {

var msg = resultXml.selectSingleNode(“//description”).nodeTypedValue;

alert(“Error while association; ” + msg);

return null;

} else {

alert(“Success!!!”);

return resultXml;

}

}

How do I call these functions :-

  • Let’s assume, you have N:N relationship between entityA & entity with relationship name (“new_entityAentityB”)

    var relationshipName = ” new_entityAentityB “;

var entityA_SchemaName = “new_entitya”;

var entityB_SchemaName = “new_entityb”;

var entiyA_Id = “{}”; //GUID of entityA record

var entiyB_Id = “{}”; //GUID of entityB record

AssociateEntities(relationshipName, entityA_SchemaName, entiyA_Id, entityB_SchemaName, entiyB_Id);

Note :- If you copy above functions, please replace double quote & single quote symbols with keyboard symbols.

Hope it helps 🙂

Advertisements

Could not load type System.ServiceModel.Activation.HttpModule exception

Hi,

I was getting below exception, When I hosted my asp.net web application on IIS and tried to browse my .aspx page.

Could not load type error

Could not load type error

Reason :-

  • The reason could be, when the IIS is installed after the installation of .NET Framework 4, or if the 3.0 version of the WCF Http Activation module is installed after installing IIS and .NET Framework 4.

Fix :-

  • To resolve this issue follow bellow steps
    • Open Command Prompt (Run -> cmd)
    • Navigate to the path “%windir%\Microsoft.NET\Framework\v4.0.30319”
    • Type “aspnet_regiis.exe /iru” and press enter button.
    Run Command Aspnet_iis.exe

    Run Command Aspnet_iis.exe

    • Restart the IIS

Hope it helps 🙂

Retrieve multiple records using OData & JQuery in CRM 2011

March 26, 2012 5 comments

Hi,

Below are the Jscript functions to retrieve multiple records using OData & JQuery.

function retrieveMultiple(odataSetName, select, filter, successCallback) {

var serverUrl = Xrm.Page.context.getServerUrl();

var ODATA_ENDPOINT = “/XRMServices/2011/OrganizationData.svc”;

var odataUri = serverUrl + ODATA_ENDPOINT + “/” + odataSetName + “?”;

if (select) {

odataUri += “$select=” + select + “&”;

}

if (filter) {

odataUri += “$filter=” + filter;

}

//Asynchronous AJAX function to Retrieve CRM records using OData

$.ajax({

type: “GET”,

contentType: “application/json; charset=utf-8”,

datatype: “json”,

url: odataUri,

beforeSend: function (XMLHttpRequest) {

//Specifying this header ensures that the results will be returned as JSON.

XMLHttpRequest.setRequestHeader(“Accept”, “application/json”);

},

success: function (data, textStatus, XmlHttpRequest) {

if (successCallback) {

if (data && data.d && data.d.results) {

successCallback(data.d.results, textStatus, XmlHttpRequest);

}

else if (data && data.d) {

successCallback(data.d, textStatus, XmlHttpRequest);

}

else {

successCallback(data, textStatus, XmlHttpRequest);

}

}

},

error: function (XmlHttpRequest, textStatus, errorThrown) {

if (XmlHttpRequest && XmlHttpRequest.responseText) {

alert(“Error while retrieval ; Error – ” + XmlHttpRequest.responseText);

}

}

});

}

function readRecordsOnSuccess(data, textStatus, XmlHttpRequest) {

// Loop through the retrieved records

for (var indx = 0; indx < data.length; indx++) {

alert(“Name – ” + data[indx].name);

}

}

  • Copy & Paste above methods to a new .jscript file
  • Replace double quotes(“) symbols with your keyboard double quotes
  • Add the new .jscript file as web resource to CRM application
  • Add “Jquery” & json” helper classes as web resources

How do I call this method :-

  • In my sample below, I will show you how to read “Contacts” for an “Account”

function retrieveContactsByAccountId() {

// Pass ‘Contact’ set name since we are reading Contacts

var oDataSetName = “ContactSet”;

// Column names of ‘Contact’ (Pass * to read all columns)

var columns = “FirstName,LastName”;

// Read Account Guid

var accountId = Xrm.Page.data.entity.getId()

// Prepare filter

var filter = “AccountId/Id eq guid'” + accountId + “‘”;

retrieveMultiple(oDataSetName, columns, filter, readRecordsOnSuccess);

}

Note :- Import “ODataQueryDesigner” solution to prepare the Filters easily.

Hope it helps 🙂

Refreshing Sub grid and Main grid using Jscript in CRM 2011

Hi,

Below is the script to refresh the sub-grid

    var mySubGrid = Xrm.Page.ui.controls.get(“{sub-grid name}“);

if (mySubGrid) {

mySubGrid.refresh();

}

To get the sub grid name

  • Open the entity customization form
  • In designer, double click on subgrid
  • In the dialog window, copy the “Name” value
Get Sub grid  name

Get Sub grid name

 

To refresh the main grid, use below script

    if (crmGrid) {

crmGrid.Refresh();

}

Hope it helps 🙂

Adding New Tab & Group & Buttons to CRM form Ribbon

March 17, 2012 3 comments

Hi,

Here are the steps to add new Tab, Group and Buttons to the ‘Contact’ form and call .jscript functions on button click.

Ribbon Customizations - Contact Form

Ribbon Customizations - Contact Form

Steps :-

  • Create a new .jscript file “contact.js” with below functions

function fnA() {

alert(“Button \”A\” clicked!!!”);

}

function fnB() {

alert(“Button \”B\” clicked!!!”);

}

  • Open CRM application and add the “contact.js” as web resource
  • Create a new unmanaged solution and add “Contact” entity and “contact.js” web resource
  • Export the solution to the local folder
  • Unzip the folder
  • Open “customizations.xml” in Visual studio
  • Navigate to “<Entities><Entity>Contact”
  • Go to  <RibbonDiffXml> node and replace with below XML

 

<RibbonDiffXml>

<CustomActions>

<CustomAction Id=”Sample.Form.contact.CustomTab.CustomAction” Location=”Mscrm.Tabs._children” Sequence=”40″>

<CommandUIDefinition>

<Tab Id=”Sample.Form.contact.CustomTab” Command=”Sample.Form.contact.CustomTab”

Title=”$LocLabels:Sample.contact.CustomTab.Title”

Description=”$LocLabels:Sample.contact.CustomTab.Description” Sequence=”40″>

<Scaling Id=”Sample.Form.contact.CustomTab.Scaling”>

<MaxSize Id=”Sample.Form.contact.CustomTab.CustomGroup.MaxSize”

GroupId=”Sample.Form.contact.CustomTab.CustomGroup” Sequence=”10″ Size=”LargeMedium” />

</Scaling>

<Groups Id=”Sample.Form.contact.CustomTab.Groups”>

<Group Id=”Sample.Form.contact.CustomTab.CustomGroup”

Command=”Sample.Form.contact.CustomGroup”

Sequence=”10″ Title=”$LocLabels:Sample.contact.CustomTab.CustomGroup.Title” Template=”Mscrm.Templates.3.3″>

<Controls Id=”Sample.Form.contact.CustomTab.CustomGroup.Controls”>

<Button Id=”Sample.Form.contact.CustomTab.CustomGroup.ButtonA”

ToolTipTitle=”$LocLabels:Sample.contact.CustomTab.CustomGroup.ButtonA.LabelText”

ToolTipDescription=”$LocLabels:Sample.contact.CustomTab.CustomGroup.ButtonA.ToolTipDescription”

Command=”Sample.Form.contact.ButtonA” Sequence=”10″

LabelText=”$LocLabels:Sample.contact.CustomTab.CustomGroup.ButtonA.LabelText”

Alt=”$LocLabels:Sample.contact.CustomTab.CustomGroup.ButtonA.LabelText”

Image16by16=”/_imgs/ribbon/runreport16.png” Image32by32=”/_imgs/ribbon/runreport32.png” TemplateAlias=”o1″ />

<Button Id=”Sample.Form.contact.CustomTab.CustomGroup.ButtonB”

ToolTipTitle=”$LocLabels:Sample.contact.CustomTab.CustomGroup.ButtonB.LabelText”

ToolTipDescription=”$LocLabels:Sample.contact.CustomTab.CustomGroup.ButtonB.ToolTipDescription”

Command=”Sample.Form.contact.ButtonB” Sequence=”20″

LabelText=”$LocLabels:Sample.contact.CustomTab.CustomGroup.ButtonB.LabelText”

Alt=”$LocLabels:Sample.contact.CustomTab.CustomGroup.ButtonB.LabelText”

Image16by16=”/_imgs/ribbon/runreport16.png” Image32by32=”/_imgs/ribbon/runreport32.png” TemplateAlias=”o1″ />

</Controls>

</Group>

</Groups>

</Tab>

</CommandUIDefinition>

</CustomAction>

</CustomActions>

<Templates>

<RibbonTemplates Id=”Mscrm.Templates”></RibbonTemplates>

</Templates>

<CommandDefinitions>

<CommandDefinition Id=”Sample.Form.contact.CustomGroup”>

<EnableRules>

<EnableRule Id=”Mscrm.Enabled” />

</EnableRules>

<DisplayRules />

<Actions />

</CommandDefinition>

<CommandDefinition Id=”Sample.Form.contact.ButtonA”>

<EnableRules>

<EnableRule Id=”Mscrm.Enabled” />

</EnableRules>

<DisplayRules />

<Actions>

<JavaScriptFunction Library=”$webresource:new_contact” FunctionName=”fnA”></JavaScriptFunction>

</Actions>

</CommandDefinition>

<CommandDefinition Id=”Sample.Form.contact.ButtonB”>

<EnableRules>

<EnableRule Id=”Mscrm.Enabled” />

</EnableRules>

<DisplayRules />

<Actions>

<JavaScriptFunction Library=”$webresource:new_contact” FunctionName=”fnB”></JavaScriptFunction>

</Actions>

</CommandDefinition>

</CommandDefinitions>

<RuleDefinitions>

<TabDisplayRules>

<TabDisplayRule TabCommand=”Sample.Form.contact.CustomTab”>

<EntityRule EntityName=”contact” Context=”Form” AppliesTo=”PrimaryEntity” />

</TabDisplayRule>

</TabDisplayRules>

<DisplayRules />

<EnableRules>

</EnableRules>

</RuleDefinitions>

<LocLabels>

<LocLabel Id=”Sample.contact.CustomTab.Title”>

<Titles>

<Title languagecode=”1033″ description=”My Tab” />

</Titles>

</LocLabel>

<LocLabel Id=”Sample.contact.CustomTab.Description”>

<Titles>

<Title languagecode=”1033″ description=”My Tab” />

</Titles>

</LocLabel>

<LocLabel Id=”Sample.contact.CustomTab.CustomGroup.Title”>

<Titles>

<Title languagecode=”1033″ description=”My Group” />

</Titles>

</LocLabel>

<LocLabel Id=”Sample.contact.CustomTab.CustomGroup.ButtonA.LabelText”>

<Titles>

<Title languagecode=”1033″ description=”A” />

</Titles>

</LocLabel>

<LocLabel Id=”Sample.contact.CustomTab.CustomGroup.ButtonA.ToolTipDescription”>

<Titles>

<Title languagecode=”1033″ description=”Button A” />

</Titles>

</LocLabel>

<LocLabel Id=”Sample.contact.CustomTab.CustomGroup.ButtonB.LabelText”>

<Titles>

<Title languagecode=”1033″ description=”B” />

</Titles>

</LocLabel>

<LocLabel Id=”Sample.contact.CustomTab.CustomGroup.ButtonB.ToolTipDescription”>

<Titles>

<Title languagecode=”1033″ description=”Button B” />

</Titles>

</LocLabel>

</LocLabels>

</RibbonDiffXml>

You can download the complete solution from this location

Download Solution

Hope it helps 🙂

“Translations file is invalid” error while importing translations in CRM 2011

March 17, 2012 2 comments

Hi,

The other day, When I tried to import Translations (i.e., RajTranslations.zip folder with ‘Content_Types.xml & CrmTranslations.xml’ files), I was getting below exception

Translations file is invalid. The compressed file must contain the following files in your root “[Content_Types].xml” and “CrmTranslations.xml”

Reason:-

  • The reason for this error was I zipped the “RajTranslations” folder, after I modified the “CrmTranslations.xml” file in it.
  • When I zip the folder it creates a “RajTranslations.zip”  file with “RajTranslations” folder at root and 2 files ‘Content_Types.xml’ & ‘CrmTranslations.xml’
  • So, when you import the .zip file, CRM does not find the files at root since the root was folder

Fix :-

  • You just need to select the two files and zip them rather than zip the folder.
Select the files and zip

Select the files and zip

Hope it helps 🙂

Excel completed file level validation and repair – message while opening excel file

Hi,

In one of my XRM implementation i used OpenXML.sdk to read/write excel files.

I was getting below screen while opening the created excel file from .aspx page.

Repairing file

Repairing file

Here is the code Iused to pop the excel file from .aspx page

Response.ClearHeaders();

Response.ClearContent();

Response.ContentType = “application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml”;

Response.AddHeader(“Content-Disposition”, “sample.xlsx”);

Response.BinaryWrite(outputFileBytes);

Response.Flush();

Response.End();

Fix  :-

  • There might be many reasons for this warning message
  • In my case, I mistakenly added <script> tag to my ‘Response’ object
  • So when you open the file, Excel application repairs the content by removing the <script> tag
  • This is the reason you get the message window
  • To verify whether the excel file corrupted
    • Save the Excel file locally
    • Open the Excel file with notepad
    • Check if any <script> tag exists

Open Excel using notepad

Hope it helps 🙂