Sample function using SetStateRequest to deactivate record



// Get all the active records for a custom entity and deacitvate it

// In this case Custom Entity name is new_customentity and it has many to one relationship with contact record

 // So we are finding all the active custom entity records for a particular contact

 

// and deactivating it

 

private
void DeActivateCustomRecords(string contactID, IOrganizationService _service)
{
// setting contact id

ConditionExpression condition1 = new ConditionExpression();

condition1.AttributeName=“new_contactid”;

condition1.Operator =ConditionOperator.Equal;

condition1.Values.Add(contactID);


// and state code =0 -> active record

ConditionExpression condition2 = new ConditionExpression();

condition2.AttributeName =“statecode”;

condition2.Operator =ConditionOperator.Equal;

condition2.Values.Add(0);

 // And the condition

FilterExpression filterExpression = new FilterExpression();

filterExpression.AddCondition(condition1);

filterExpression.AddCondition(condition2);

filterExpression.FilterOperator =LogicalOperator.And;

 //Create a column set to get the custom entity records id
//we need them to disable the records

ColumnSet columns = new ColumnSet(“new_customentityid”);
// Create query expression.

QueryExpression query1 = new QueryExpression();

query1.ColumnSet = columns;

query1.EntityName =“new_customentity”;

query1.Criteria = filterExpression;

 try{

EntityCollection result= _service.RetrieveMultiple(query1);
foreach (Entity customEntityResult in result.Entities)
{

SetStateRequest setState = new SetStateRequest();

setState.EntityMoniker = new EntityReference();

setState.EntityMoniker.Id = customEntityResult.Id;

setState.EntityMoniker.Name = “new_customentity”;

setState.EntityMoniker.LogicalName = entityElgResult.LogicalName;

setState.State =new OptionSetValue();

setState.State.Value = 1;

setState.Status = new OptionSetValue();

setState.Status.Value = -1;

SetStateResponse setStateResponse = (SetStateResponse)_service.Execute(setState);

}

}
catch (Exception ex)
{
throw ex;

}

}

Hope it is useful !

Cannot read configuration file due to insufficient permissions


 

Was getting this issue in one of my ASP.NET application in IIS 7.

Gave Read Permission to IIS_IUSRS on the site.

This fixed the problem.

Good resource on Special Characters


Just found a good site,

http://www.the-art-of-web.com/javascript/escape/

I had this url in CRM 2011 for creating related entity record

 http://servername:port/orgname/main.aspx?etn=contact&extraqs=_CreateFromId%3d%257b554272F4-A8E1-DF11-AF7A-00155D045700%257d%26_CreateFromType%3d10001%26etc%3d2&pagetype=entityrecordBye.

quickly wanted to escape the special character, used the tool given at that site to get the url without any special character

 http://servername:port/orgname/main.aspx?etn=contact&extraqs=_CreateFromId={554272F4-A8E1-DF11-AF7A-00155D045700}&_CreateFromType=10001&etc=2&pagetype=entityrecord

Sample code to add a custom group to lead form in CRM 2011


For adding a custom group to a lead form we can make use of the following sample code.

Just replace the lead with your entity’s schema name to use it

<RibbonDiffXml>

<CustomActions>

<CustomAction
Id=Sample.lead.form.CustomGroup.MaxSize.CustomAction
Location=Mscrm.Form.lead.MainTab.Scaling._children
Sequence=120>

<CommandUIDefinition>

<MaxSize
Id=Sample.lead.form.CustomGroup.MaxSize
GroupId=MyLeadCustomGroup
Sequence=21
Size=LargeLarge/>

</CommandUIDefinition>

</CustomAction>

<CustomAction
Id=MyLeadCustomAction
Location=Mscrm.Form.lead.MainTab.Groups._children
Sequence=110>

<CommandUIDefinition>

<Group
Id=MyLeadCustomGroup
Title=My Lead Custom Group
Sequence=39
Template=Mscrm.Templates.Flexible2>

<Controls
Id=MyCustomControls>

<Button
Id=MyCustomButton1
Sequence=10
LabelText=My Custom Button1
ToolTipTitle=My Custom Button1
ToolTipDescription=My Custom Button1
TemplateAlias=o1/>

</Controls>

</Group>

</CommandUIDefinition>

</CustomAction>

</CustomActions>

<Templates>

<RibbonTemplates
Id=Mscrm.Templates/>

</Templates>

<CommandDefinitions>

</CommandDefinitions>

<RuleDefinitions>

<TabDisplayRules/>

<DisplayRules/>

<EnableRules/>

</RuleDefinitions>

<LocLabels/>

</RibbonDiffXml>

 

Hope it helps!

 

 

Showing Custom Button on Update in CRM 2011


Now let us built on our existing example of adding custom button and adding JavaScript to it.

https://nishantrana.wordpress.com/2010/11/04/adding-my-first-custom-button-in-crm-2011/

https://nishantrana.wordpress.com/2010/11/04/using-javascript-in-a-custom-button-in-crm-2011/

Get the solution file here

http://www.box.net/shared/m9q9otkqfg

Now we want our button to appear only in case of Update form, while creating a new record it shouldn’t appear.

For this we need to first define a DisplayRule in RuleDefinitions.

<RuleDefinitions>

<TabDisplayRules/>

<DisplayRules>


<DisplayRule
Id=MyRule>

<FormStateRule
State=Create
InvertResult=true />

</DisplayRule>

</DisplayRules>

<EnableRules/>

</RuleDefinitions>

This rule we will refer in our CommandDefinition for our button.

<CommandDefinitions>

<CommandDefinition
Id=Cmd_JavaScript>

<EnableRules>

<EnableRule
Id=Mscrm.Enabled/>

</EnableRules>


<DisplayRules>

<DisplayRule
Id=MyRule></DisplayRule>

</DisplayRules>

<Actions>

<JavaScriptFunction
Library=$webresource:new_MyJavaScript
FunctionName=HelloWorld>

</JavaScriptFunction>

</Actions>

</CommandDefinition>

</CommandDefinitions>

Now after importing and publishing it

The final RibbonDiffXml looks like this

<RibbonDiffXml>

<CustomActions>

<CustomAction
Id=CA_MyFirstButton
Location=Mscrm.Form.account.MainTab.Save.Controls._children
Sequence=31>

<CommandUIDefinition>

<Button
Id=B_MyFirstButton
Command=Cmd_JavaScript
LabelText=My First Button
ToolTipTitle=My First Button Tool Tip Title
ToolTipDescription=My First Button Tool Tip Description
TemplateAlias=o1
Image16by16=/_imgs/ribbon/saveandclose16.png
Image32by32=/_imgs/ribbon/saveandclose32.png/>

</CommandUIDefinition>

</CustomAction>

</CustomActions>

<Templates>

<RibbonTemplates
Id=Mscrm.Templates/>

</Templates>

<CommandDefinitions>

<CommandDefinition
Id=Cmd_JavaScript>

<EnableRules>

<EnableRule
Id=Mscrm.Enabled/>

</EnableRules>

<DisplayRules>

<DisplayRule
Id=MyRule>

</DisplayRule>

</DisplayRules>

<Actions>

<JavaScriptFunction
Library=$webresource:new_MyJavaScript
FunctionName=HelloWorld>

</JavaScriptFunction>

</Actions>

</CommandDefinition>

</CommandDefinitions>

<RuleDefinitions>

<TabDisplayRules/>

<DisplayRules>

<DisplayRule
Id=MyRule>

<FormStateRule
State=Create


InvertResult=true />

</DisplayRule>

</DisplayRules>

<EnableRules/>

</RuleDefinitions>

<LocLabels/>

</RibbonDiffXml>

Hope it helps !

Using JavaScript in a custom button in CRM 2011


Now let us built on our existing example of adding custom button.

https://nishantrana.wordpress.com/2010/11/04/adding-my-first-custom-button-in-crm-2011/

Now on click of the button I want to show alert message “Hello World” i.e. call JavaScript.

This is our existing Button Definition

<Button
Id=B_MyFirstButton
LabelText=My First Button
ToolTipTitle=My First Button Tool Tip Title
ToolTipDescription=My First Button Tool Tip Description
TemplateAlias=o1
Image16by16=/_imgs/ribbon/saveandclose16.png
Image32by32=/_imgs/ribbon/saveandclose32.png/>

(Add a new Jscript web resource to the solution named MyJavaScript)

function HelloWorld() {

alert(‘Hello World’);

}

Now add a Command attribute to Button

    <Button Id=”B_MyFirstButton


Command=”Cmd_JavaScript


                            LabelText=”My First Button ToolTipTitle=”My First Button Tool Tip Title ToolTipDescription=”My First Button Tool Tip Description TemplateAlias=”o1 Image16by16=”/_imgs/ribbon/saveandclose16.png Image32by32=”/_imgs/ribbon/saveandclose32.png“/>

Now we need to define this Command.

This is how we can define it

<CommandDefinitions>

<CommandDefinition
Id=Cmd_JavaScript>

<EnableRules/>

<DisplayRules/>

<Actions>


<JavaScriptFunction
Library=$webresource:new_MyJavaScript
FunctionName=HelloWorld>

</JavaScriptFunction>

</Actions>

</CommandDefinition>

</CommandDefinitions>

However if we zip and import this solution, the button would appear disabled.

To enable it we need to add EnableRule to it.

<CommandDefinitions>

<CommandDefinition
Id=Cmd_JavaScript>


<EnableRules>

<EnableRule
Id=Mscrm.Enabled/>

</EnableRules>

<DisplayRules/>

<Actions>

<JavaScriptFunction
Library=$webresource:new_MyJavaScript
FunctionName=HelloWorld>

</JavaScriptFunction>

</Actions>

</CommandDefinition>

</CommandDefinitions>

Now after importing and publishing it

The final RibbonDiffXml looks like this

<RibbonDiffXml>
<CustomActions>
<CustomAction
Id="CA_MyFirstButton"
Location="Mscrm.Form.account.MainTab.Save.Controls._children"
Sequence="31">
<CommandUIDefinition>
<Button
Id="B_MyFirstButton"
Command="Cmd_JavaScript"
LabelText="My First Button"
ToolTipTitle="My First Button Tool Tip Title"
ToolTipDescription="My First Button Tool Tip Description"
TemplateAlias="o1"
Image16by16="/_imgs/ribbon/saveandclose16.png"
Image32by32="/_imgs/ribbon/saveandclose32.png"/>
</CommandUIDefinition>
</CustomAction>
</CustomActions><Templates>
<RibbonTemplates
Id="Mscrm.Templates"/>
</Templates>
<CommandDefinitions>
<CommandDefinition
Id="Cmd_JavaScript">
<EnableRules>
<EnableRule
Id="Mscrm.Enabled"/>
</EnableRules>
<DisplayRules/>
<Actions>
<JavaScriptFunction
Library="$webresource:new_MyJavaScript"
FunctionName="HelloWorld">
</JavaScriptFunction>
</Actions>
</CommandDefinition>
</CommandDefinitions>
<RuleDefinitions>
<TabDisplayRules/>
<DisplayRules/>
<EnableRules/>
</RuleDefinitions>
<LocLabels/>
</RibbonDiffXml>

Hope it helps!