Sample code to read XML Data from Web Resource in Plugin in CRM 2011/2013/2015

We recently had a requirement to read xml data stored in a particular web resource in plugin.

Sharing the sample code


public static Dictionary<string, string> GetMarketClusterAndMarketSegment(string webresourceName, string buildingTypeValue, IOrganizationService organizationService)
{
Dictionary<string, string> dictionaryMarket = new Dictionary<string, string>();

// tk_et_oppline_marketingfilterpicklists
// create request to retrieve Webresource
ColumnSet cols = new ColumnSet();
cols.AddColumn("content");
QueryByAttribute requestWebResource = new QueryByAttribute
{
EntityName = "webresource",
ColumnSet = cols
};
requestWebResource.Attributes.AddRange("name");
requestWebResource.Values.AddRange(webresourceName);

Entity webResourceEntity = null;
EntityCollection webResourceEntityCollection = organizationService.RetrieveMultiple(requestWebResource);

if (webResourceEntityCollection.Entities.Count > 0)
{
webResourceEntity = webResourceEntityCollection.Entities[0];
byte[] binary = Convert.FromBase64String(webResourceEntity.Attributes["content"].ToString());
string resourceContent = System.Text.Encoding.UTF8.GetString(binary);
string byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
if (resourceContent.StartsWith(byteOrderMarkUtf8))
{
resourceContent = resourceContent.Remove(0, byteOrderMarkUtf8.Length);
}

XDocument xmlDocument = XDocument.Parse(resourceContent);

var marketSegment = from t in xmlDocument.Descendants("ParentField").ElementAt(1).Descendants("ShowOption")
where t.Attribute("value").Value.Equals(buildingTypeValue)
select new
{
marketSegmentValue = t.Parent.Attribute("value").Value,
marketSegmentLabel = t.Parent.Attribute("label").Value
};

foreach (var v in marketSegment)
{
dictionaryMarket.Add("marketsegment", v.marketSegmentValue);
var marketCluster = from t in xmlDocument.Descendants("ParentField").ElementAt(0).Descendants("ShowOption")
where t.Attribute("value").Value.Equals(v.marketSegmentValue)
select new
{
marketClusterValue = t.Parent.Attribute("value").Value,
marketClusterLabel = t.Parent.Attribute("label").Value
};

foreach (var m in marketCluster)
{
dictionaryMarket.Add("marketcluster", m.marketClusterValue);
}
}
}

return dictionaryMarket;
}
}

Hope it helps..

Advertisements

Query execution time of 60 seconds exceeded the threshold of 10 seconds warning in CRM 2013.

Query execution time of 60 seconds exceeded the threshold of 10 seconds warning in CRM 2013.

We were getting the below warning in one of our test environment

On analyzing figured out that one of the plugin was using retrieve with ColumnSet(true) option i.e. retrieving all the columns of the entity which was not required in this case.

Fetching only the required columns fixed the warning.

The other option could have been indexing or increate the value for LongQueryThersholdInSeconds column in ServerSetingProperties table of MSCRM_Config.

Helpful posts

http://blog.edgewater.com/2013/01/07/microsoft-dynamics-crm-querying-data-using-late-binding-versus-early-binding/

http://www.dynamicscrmpros.com/mscrmplatform-warning-in-event-viewer-in-microsoft-dynamics-cr-event-id-17972/

Hope it helps..

 

JavaScript runtime error: Unable to get property ‘get_code’ of undefined or null reference in CRM

We can get the ‘get_code’ error if we are using Assign Step in Action/Workflow and the value is null for the field selected.

Here we need to use If condition to check if the value exist for that particular field before using Assign Step.

Sample code: Using jqGrid in ASP.NET page to show CRM data

Sample code: Using jqGrid in ASP.NET page to show CRM data

Just sharing a simple ASP.NET page that uses jqGrid to show contact records in CRM and allows user to search.

Here is the code SampleJQGrid.zip (remove .doc)

Hope it helps ..

JavaScript Reference for Microsoft Dynamics CRM 2011 / 2013

GT // CRM

Here’s a quick reference guide covering Microsoft CRM syntax for common jscript requirements.

Most of the examples are provided as functions that you can easily test in the OnLoad event of the Account form to see a working example.  These are not production hardened but they’ll help you with the CRM syntax.

(updated: 30 July 2012)

Index:

  1.   Get the GUID value of a Lookup field
  2.   Get the Text value of a Lookup field
  3.   Get the value of a Text field
  4.   Get the database value of an Option Set field
  5.   Get the text value of an Option Set field
  6.   Get the database value of a Bit field
  7.   Get the value of a Date field
  8.   Get the day, month and year parts from a Date field
  9.   Set the value of a String field
  10. Set the value of an Option Set (pick list)…

View original post 1,401 more words

Fix: Invalid ‘where’ condition. An entity member is invoking an invalid property or method while using LINQ in CRM 2013.

Hi,

We were writing a LINQ query to get the contract record information by passing in the lawyer name. The lawyer is a lookup in the contract record.

So we were using contract.lawyerid.Name field of lookup in our where condition.

However we got the below error while doing so..

 

It seems like the LINQ Implementation for CRM doesn’t correctly interpret the lookup field’s name if we are using it in the where clause.

The solution was to do a join between the entities and then use the where condition against the lawyer entity username field itself.

Hope it helps..

Debugging online plugin in CRM 2013.

Hi,

Let us take a simple plugin to understand the debugging process. We need the profile which we will use for debugging.

Register the plugin (debug version) and corresponding step using plugin registration tool.

Select Install Profiler in Plugin Registration tool and wait till the installation is complete.


Select the Step in Plugin and Click Profile in the tool

Select Ok in the dialog box

Open CRM and perform the step that will run the plugin.

Download the log file.

Open Visual Studio and attach debugger to PluginRegistrationTool.exe

Go back to Plugin Registration tool, select the step and select Debug from the tool bar.

 

Specfiy the log file in the Profile location and plugin assembly that is in debug folder.

Click on Start Execution to start debugging

Hope it helps.

Sample code to get label and value for OptionSet in CRM

Hi,

Just sharing a sample code that retrieves optionset data from CRM and also sorts it. We are using it to bind drop down in one of our custom SharePoint Application Page.


/// <summary>
/// The get global option sets.
/// </summary>
/// <param name="config">
/// The config.
/// </param>
/// <param name="globalOptionSetName">
/// The _global option set name.
/// </param>
/// <returns>
/// The <see cref="List"/>.
/// </returns>
public static List<XrmOptionSet> GetGlobalOptionSets(IOrganizationService config, string globalOptionSetName)
{
using (
XrmServiceContext context = SDKHelper.GetXrmServiceContext(
config.OrganizationServiceUrl,
config.Credentials))
{
RetrieveOptionSetRequest retrieveOptionSetRequest = new RetrieveOptionSetRequest
{
Name =
globalOptionSetName
};

// Execute the request.
RetrieveOptionSetResponse retrieveOptionSetResponse =
(RetrieveOptionSetResponse)context.Execute(retrieveOptionSetRequest);

OptionSetMetadata retrievedOptionSetMetadata =
(OptionSetMetadata)retrieveOptionSetResponse.OptionSetMetadata;

OptionMetadata[] optionList = retrievedOptionSetMetadata.Options.ToArray();

List<XrmOptionSet> optionSetItems = new List<XrmOptionSet>();

// Add each item in OptionMetadata array to the ListItemCollection object.
foreach (OptionMetadata o in optionList)
{
XrmOptionSet xrmOptionSet = new XrmOptionSet();
xrmOptionSet.Label = o.Label.LocalizedLabels[0].Label;
xrmOptionSet.Value = o.Value.Value.ToString(CultureInfo.InvariantCulture);
optionSetItems.Add(xrmOptionSet);
}

optionSetItems.Sort(
delegate(XrmOptionSet thisItem, XrmOptionSet otherItem)
{
return thisItem.Label.CompareTo(otherItem.Label);
});

return optionSetItems;
}
}

/// <summary>
/// The get local option sets.
/// </summary>
/// <param name="config">
/// The config.
/// </param>
/// <param name="entityName">
/// The entity name.
/// </param>
/// <param name="optionSetAttributeName">
/// The option set attribute name.
/// </param>
/// <returns>
/// The <see cref="List"/>.
/// </returns>
public static List<XrmOptionSet> GetLocalOptionSets(
IOrganizationService config,
string entityName,
string optionSetAttributeName)
{
using (
XrmServiceContext context = SDKHelper.GetXrmServiceContext(
config.OrganizationServiceUrl,
config.Credentials))
{
// Create the Attribute Request.
RetrieveAttributeRequest retrieveAttributeRequest = new RetrieveAttributeRequest
{
EntityLogicalName =
entityName,
LogicalName =
optionSetAttributeName,
RetrieveAsIfPublished
= true
};

// Get the Response and MetaData. Then convert to Option MetaData Array.
RetrieveAttributeResponse retrieveAttributeResponse =
(RetrieveAttributeResponse)context.Execute(retrieveAttributeRequest);
PicklistAttributeMetadata retrievedPicklistAttributeMetadata =
(PicklistAttributeMetadata)retrieveAttributeResponse.AttributeMetadata;
OptionMetadata[] optionList = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();

List<XrmOptionSet> optionSetItems = new List<XrmOptionSet>();

// Add each item in OptionMetadata array to the ListItemCollection object.
foreach (OptionMetadata o in optionList)
{
XrmOptionSet xrmOptionSet = new XrmOptionSet();
xrmOptionSet.Label = o.Label.LocalizedLabels[0].Label;
xrmOptionSet.Value = o.Value.Value.ToString(CultureInfo.InvariantCulture);
optionSetItems.Add(xrmOptionSet);
}

optionSetItems.Sort(
delegate(XrmOptionSet thisItem, XrmOptionSet otherItem)
{
return thisItem.Label.CompareTo(otherItem.Label);
});

return optionSetItems;
}
}
}

&nbsp;

 

Hope it helps.

Generic SQL Error – RetriveMultiple with ConditionOperator.Contains in CRM

Hi,

Was getting Generic SQL Error while using ConditionOperator.Contains in the ConditionExpression. The requirement was to get all the incident records based on the title attribute value.

condExp.AttributeName = “title”;

condExp.Operator = ConditionOperator.Contains;

condExp.Values.Add(“test”);

The correct way to perform this kind of search is by using Like operator with %.

condExp.AttributeName = “title”;

condExp.Operator = ConditionOperator.Contains;

condExp.Values.Add(“%test%”);

http://social.microsoft.com/Forums/en-US/ede5932b-5ac9-4fc9-86d9-12ef5b2b305d/crm-2011-getting-generic-sql-error-when-performing-retrievemultiple-with?forum=crmdevelopment

Hope it helps.

 

 

 

Microsoft Dynamics CRM Compatibility List

Hi,

Found this article that lists the minimum version of products compatible with Microsoft Dynamics CRM 4.0 and 2011.

Do check it out

http://support.microsoft.com/kb/2669061

Bye.