Form programming methods

The following table lists the form programming methods available for updated entities only.

Method Description
Xrm.Page.context.client.getClient Returns a value to indicate which client the script is executing in.
Xrm.Page.context.client.getClientState Returns a value to indicate the state of the client.
Xrm.Page.context.getUserName Returns the name of the current user.
Xrm.Page.data.entity.getPrimaryAttributeValue Gets a string for the value of the primary attribute of the entity.
Xrm.Page.data.refresh Asynchronously refreshes the data of the form without reloading the page.
Xrm.Page.data.save Saves the record asynchronously with the option to set callback functions to be executed after the save operation is completed.
Xrm.Page.data.entity attribute.getIsPartyList Determines whether a lookup attribute represents a partylist lookup.
Xrm.Page.ui control.clearNotification Removes a message already displayed for a control.
Xrm.Page.ui control.setNotification Displays a message near the control to indicate that data is not valid.
Xrm.Page.ui.clearFormNotification Use this method to remove form level notifications.
Xrm.Page.ui.setFormNotification Use this method to display form level notifications.
Xrm.Page.ui control.addCustomFilter Use fetchXml to add additional filters to the results displayed in the lookup. Each filter will be combined with any previously added filters as an ‘AND’ condition.
Xrm.Page.ui control.setShowTime Specifies whether a date control should show the time portion of the date.
Xrm.Utility.alertDialog Displays a non-blocking alert dialog with a callback function.
Xrm.Utility.confirmDialog Displays a non-blocking confirm dialog with different callbacks depending on the button clicked by the user.
Advertisements

PreSearch Event in Microsoft Dynamics 365

PreSearch event for updated entities

The new PreSearch event occurs just before the search dialog box opens when you set a lookup value. This event does not have user interface to set an event handler in the application; it can only be set using the Xrm.Page.ui control.addPreSearch method. Use this event with the addCustomFilter, addCustomView and setDefaultView methods to control the views opened when people search for a record to set as the value of a lookup field.

Use this method to apply changes to lookups based on values current just as the user is about to view results for the lookup.

Xrm.Page.getControl(arg).addPreSearch(handler)
Arguments
Type: Function to add.

Remarks
This method is only available for Updated entities.

The argument is a function that will be run just before the search to provide results for a lookup occurs. You can use this handler to call one of the other lookup control functions and improve the results to be displayed in the lookup.

 

Use this method to remove event handler functions that have previously been set for the PreSearch event.

Xrm.Page.getControl(arg).removePreSearch(handler)
Arguments
Type: Function to remove.

Remarks
This method is only available for Updated entities.

Pass parameters to Plugin Using Secure or Unsecure Configuration Using Plugin Registration Tool in MS CRM 2011

Pass parameters to Plugin Using Secure or Unsecure Configuration to Plugin:

When you start developing plugins, you often need an input parameter or a configuration for the plugin execution which can be easily updated without having to re-compile and/or re-register the plugin.

For example , i want to read some of the data which may change every time. Rather than creating the custom entity to read the Configuration kind of thing better to use this method to read the Parameters.

Use the plugin step “Configuration”
When you register a plugin step, there is a field where you can specify some configuration parameters for the plugin execution as below:Image

Then in the Constructor of your plugin class you will get the configuration value which you can use later in the Execute method:

Image2

 

In the Quickwatch you can watch the total configuration as follows:

Image1

 

have a glance below for plugin code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using System.Xml;

namespace ReadPluginConfgInPlugins
{
public class PluginConfg : IPlugin
{
private readonly string _unsecureString;
private readonly string _secureString;

public PluginConfg(string unsecureString, string secureString)
{
if (String.IsNullOrWhiteSpace(unsecureString) || String.IsNullOrWhiteSpace(secureString))
{
try
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(unsecureString);
Guid DefaultQueueGuid = PluginConfiguration.GetConfigDataGuid(doc, “Contact Guid”);
string ContactFullName = PluginConfiguration.GetConfigDataString(doc, “Full Name”);
int MobileNumber = PluginConfiguration.GetConfigDataInt(doc, “Mobile Number”);
bool Gender = PluginConfiguration.GetConfigDataBool(doc, “Gender 0 refer to Male”);
}
catch (Exception ex)
{
throw new Exception(“SoapException” + ex.Message + “########” + ex.StackTrace + “$$$$Inner Exception” + ex.InnerException);
}
}
}

public void Execute(IServiceProvider serviceProvider)
{
//Extract the tracing service for use in debugging sandboxed plug-ins.
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));

// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

// For this sample, execute the plug-in code only while the client is online.
tracingService.Trace(“AdvancedPlugin: Verifying the client is not offline.”);
if (context.IsExecutingOffline || context.IsOfflinePlayback)
return;

// The InputParameters collection contains all the data passed
// in the message request.
if (context.InputParameters.Contains(“Target”) &&
context.InputParameters[“Target”] is Entity)
{
Entity entity = (Entity)context.InputParameters[“Target”];

}

}
}

}

For this approach also have a Pros and Cons.

PROS:

  • The step configuration is solution-aware so it will be automatically transported with the plugin step.

CONS:

  • You need to use the plugin registration tool or another application to update the step configuration.
  • The configuration is step-specific so you have to provide it and/or update it for every step even if the value is the same for all the steps (the configuration is on each step instead of the assembly or plugin type).
  • the configuration is just an attribute of the plugin step so you cannot control privileges on the configuration independently from privileges on plugin step entity.

*. Use the plugin step “Secure Configuration”
This is similar to the step Configuration except that the configuration data is stored in a separate entity which can be secured.
PROS:

  • The configuration data can be secured as any other entity using the CRM security model. This is useful when the configuration contains sensitive information such as passwords.

CONS:

  • Secure configuration is not solution aware so you will need to configure it for each environment.
  • You need to use the plugin registration tool or another application to update the step configuration.
  • The configuration is step-specific so you have to provide it and/or update it for every step even if the value is the same for all the steps (the configuration is on each step instead of the assembly or plugin type).

*. Use a Web Resource
You can store configuration information in web resources, for example you might have some XML configuration stored in a web resource and have your plugin read the web resource each time it executes.

PROS:

  • Web resources are solution aware and the GUID is preserved across environments so you can hardcode the web resource GUID in your plugin code. You can transport the web resource and the plugin in the same solution.
  • Can be easily updated using the CRM UI.

CONS:

  • You cannot secure the configuration since it depends on the web resource access privileges and most users will need at least read access to web resources.

Hope this may help you.

 

Regards,

Update Ownerid in the CRM Form

In some times , we may need to update the Owner Lookup with the Current logged in user in MS CRM 2011.

Wrong Method :

entity[“ownerid”] = new EntityReference(“systemuser”,Guid);

Preferred Method :

For this requirement we need to assign the Record to Current Logged in User in the Owner Look up.

Have a look the below code , it works fine for me.

AssignRequest assign = new AssignRequest
{
Assignee = new EntityReference(“systemuser”,Guid),
Target = new EntityReference(“<entity name>”, Guid)
};
// Execute the Request
service.Execute(assign);

Regards,

Share a Record to The Specific Team In MS CRM 2011 using C# Code

Share a Record to The Specific Team In MS CRM 2011 using C# Code

For.eg. to share a incident record to a team

//Paste the Below code in your plugin or any custompage

private void ShareRecordToBuTeam(IOrganizationService service, string SharedRecordEntitySchemaName, Guid SharedRecordGuid , string TeamLogicalName,Guid TeamGuid)
{
GrantAccessRequest grantShareingRequest = new GrantAccessRequest()
{
Target = new EntityReference(SharedRecordEntitySchemaName,SharedRecordGuid),
PrincipalAccess = new PrincipalAccess()
{
Principal = new EntityReference(TeamLogicalName,TeamGuid),
AccessMask = AccessRights.ReadAccess
}
};
GrantAccessResponse AccessResponse = (GrantAccessResponse)service.Execute(grantShareingRequest);
}

//Pass the parameter to the Function as below

Need

ShareRecordToBuTeam(service,”<entity Logical Name>”,<SharedRecordGUID>, “<team logical name>”,<TeamGuid>);

 

Cheers,

SetState Dynamic Entity – Using MS CRM 2011

SetStae For Entity :

For e.g. While Reactivate the Incident Entity “SetState”

Register this Plugin in Both “SetState” and “SetStateDynamicEntity” on incident entity

Entity caseEntity;
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

try
{
if (context.InputParameters.Contains(“EntityMoniker”))
{
// Work with the Moniker
EntityReference targetEntity = (EntityReference)context.InputParameters[“EntityMoniker”];
if (targetEntity.LogicalName != “incident”)
{ return; }
IOrganizationService service = factory.CreateOrganizationService(context.UserId);

switch (context.MessageName)
{
// Check if entity status is changed
case “SetStateDynamicEntity”:
if (context.InputParameters.Contains(“State”))
{
EntityReference CaseEntityRef = (EntityReference)context.InputParameters[“EntityMoniker”];

if (((OptionSetValue)context.InputParameters[“State”]).Value == 0)
{

U can write any logic here

}
}
break;
}
service.Update(caseEntity);
}
}
catch (Exception e)
{
throw new InvalidPluginExecutionException(“An error occured for SetState plugin ” + e.Message + e.InnerException);
}

}

 

Cheers,