How TO :Get Text Value of an OptionSetValue Using LINQ in Microsoft Dynamics CRM 2011

Hi,

i have been always trying to get the label of an option set value by the way described in the following Article  which uses RetrieveAttributeRequest and passed the value which we want to get the label for.

CRM SDK Provides another new way instead of the one described in the above link which is using LINQ To CRM Queries.

The basics are as follows. 

1. Use CrmSvcUtil.exe (comes with the SDK) to get a context that you can use with the CRM LINQ provider.

2. Use LINQ to pull the entity attribute FormattedValues for a specific attribute of type OptionSetValue.  The example below is pulling based on a specific entity ID so it pulls the specific text value of the optionset in this instance.

var strstringvalue = (from ca in context.CreateQuery<CampaignActivity>() where ca.ActivityId == campaignResponse.new_CampaignActivityId.Id select ca.FormattedValues["new_campaignactivitytype"]);

How To : Get the Label Value for an OptionSet Using RetrieveAttribute Request in CRM using C# SDK

The following code shows how to to the get label of an option value.

Code:

private string GetCRMOptionSetLabel(IOrganizationService service, string entityname, string optionsetname, int value)
{
RetrieveAttributeRequest reqOptionSet = new RetrieveAttributeRequest();
reqOptionSet.EntityLogicalName = entityname;
reqOptionSet.LogicalName = optionsetname;
RetrieveAttributeResponse resp = (RetrieveAttributeResponse)service.Execute(reqOptionSet);
PicklistAttributeMetadata opdata = (PicklistAttributeMetadata)resp.AttributeMetadata;
var option = opdata.OptionSet.Options.FirstOrDefault(o => o.Value == value);
return option.Label.LocalizedLabels.FirstOrDefault().Label;

}

How To : Retrieve the Marketing Lists Count Associated With An Event in CRM Using JavaScript

Hi, in this article am gonna show how to retrieve marketing lists count associated with certain event in CRM Using JavaScript.

Code:

function retrieveEventAssociatedLists(EventID) {

var listsCount = 0;

var serverUrl = "http://" + document.location.href.split("/")[2];

var ODataPath = serverUrl + "/OrganizationName/XRMServices/2011/OrganizationData.svc";

var retrieveEventListsReq = new XMLHttpRequest();

retrieveEventListsReq.open("GET", ODataPath + "/new_new_event_listSet?$filter=new_eventid eq guid'" + EventID + "'",   false);

retrieveEventListsReq.setRequestHeader("Accept", "application/json");

retrieveEventListsReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");

retrieveEventListsReq.send();

if (retrieveEventListsReq.status == 200) {

//Success

var retrievedEventLists = JSON.parse(retrieveEventListsReq.responseText).d;

listsCount = retrievedEventLists.results.length;

}

return listsCount;

}

How To : Retrieve Web Resource Contents in CRM using C# SDK

Hi, in this article i am gonna show how to retrieve the content of an "HTML Web Resource" in Microsoft Dynamics CRM using C# SDK

You need to perform 2 steps 

1. perform a "RetrieveMultiple" call to pull back your web resource entity.
2. Decode the "content" property from base-64 to a string.

code :

RetrieveMultipleRequest RetrieveWebResources= new RetrieveMultipleRequest();
RetrieveMultipleResponse resp = new RetrieveMultipleResponse();
Entity webresource = new Entity();

QueryExpression query = new QueryExpression()
{
EntityName = "webresource",
ColumnSet = new ColumnSet("content"),
Criteria = new FilterExpression
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression
{
AttributeName = "name",
Operator = ConditionOperator.Equal,
Values = { "web resource name"}
}
}
}
};

RetrieveWebResources.Query = query;
resp = (RetrieveMultipleResponse)_serviceproxy.Execute(RetrieveWebResources);
webresource= resp.EntityCollection.Entities[0];

byte[] b = Convert.FromBase64String(webresource["content"].ToString());

string strHTML = System.Text.Encoding.UTF8.GetString(b);

 

Dynamics CRM Common Workflow Activities Library

Hi ,

You can now download the Common Workflow Activities Library and save time by not implementing components repeatedly in each project such as

  • Apply Routing Rules
  • Share Record With Team
  • Check If User is in Role
  • Check If User is in Team

Download Link : https://msdyncrmworkflowtools.codeplex.com/

Dynamcis CRM Execute Sync Workflows In Order

Scenario & Problem:

I was performing an upgrade to CRM 2016 and one of the customer requirement was to convert all plugins to run time

workflows,multiple plugins were registered for the same event on the same entity by benefiting from execution order feature in

plugin which doesn't exist in run time workflows.

 

Solution:

let's say you have 2 WFs,

  • configure the second to be called on demand

  • call it from the first after finalizing all the logic

Connect To Dynamcis 365 Online using Power Shell

Hi,

In this post I am going to show a basic example using CRM SDK through power shell to connect to CRM .

Power shell is so useful when you need to execute operations through SDK without Visual studio installed in your environment.

Code :

clear;

#navigate to SDK Folder
cd C:\CRMSDK\SDK\Bin

#execute script
.\RegisterXRMTooling.ps1

#add crm snappin
Add-PSSnapin Microsoft.Xrm.Tooling.Connector

#Get Tool Alias
Set-Alias installUtil $env:windir\Microsoft.NET\Framework64\v4.0.30319\installutil.exe

#copy to gac
installUtil /u Microsoft.Xrm.Sdk.dll;

#Initialize connection
$CRMConn =Get-CrmConnection –ConnectionString "Url=https://MOQCRM20.crm6.dynamics.com/; Username=###; Password=###; authtype=Office365"

#Get Organization service proxy
$orgService=$CRMConn.OrganizationServiceProxy ;

#construct query
$query="<fetch mapping='logical' distinct='false' aggregate='true'>
<entity name='account'>
<attribute name='account' alias='recordcount' aggregate='count'/>
</entity>
</fetch>";

#Create Query expression

$queryExp = New-Object -TypeName Microsoft.Xrm.Sdk.Query.FetchExpression -ArgumentList $query ;

#Get Results
$results=$orgService.RetrieveMultiple($queryExp);

#Extract Count
$countItem=[Microsoft.Xrm.Sdk.AliasedValue]$results.Entities[0]["recordcount"]

Write-Host $countItem.Value

Dynamcis 365 - Override Lookup view using Java script

Hi,

I have encountered some scenarios  in which I needed to override lookup view columns or apply some filters on the results depending on dynamics conditions at run time on CRM form.

In order to do that , add the following piece of code in the on load event

The following example filters results set no a specific case type

 

var viewId = "{00000000-0000-0000-0000-000000000001}";

//add the entity name
var entityName = "incident";

// add the view display name
var viewDisplayName = "Problem Cases";

var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='incident'>" +
" <attribute name='title' />" +
"<attribute name='customerid' />" +
"<attribute name='incidentid' />" +
"<order attribute='title' descending='false' />" +
"<filter type='and'>" +
"<condition attribute='bps_type' operator='eq' value='200000005' />" +
" </filter>" +
" </entity>" +
"</fetch>";

var layoutXml = "<grid name='resultset' object='1' jump='productid' select='1' icon='1' preview='1'>" +
"<row name='result' id='incidentid'>" +
"<cell name='title' width='150' />" +
"<cell name='customerid' width='150' />" +
"</row>" +
"</grid>";

Xrm.Page.getControl("parentcaseid").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);