PlugIn which Supports All DataTypes To Retrieve In MS CRM 2011

PlugIn which Supports All DataTypes To Retrieve In MS CRM 2011

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Query;

using Microsoft.Xrm.Sdk.Messages;

using Microsoft.Xrm.Sdk.Metadata;

using System.Web.Services.Protocols;

namespace Test_AllTypes

{

public class AllTypes : IPlugin

{

Guid AllTypesID;

Guid OwnerID;

Guid LookupID;

string Name = string.Empty;

string Text = string.Empty;

string Picklist = string.Empty;

Boolean Chk;

string option = string.Empty;

int Number;

Double FloatNumber;

Decimal DecimalNumber;

string Description = string.Empty;

Money Currency;

DateTime Date;

public void Execute(IServiceProvider serviceProvider)

{

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

if (context.InputParameters.Contains(“Target”) && context.InputParameters[“Target”] is Entity)

{

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

if (entity.LogicalName == “new_alltypes”)

{

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

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

AllTypesID = new Guid(entity.Attributes[“new_alltypesid”].ToString());

if (entity.Attributes.Contains(“ownerid”))

{

EntityReference OwnerId = (EntityReference)entity.Attributes[“ownerid”];

OwnerID = ((Microsoft.Xrm.Sdk.EntityReference)(entity.Attributes[“ownerid”])).Id;

}

if (entity.Attributes.Contains(“new_acclookup”))

{

EntityReference TaxType = (EntityReference)entity.Attributes[“new_acclookup”];

LookupID = ((Microsoft.Xrm.Sdk.EntityReference)(entity.Attributes[“new_acclookup”])).Id;

}

QueryExpression query = new QueryExpression

{

EntityName = “new_alltypes”,

ColumnSet = new ColumnSet(“new_name”, “ownerid”, “new_text”, “new_currency”, “new_acclookup”, “new_description”, “new_number”, “new_flotingnumber”, “new_decimalnumber”, “new_date”, “new_picklist”, “new_options”),

Criteria =

{

FilterOperator = LogicalOperator.And,

Conditions =

{

//The ObjectTypeCode must be specified, or else the query

//defaults to “email” instead of “template”.

new ConditionExpression

{

AttributeName = “new_alltypesid”,

Operator = ConditionOperator.Equal,

Values = {AllTypesID.ToString()}

}

}

}

};

foreach (Entity Demo in service.RetrieveMultiple(query).Entities)

{

Name = Demo.Attributes[“new_name”].ToString();

if (Demo.Attributes.Contains(“ownerid”))

{

OwnerID = ((Microsoft.Xrm.Sdk.EntityReference)(Demo.Attributes[“ownerid”])).Id;

}

Text = Demo.Attributes[“new_text”].ToString();

Picklist = ((Microsoft.Xrm.Sdk.OptionSetValue)(Demo.Attributes[“new_picklist”])).Value.ToString();

Chk = Convert.ToBoolean(Demo.Attributes[“new_options”]);

Number = Convert.ToInt32(Demo.Attributes[“new_number”]);

FloatNumber = Convert.ToDouble(Demo.Attributes[“new_flotingnumber”]);

DecimalNumber = Convert.ToDecimal(Demo.Attributes[“new_decimalnumber”]);

Currency = new Microsoft.Xrm.Sdk.Money(((Microsoft.Xrm.Sdk.Money)(Demo.Attributes[“new_currency”])).Value);

Date = Convert.ToDateTime(Demo.Attributes[“new_date”]);

Description = Demo.Attributes[“new_description”].ToString();

if (Demo.Attributes.Contains(“new_acclookup”))

{

LookupID = ((Microsoft.Xrm.Sdk.EntityReference)(Demo.Attributes[“new_acclookup”])).Id;

}

}

Entity AllTypes = new Entity(“new_alltypes”);

AllTypes.Attributes.Add(“new_name”, “Dup” + Name);

AllTypes.Attributes.Add(“ownerid”, new EntityReference(“systemuser”, OwnerID));

AllTypes.Attributes.Add(“new_text”, Text);

OptionSetValue option = new OptionSetValue(Convert.ToInt32(Picklist));

AllTypes.Attributes.Add(“new_picklist”, option);

AllTypes.Attributes.Add(“new_options”, Chk);

AllTypes.Attributes.Add(“new_number”, “” + Number);

AllTypes.Attributes.Add(“new_flotingnumber”, FloatNumber);

AllTypes.Attributes.Add(“new_decimalnumber”, DecimalNumber);

AllTypes.Attributes.Add(“new_currency”, Currency);

AllTypes.Attributes.Add(“new_date”, Date);

AllTypes.Attributes.Add(“new_description”, Description);

AllTypes.Attributes.Add(“new_acclookup”, new EntityReference(“account”, LookupID));

service.Create(AllTypes);

}

}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s