CRM 2013: Cannot specify child attribute retrieve – MergeRequest C#

Hello!

If you are working on code to merge two entity records and come across “Cannot specify child attribute retrieve” error; and you have referred SDK/MSDN documentation. You probably have kept UpdateContent property optional and havent set it. UpdateContent is a required property though the documentation says it is not.

This property is optional in case of merging incidents only.

You just have to set Entity object to UpdateContent property which has attributes to be updated to the merged record.

Entity contact = new Entity("contact");
contact["firstname"] = "Crm";
contact["lastname"] = "Xpress";
 
// merging contact records
MergeRequest merge = new MergeRequest
{
    // SubordinateId is the GUID of the contact merging.
    SubordinateId = contactId,
    Target = target,
    PerformParentingChecks = true,
    UpdateContent = contact // Entity Object to set the content you want updated on the merged contact
};

HTH!
Happy Merging!

Advertisements

C#: Security accessibility of the overriding method must match the security accessibility of the method being overriden

I was porting a .Net 2.0 assembly to .Net 4.0 plugin assembly and faced this particular challenge.

The project had a test app(A WinForm app) and it worked fine on the desktop but when I deployed the logic on server to run as a plugin it gave the following error

xyz..GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext) Security accessibility of the overriding method must match the security accessibility of the method being overriden.
Just notice, the spelling for “overridden” is also not correct. But this is what .Net framework threw.
After some research I found that this behavior is “By Design” i.e. as per security transparency model defined in .Net v4.0. It basically has two models:
1. Level1 (High Level) for v2.0
2. Level2 for v4.0
Here is a a list of critical operations:

1. Call native code

2. Contain unverifiable code

3. Call critical code

4. Perform security asserts

5. Satisfy link demands

6. Derive from non-transparent types

7. Override security critical virtuals

And only full trust code can be security critical.

Hence an overridden member should have the same security accessibility (Critical, Safe Critical or Transparent).
To resolve this error take the following step:
Go to the AssemblyInfo.cs class to your project and add following attributes to it. And this should resolve the exception.

[assembly: SecurityRules(SecurityRuleSet.Level1)] 
[assembly: AllowPartiallyTrustedCallers]

I believe it might be helpful to many as there are lot of Dynamics CRM implementations being upgraded.

Reference links to the differences between both and transparency rules:
http://blogs.msdn.com/b/shawnfa/archive/2009/11/12/differences-between-the-security-rule-sets.aspx
http://blogs.msdn.com/b/shawnfa/archive/2009/11/03/transparency-101-basic-transparency-rules.aspx

C#: Get Current Month Name and Short Name

We had a requirement to get the current month name and its short name (example- November|Nov). While DateTime class does not provide these methods out of .net framework, we had to create methods which gives the similar output.. We have created similar methods through Extension Methods supported with recent .net framework.

Extension methods can be used as instance methods and it uses “this” keyword as a parameter. They are static and they must be defined on static class.

Here is the solution:

using System;
using System.Globalization;

namespace TestConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(DateTime.Today.Day + "." + DateTime.Today.Month + "." + DateTime.Today.Year);
            Console.WriteLine(DateTime.Now.AddMonths(-2).ToMonthName());
            Console.WriteLine(DateTime.Now.ToShortMonthName());
            
            // Update: Similar functionality can be achieved through below statements also.
            // Thanks Michael for inputs.
            Console.WriteLine(String.Format("{0:MMMM}", DateTime.Now));
            Console.WriteLine(String.Format("{0:MMM}", DateTime.Now));
            Console.WriteLine(DateTime.Now.ToString("MMMM"));
            Console.WriteLine(DateTime.Now.ToString("MMM"));
        }
    }

    static class DateTimeExtensions
    {
        public static string ToMonthName(this DateTime dateTime)
        {
            return CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(dateTime.Month);
        }

        public static string ToShortMonthName(this DateTime dateTime)
        {
            return CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(dateTime.Month);
        }
    }
}

Output:
28.11.2014
September
Nov

Extension Methods in Visual Studio appears with special symbol:

Extension Methods

Extension Methods

Reference: http://www.dotnetperls.com/extension

C#: The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail.

Hello guys,

Before some time I had a situation where my console application would not run and without showing any screen/anything else it gave me this alert mentioned below:

The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail.

Most of the google links will guide you to check your VC++ runtime and their versions. I checked everything and everything was as expected as other applications were working smooth without any error.

Also, I checked with a simple “Hello world!” console application but the output was same. And with no clue to my mind I removed the .config file from the directory.

If same is your situation then, suggested solution would be to rename the extension of the .config file to .xml file and run/parse it to any browser. In my case the .xml file did not meet with the standards as an value contained & (ampersand) and it was breaking the it to be well formed xml document.

Check with the standard list of invalid xml characters so that you can avoid this kind of situation in future. Check this link – http://www.w3.org/TR/2000/REC-xml-20001006#charsets to see well-formed xml document rules.

C#: Converting from Generic List to CSV and CSV to Generic List

CSV from List

public string GetCsvFromList(List<string> list)
{
    return String.Join(",", list.Select(x => x.ToString()).ToArray());
}

List from CSV

public List<T> GetListFromCsv<T>(string commaSplited)
{
    return (from e in commaSplited.Split(',')
            select (T)Convert.ChangeType(e.Trim(), typeof(T))).ToList();
}

AD Change Password C#: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

Hi,

I was trying to Reset/Change Password for Active Directory through C# code behind. And I encountered with the same error:Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

Access Denied errors that are reported by scripts and applications that access WMI namespaces and data generally fall into three categories.

Possible Reasons are:
The user does not have remote access to the computer through DCOM. Typically, DCOM errors occur when connecting to a remote computer with a different operating system versions.

Solution:
Give the user Remote Launch and Remote Activation permissions in dcomcnfg.

  • Right-click My Computer-> Properties Under COM Security, click “Edit Limits” for both sections.
  • Give the user you want remote access, remote launch, and remote activation.
  • Go to DCOM Config, find “Windows Management Instrumentation”, and give the user you want Remote Launch and Remote Activation.

For more information, see Connecting Between Different Operating Systems

References: http://msdn.microsoft.com/en-us/library/windows/desktop/aa394603(v=vs.85).aspx

ADFS 2.0: “Could not load file or assembly ‘Microsoft.IdentityServer.Compression’ or one of its dependencies. An attempt was made to load a program with an incorrect format”

If you have come to this blog searching for the error mentioned above then probably you’re doing some modifications to ADFS 2.0 site.

Following would be the symptoms that you have done so far:

  • You have opened adfs\ls website as Website project in Visual Studio.
  • If the project type is Website, you probably had a message from Visual Studio to upgrade for the .Net v4.0.
  • You have created a new project Empty Web Application and imported adfs\ls website content to the project.
  • You are modifying this site/application on local machine and not on Windows Server where ADFS is installed.

Follow these steps to successfully start modifying ADFS site:

  1. Install Visual Studio 2010 on Windows Server where ADFS is installed and configured.
  2. Create an Empty Web Application
  3. Import all the files from adfs\ls directory to the Web Application. (generally, you can find it on c:\inetpub\adfs\ls)
  4. Right click on the project in Solution Explorer and click on “Convert to Web Application”.
  5. You’re ready to go!

You do not need to add reference of any files to this web application. Just Build and Publish the site to the same directory where adfs is installed.

Microsoft.IdentityServer.Compression and other required files will be referenced from GAC as you are developing the project on the server itself.

Happy Programming! 🙂

CRM 2011, C#, v3.5: Type ‘OptionSetValue’ with data contract name ‘OptionSetValue:http://schemas.microsoft.com/xrm/2011/Contracts’ is not expected

We already have discussed how to achieve connection of CRM 2011 through .net version 3.5 from below mentioned post. https://consultrikin.wordpress.com/2013/09/20/crm-2011-c-entity-class-redefined-with-net-framwork-3-5-to-connect-crm-2011/

Once this is achieved and you start working on CRUD operations, you will find some run time exceptions thrown like one I have described below.

Type ‘OptionSetValue’ with data contract name ‘OptionSetValue:http://schemas.microsoft.com/xrm/2011/Contracts&#8217; is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types – for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.’.  Please see InnerException for more details.

The same thing will be repeated for EntityReference and PicklistAttributeMetadata.

Resolution:

1. Explore the reference.cs from the project in Windows Explorer and open it.

2. Search for class Entity and class EntityReference

3. Add below lines to both partial classes.
     [System.Runtime.Serialization.KnownTypeAttribute(typeof(OptionSetValue))]
     [System.Runtime.Serialization.KnownTypeAttribute(typeof(EntityReference))]

4. Now search for “class OrganizationRequest”.
Add these lines above it:

     [System.Runtime.Serialization.KnownTypeAttribute(typeof(EntityReference))]
     [System.Runtime.Serialization.KnownTypeAttribute(typeof(PrincipalAccess))]
     [System.Runtime.Serialization.KnownTypeAttribute(typeof(OptionSetValue))]

5. Continue adding to classes for which you’re getting these exceptions.

6. Build the solution and run your code again.

Happy Programming! 🙂

CRM 2011, C#: OrganizationRequest and OrganizationResponse with .Net version 3.5

We already discussed how a CRM 2011 web service can help us create object of OrganizationServiceProxy or Instance of IOrganizationService in my previous posts. This is the case when we’re not using assemblies from SDK and thus several SDK request and response messages will not be available to you like RetrieveAttributeRequest/Response etc.

All we have is OrganizationRequest and OrganizationResponse and in this case you will have to use OrganizationRequest and OrganizationResponse to request data from CRM 2011. See below example which uses similar example for RetrieveAttributeRequest.

OrganizationRequest request = new OrganizationRequest();
request.RequestName = "RetrieveAttribute";
request.Parameters = new ParameterCollection();
request.Parameters.Add(new KeyValuePair<string, object>("EntityLogicalName", "salesorder"));
request.Parameters.Add(new KeyValuePair<string, object>("RetrieveAsIfPublished", true));
request.Parameters.Add(new KeyValuePair<string, object>("LogicalName", "new_cardtype"));

try
{
    OrganizationResponse response = (OrganizationResponse)CrmHelper.OrgService.Execute(request);
    PicklistAttributeMetadata picklist = (PicklistAttributeMetadata)response.Results[0].Value;
}
catch (FaultException<OrganizationServiceFault> ex)
{
    throw new Exception("Error details: " + ex.Message);
}

Happy Programming!

CRM 2011, C#: Entity Class redefined with .net framwork 3.5 to connect CRM 2011

Look at my previous post on connecting CRM 2011 using .net framework 3.5 without CRM 2011 SDK. https://consultrikin.wordpress.com/2013/09/19/crm-2011-c-connect-crm-2011-without-using-sdkusing-net-framework-3-5/

Here, if you use Entity Class to create Entity records the same way we create records in .net framework 4.0 with SDK in late bound; this requires following syntax.

Entity entity = new Entity();
entity.LogicalName = "contact";
entity.Attributes = new ServiceReference1.AttributeCollection();
entity.Attributes.Add(new KeyValuePair<string, object>("firstname", "test first name"));
entity.Attributes.Add(new KeyValuePair<string, object>("lastname", "test last name"));
OrgService.Create(entity);

Here, you must add an attribute value as a new KeyValuePair<string, object>. Or you need to access the attributes by their indexes like:

Entity entity = new Entity();
entity.LogicalName = "contact";
entity.Attributes = new ServiceReference1.AttributeCollection();
entity.Attributes[0] = "test first name";
entity.Attributes[1] = "test last name";
OrgService.Create(entity);

This code does not have readability as you need to find int index every time you access entity. While I found the same extended Entity Class from the posts I mentioned in my previous post. Here they are listed. Just add them to your project and you will be able to add attributes with string parameters.

/// <summary>
/// Entity Class
/// </summary>
partial class Entity
{
	/// <summary>
	/// Initializes a new instance of the <see cref="Entity" /> class.
	/// </summary>
	public Entity()
	{
		this.FormattedValuesField = new FormattedValueCollection();
		this.RelatedEntitiesField = new RelatedEntityCollection();
	}

	/// <summary>
	/// Gets the attribute value.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="attributeLogicalName">Name of the attribute logical.</param>
	/// <returns></returns>
	public T GetAttributeValue<T>(string attributeLogicalName)
	{
		if (null == this.Attributes)
		{
			this.Attributes = new AttributeCollection();
		};
		object value;
		if (this.Attributes.TryGetValue(attributeLogicalName, out value))
		{
			return (T)value;
		}

		return default(T);
	}

	/// <summary>
	/// Gets or sets the <see cref="System.Object"/> with the specified attribute name.
	/// </summary>
	/// <value>
	/// The <see cref="System.Object"/>.
	/// </value>
	/// <param name="attributeName">Name of the attribute.</param>
	/// <returns></returns>
	public object this[string attributeName]
	{
		get
		{
			if (null == this.Attributes)
			{
				this.Attributes = new AttributeCollection();
			};

			return this.Attributes.GetItem(attributeName);
		}
		set
		{
			if (null == this.Attributes)
			{
				this.Attributes = new AttributeCollection();
			};

			this.Attributes.SetItem(attributeName, value);
		}
	}
}

And Add following Class-

/// <summary>
/// CollectionExtensions Class
/// </summary>
public static class CollectionExtensions
{
	/// <summary>
	/// Gets the item.
	/// </summary>
	/// <typeparam name="TKey">The type of the key.</typeparam>
	/// <typeparam name="TValue">The type of the value.</typeparam>
	/// <param name="collection">The collection.</param>
	/// <param name="key">The key.</param>
	/// <returns>returns TValue</returns>
	/// <exception cref="System.Collections.Generic.KeyNotFoundException">Key =  + key</exception>
	public static TValue GetItem<TKey, TValue>(this IList<KeyValuePair<TKey, TValue>> collection, TKey key)
	{
		TValue value;
		if (TryGetValue(collection, key, out value))
		{
			return value;
		}

		throw new System.Collections.Generic.KeyNotFoundException("Key = " + key);
	}

	/// <summary>
	/// Sets the item.
	/// </summary>
	/// <typeparam name="TKey">The type of the key.</typeparam>
	/// <typeparam name="TValue">The type of the value.</typeparam>
	/// <param name="collection">The collection.</param>
	/// <param name="key">The key.</param>
	/// <param name="value">The value.</param>
	public static void SetItem<TKey, TValue>(this IList<KeyValuePair<TKey, TValue>> collection, TKey key, TValue value)
	{
		int index;

		if (TryGetIndex<TKey, TValue>(collection, key, out index))
		{
			collection.RemoveAt(index);
		}

		//If the value is an array, it needs to be converted into a List. This is due to how Silverlight serializes
		//Arrays and IList<T> objects (they are both serialized with the same namespace). Any collection objects will
		//already add the KnownType for IList<T>, which means that any parameters that are arrays cannot be added
		//as a KnownType (or it will throw an exception).
		Array array = value as Array;
		if (null != array)
		{
			Type listType = typeof(List<>).GetGenericTypeDefinition().MakeGenericType(array.GetType().GetElementType());
			object list = Activator.CreateInstance(listType, array);
			try
			{
				value = (TValue)list;
			}
			catch (InvalidCastException)
			{
				//Don't do the conversion because the types are not compatible
			}
		}

		collection.Add(new KeyValuePair<TKey, TValue>(key, value));
	}

	/// <summary>
	/// Determines whether the specified collection contains key.
	/// </summary>
	/// <typeparam name="TKey">The type of the key.</typeparam>
	/// <typeparam name="TValue">The type of the value.</typeparam>
	/// <param name="collection">The collection.</param>
	/// <param name="key">The key.</param>
	/// <returns>returns true or false</returns>
	public static bool ContainsKey<TKey, TValue>(this IList<KeyValuePair<TKey, TValue>> collection, TKey key)
	{
		int index;
		return TryGetIndex<TKey, TValue>(collection, key, out index);
	}

	/// <summary>
	/// Tries the get value.
	/// </summary>
	/// <typeparam name="TKey">The type of the key.</typeparam>
	/// <typeparam name="TValue">The type of the value.</typeparam>
	/// <param name="collection">The collection.</param>
	/// <param name="key">The key.</param>
	/// <param name="value">The value.</param>
	/// <returns>returns true or false</returns>
	public static bool TryGetValue<TKey, TValue>(this IList<KeyValuePair<TKey, TValue>> collection, TKey key, out TValue value)
	{
		int index;
		if (TryGetIndex<TKey, TValue>(collection, key, out index))
		{
			value = collection[index].Value;
			return true;
		}

		value = default(TValue);
		return false;
	}

	/// <summary>
	/// Tries the index of the get.
	/// </summary>
	/// <typeparam name="TKey">The type of the key.</typeparam>
	/// <typeparam name="TValue">The type of the value.</typeparam>
	/// <param name="collection">The collection.</param>
	/// <param name="key">The key.</param>
	/// <param name="index">The index.</param>
	/// <returns>returns true or false</returns>
	private static bool TryGetIndex<TKey, TValue>(IList<KeyValuePair<TKey, TValue>> collection, TKey key, out int index)
	{
		if (null == collection || null == key)
		{
			index = -1;
			return false;
		}

		index = -1;
		for (int i = 0; i < collection.Count; i++)
		{
			if (key.Equals(collection[i].Key))
			{
				index = i;
				return true;
			}
		}

		return false;
	}
}

Happy Programming!!