Sample code to update Access Mode of System User in C# (CRM 2016 or earlier)

Recently had a requirement to set Access Mode of around 500 users to Administrative from Read – Write. We wrote an on demand workflow for that. However, running that workflow on those 500 records in a batch of 100 records was causing them to stuck in waiting stage and we had to then manually resume those workflow instance. So we ended up writing a Console Application for that.


   QueryExpression queryExpression = new QueryExpression();
            queryExpression.EntityName = "systemuser";
            queryExpression.ColumnSet = new ColumnSet();
            queryExpression.ColumnSet.AddColumn("fullname");

            ConditionExpression conditionExpression1 = new ConditionExpression();
            conditionExpression1.AttributeName = "tk_businessarea";
            conditionExpression1.Values.Add("EE286698-AF08-E311-B55E-1CC1DE6DAA0B");
            conditionExpression1.Operator = ConditionOperator.Equal;

            ConditionExpression conditionExpression2 = new ConditionExpression();
            conditionExpression2.AttributeName = "isdisabled";
            conditionExpression2.Values.Add(false);
            conditionExpression2.Operator = ConditionOperator.Equal;

            // access mode ==> 0 --> Read Write and 1 --> Adminstrative
            ConditionExpression conditionExpression3 = new ConditionExpression();
            conditionExpression3.AttributeName = "accessmode";
            conditionExpression3.Values.Add(1);
            conditionExpression3.Operator = ConditionOperator.Equal;
            
            queryExpression.Criteria.AddCondition(conditionExpression1);
            queryExpression.Criteria.AddCondition(conditionExpression2);
            queryExpression.Criteria.AddCondition(conditionExpression3);

            queryExpression.Criteria.FilterOperator = LogicalOperator.And;

            EntityCollection entityColl = organizationProxy.RetrieveMultiple(queryExpression);
            foreach(var entity in entityColl.Entities)
            {
                Entity userEntity = new Entity("systemuser");
                userEntity.Id = entity.Id;
                userEntity.Attributes["accessmode"] = new OptionSetValue(0);
                organizationProxy.Update(userEntity);
            }

Hope it helps !!

Generic SQL error in CRM 2015 Online (and earlier)

Recently we were getting the Generic SQL Error in one of our Plugin which was registered in Create of Opportunity Line.

The scenario was that on qualifying the lead, opportunity was getting created and we had Async Plugin on Create of Opportunity that will create an Opportunity Line and had Sync Plugin on Create of Opportunity Line that was updating the parent Opportunity. This issue was occurring randomly.

We observed the following, that if both the Plugin are registered as Sync or Async the issue doesn’t occur. It also doesn’t occur if we have Opportunity Plugin on Create as Sync and the one on Opp. Line as Async.

The other way of solving the issue was to introduce delay before updating the Parent Opportunity from the child opportunity line create Plugin.

Hope this helps..

SandboxFault.ThrowIfGuidEmpty: entityId in CRM 2015.

Hi,

We had one of our Plugins registered in RetrieveMultiple of an entity. And one of the JavaScript was using FetchXML to retrieve that particular entity records. This JavaScript was throwing the following exception

Putting the Guid.Empty check for the OutputParameter EntityCollection fixed the issue.

Hope it helps..

“The content of the import file is not valid. You must select a text file” – Error on Solution Import in CRM 2015 (and earlier)

Hi,

We got this issue while importing (managed) solution from dev to test organization. We had recently deleted one of our existing 1 – n relationship and had created a new n – n relationship between those entities. The issue was that the new relationship had exactly the same schema name as the old relationship which we deleted.

The solution was to delete the newly created n – n relationship in Dev and create a new one with different schema name. After this change we were able to import the solution successfully.

Hope it helps..

Sample code to close a quote as won using WinQuoteRequest in CRM 2016 (and earlier)

Just sharing a sample code to close quote as won through C#


WinQuoteRequest winQuoteRequest = new WinQuoteRequest();
Entity quoteClose = new Entity("quoteclose");
quoteClose.Attributes["quoteid"] = new EntityReference("quote", new Guid("015816C2-2F10-E611-8112-3863BB353ED0"));
quoteClose.Attributes["subject"] = "Quote Close" + DateTime.Now.ToString();
winQuoteRequest.QuoteClose = quoteClose;
winQuoteRequest.Status = new OptionSetValue(-1);
organizationProxy.Execute(winQuoteRequest);

Hope it helps..

Set PartyList field To in Email in CRM 2015 (and earlier)

Sample code to set the PartyList field for Email Activity.


function setContact() {

var partlistData = new Array();
partlistData[0] = new Object();

// guid of the record
partlistData[0].id = "8B2AD82B-30D6-E511-811F-3863BB356F90";

// name of the record
partlistData[0].name = "Hugh Grant";

// entity schema name
partlistData[0].entityType = "contact";
Xrm.Page.getAttribute("to").setValue(partlistData)

}

Hope it helps..

The selected translations file is either invalid or does not conform to the required translations file schema error while importing translations in CRM 2015 (and earlier)

Hi,

While importing translations we got the below error

On opening the CrmTranslations.xml we saw the translations missing for that row.

On specifying the values for those fields and importing it back fixed the issue.

Hope it helps..

Unit Testing (Microsoft Fakes) RetrieveSharedPrincipalsAndAccessResponse in Plugin in CRM 2016 (and earlier)

Hi,

While trying to write Fakes for RetrieveSharedPrincipalsAndAccessResponse we’d realize that PrincipalAccess is read only property and the class itself is sealed.

So to unit test it we need to write a wrapper class as suggested here

http://alexanderdevelopment.net/post/2013/10/17/unit-testing-custom-microsoft-dynamics-crm-code-part-4/

The wrapper class


/// <summary>
/// Wrapper class for retrieve response
/// </summary>
[DataContract(Namespace = "http://schemas.microsoft.com/xrm/2011/Contracts")]
public class RetrieveSharedPrincipalsAndAccessResponseWrapper : OrganizationResponse
{
/// <summary>
/// The _entity
/// </summary>
private PrincipalAccess[] _principalaccess;

/// <summary>
/// Initializes a new instance of the <see cref="RetrieveResponseWrapper"/> class.
/// </summary>
/// <param name="response">The response.</param>
public RetrieveSharedPrincipalsAndAccessResponseWrapper(OrganizationResponse response)
{
try
{
this.PrincipalAccesses = ((RetrieveSharedPrincipalsAndAccessResponseWrapper)response).PrincipalAccesses;
}
catch
{
this.PrincipalAccesses = ((RetrieveSharedPrincipalsAndAccessResponse)response).PrincipalAccesses;
}
}

/// <summary>
/// Gets or sets the PrincipalAccesses.
/// </summary>
/// <value>
/// The entity.
/// </value>
public PrincipalAccess[] PrincipalAccesses
{
get
{
return _principalaccess;
}

set
{
_principalaccess = value;
}
}
}

The unit test


 organizationService.ExecuteOrganizationRequest = request =>
 { 
 RetrieveSharedPrincipalsAndAccessResponseWrapper retrieveResponseWrapper = new RetrieveSharedPrincipalsAndAccessResponseWrapper(new RetrieveSharedPrincipalsAndAccessResponse());
 PrincipalAccess[] principalAccess = new PrincipalAccess[1]; 
 PrincipalAccess pAccess = new PrincipalAccess();
 pAccess.AccessMask = AccessRights.CreateAccess;
 pAccess.Principal = new EntityReference("team", new Guid("9A69533A-2306-4DFC-9662-65ABFAB41348"));
 principalAccess[0] = pAccess;
 retrieveResponseWrapper.PrincipalAccesses = principalAccess; 
 return retrieveResponseWrapper;
 };

 

Change in the Plugin code using Wrapper instead of original class



 IOrganizationService orgService = serviceFactory.CreateOrganizationService(context.UserId);

 var accessRequest = new RetrieveSharedPrincipalsAndAccessRequest
 {
 Target = new EntityReference("projectsite", new Guid("CDF30DD5-C64E-4EF5-ABA6-C9715C10A07D"))
 };

 RetrieveSharedPrincipalsAndAccessResponseWrapper accessResponse = (new RetrieveSharedPrincipalsAndAccessResponseWrapper(orgService.Execute(accessRequest)));

Hope it helps

The type or namespace name ‘RSASignaturePadding’ does not exist in the namespace ‘System.Security.Cryptography’ (are you missing an assembly reference?) in Unit Test Project.

Was getting the below error while trying to build a unit test project (framework version 4.5.2) in VS 2012.

Delete the .messages file as suggested in some forums, but that didn’t help.

Just tried building the project by changing the Target Framework to 4.6.

That worked and after that on switching back the target version to 4.5.2 also worked.

Helpful post

https://social.msdn.microsoft.com/Forums/vstudio/en-US/2de45cd4-2744-4a91-91a2-785f305c1ef7/errors-on-trying-to-add-fakes-assembly-on-system?forum=vsunittest

Hope it helps..

Xrm.Utility.openEntityForm with Regarding field in CRM 2015.

Hi

We recently had a requirement to open new Task form on click of a ribbon button with Regarding field prefilled.

And as mentioned in the SDK, we can’t set the values for regarding lookups.

https://msdn.microsoft.com/en-us/library/gg334375.aspx#BKMK_SetLookupFieldValues

The workaround is to create custom parameters, set its value in JavaScript and use these values on the onLoad of the Form.

http://www.crmnerd.com/customizations/using-xrm.utility.openentityform-with-custom-parameters/

http://butenko.pro/2013/01/ms-crm-2011-open-new-activity-form-with-regarding-field-prefilled/#comment-134

Hope it helps..