Friday 21 September 2012

Sample code to modify the users  last viewed form id :


CRM saves the Id of the last form that a user viewed using the UserEntityUISettings entity, and any UserEntityUISettings record can be easily updated to set the Last Viewed form for a specific user:

//retrieve the user UI settings for a specific user and a specified entity:
QueryExpression query = new QueryExpression(UserEntityUISettings.EntityLogicalName);
query.Criteria.AddCondition("ownerid", ConditionOperator.Equal, userId);
query.Criteria.AddCondition("objecttypecode", ConditionOperator.Equal, entityObjectTypeCode);
EntityCollection UISettingsCollection = service.RetrieveMultiple(query);
if (UISettingsCollection.Entities.Count > 0)
{
//update the last viewed formId:
UserEntityUISettings settings = (UserEntityUISettings)UISettingsCollection[0];
settings.LastViewedFormXml = "<MRUForm><Form Type=\"Main\" Id=\"f5cfab6a-d4c2-4519-b68f-6e7485432e29\" /></MRUForm>";
service.Update(settings);
}

ODATA  restrictions in mscrm 2011:

 

Operator

Restrictions

$expand
· Max expansion 6
$top
· Page size is fixed to max 50 records
· $top gives the total records returned across multiple pages
$skip
· When using with distinct queries, we are limited to the total (skip + top) record size = 5000.
· In CRM the distinct queries does not use paging cookie are and so we are limited by the CRM platform limitation to the 5000 record.
$select
· One level of navigation property selection is allowed I.e.
…/AccountSet?$select=Name,PrimaryContactId,account_primary_contact
…/AccountSet?$select=Name,PrimaryContactId,account_primary_
contact/LastName&$expand=account_primary_contact
$filter
· Conditions on only one group of attributes are allowed. By a group of attribute I am referring to a set of conditions joined by And/Or clause.
· The attribute group may be on the root entity
.../TaskSet?$expand=Contact_Tasks&$filter=Subject eq 'test' and Subject ne null
· (or) on the expanded entity.
.../TaskSet?$expand=Contact_Tasks&$filter=Contact_Tasks/FirstName eq '123‘
· Arithmetic, datetime and math operators are not supported
· Under string function we support Substringof, endswith, startswith
$orderby
· Order are only allowed on the root entity.
Navigation
· Only one level of navigation is allowed in any direction of a relationship. The relationship could be 1:N, N:1, N:N
Accessing context data  in silvrlight webresource like cr for javascript:



If your Silverlight web resource is designed to be viewed in an entity form, the form has an Xrm.Page.context object you can use to access contextual information.
If you need your Silverlight application to appear outside the context of the form you must configure an HTML web resource to provide this context information by adding a reference to the ClientGlobalContext.js.aspx page. After this reference is added, your Silverlight application can access contextual information in the same way it can in an entity form. The following sample shows how to call the getServerUrl function from the Xrm.Page.context object.

private string serverUrl = "";
ScriptObject xrm = (ScriptObject)HtmlPage.Window.GetProperty("Xrm");
ScriptObject page = (ScriptObject)xrm.GetProperty("Page");
ScriptObject pageContext = (ScriptObject)page.GetProperty("context");
serverUrl = (string)pageContext.Invoke("getServerUrl");

Friday 14 September 2012


Mscrm 2011 Plugin basic code snippsets:

Code to extract service objects:

IPluginExecutionContext context =

                (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

 

            // Get a reference to the organization service.

            IOrganizationServiceFactory factory =

                (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

            IOrganizationService service = factory.CreateOrganizationService(context.UserId);

 

            // Get a reference to the tracing service.

            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

 

Getting input parameters:

if (context.InputParameters.Contains("Target") &&

    context.InputParameters["Target"] is Entity)

{

    // Obtain the target entity from the input parmameters.

    Entity entity = (Entity)context.InputParameters["Target"];

Shared variable:

context.SharedVariables.Add("PrimaryContact", (Object)contact.ToString());

if (context.SharedVariables.Contains("PrimaryContact"))

            {

                Guid contact =

                    new Guid((string)context.SharedVariables["PrimaryContact"]);

 

                // Do something with the contact.

            }

Image reading:

if (context.PreEntityImages.Contains("PreImage") &&
12
    context.PreEntityImages["PreImage"] is Entity)

 

13
{
14
    preMessageImage = (Entity)context.PreEntityImages["PreImage"];

 

15
}

 

 

Query expression class:

//  Create query using querybyattribute

    QueryByAttribute querybyexpression = new QueryByAttribute("account");

    querybyexpression.ColumnSet = new ColumnSet("name", "address1_city", "emailaddress1");

//  Attribute to query

    querybyexpression.Attributes.AddRange("address1_city");

//  Value of queried attribute to return

    querybyexpression.Values.AddRange("Detroit");

   

//  Query passed to the service proxy

    EntityCollection retrieved = _serviceProxy.RetrieveMultiple(querybyexpression);

  

//  Iterate through returned collection

    foreach (var c in retrieved.Entities)

    {

       System.Console.WriteLine("Name: " + c.Attributes["name"]);

       System.Console.WriteLine("Address: " + c.Attributes["address1_city"]);

       System.Console.WriteLine("E-mail: " + c.Attributes["emailaddress1"]);

    } 

-___________________________X___________________________

//  Query using ConditionExpression and FilterExpression

ConditionExpression condition1 = new ConditionExpression();

condition1.AttributeName = "lastname";

condition1.Operator = ConditionOperator.Equal;

condition1.Values.Add("Brown");           

 

FilterExpression filter1 = new FilterExpression();

filter1.Conditions.Add(condition1);

 

QueryExpression query = new QueryExpression("contact");

query.ColumnSet.AddColumns("firstname", "lastname");

query.Criteria.AddFilter(filter1);

 

EntityCollection result1 = _serviceProxy.RetrieveMultiple(query);

 

_________________x________________

QueryExpression contactquery = new QueryExpression

                        {

                            EntityName="contact",

                            ColumnSet = new ColumnSet("firstname", "lastname", "contactid")

                        };