Sample code to check if User is a member of a team C# CRM 2011/2013/2015


Sharing sample code to check if a user belongs to a team or not


public static bool IsTeamMember(Guid teamID, Guid userID, IOrganizationService service)
{
QueryExpression query = new QueryExpression("team");
query.ColumnSet = new ColumnSet(true);
query.Criteria.AddCondition(new ConditionExpression("teamid", ConditionOperator.Equal, teamID));
LinkEntity link = query.AddLink("teammembership", "teamid", "teamid");
link.LinkCriteria.AddCondition(new ConditionExpression("systemuserid", ConditionOperator.Equal, userID));
var results = service.RetrieveMultiple(query);

if (results.Entities.Count > 0)
{
return true;
}
else
{
return false;
}
}

Hope it helps.

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..

You cannot send email as the selected user. The selected user has not allowed this or you do not have sufficient privileges to do so. Contact your system administrator for assistance. Error in CRM 2013/2015


Was getting the below error in one of the custom workflow activity we were using for sending email.

To fix the issue,

Login to CRM with the user selected as From.

Open Set Personal Options box

And check the check box “Allow other Microsoft Dynamics CRM users to send email on your behalf”

Hope it helps..

The operation could not be completed because you do not have read access on some of the fields in {0} record error in CRM 2013\2015


Hi,

While trying to add a new quote record to an opportunity record, we were getting the below error

The issue turned out that the user was not having read access to one of the fields in the opportunity record configured through Field Level Security.

Hope it helps..

Using Action to trigger Plugin in CRM 2013\2015


Hi,

We recently had a requirement to call Plugin on execute of an Action.

To implement this,

We created a blank Action named Trigger Plugin for custom entity named Security Entity.

Open Plugin Registration Tool and connect to the Org and we will have our action available there as message for our Plugin Step

We can execute our Action from C# and Jscript which in turns will call our Plugin.

C# Code:

Jscript Code:

</p>
<p>var xml = "" +<br />
"&lt;s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"&gt;&lt;s:Body&gt;&lt;Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"&gt;&lt;request xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\"&gt;&lt;a:Parameters xmlns:b=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\"&gt;&lt;a:KeyValuePairOfstringanyType&gt;&lt;b:key&gt;Target&lt;/b:key&gt;&lt;b:value i:type=\"a:EntityReference\"&gt;&lt;a:Id&gt;1c54dadd-f91b-e511-a5ca-c4346bacae20&lt;/a:Id&gt;&lt;a:LogicalName&gt;tk_securityentity&lt;/a:LogicalName&gt;&lt;a:Name i:nil=\"true\"/&gt;&lt;/b:value&gt;&lt;/a:KeyValuePairOfstringanyType&gt;&lt;/a:Parameters&gt;&lt;a:RequestId i:nil=\"true\"/&gt;&lt;a:RequestName&gt;new_TriggerPlugin&lt;/a:RequestName&gt;&lt;/request&gt;&lt;/Execute&gt;&lt;/s:Body&gt;&lt;/s:Envelope&gt;" +<br />
"";</p>
<p>var xmlHttpRequest = new XMLHttpRequest();</p>
<p>xmlHttpRequest.open("POST", Xrm.Page.context.getClientUrl() +"/XRMServices/2011/Organization.svc/web", false);<br />
xmlHttpRequest.setRequestHeader("SOAPAction","http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");<br />
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");<br />
xmlHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");<br />
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);<br />
xmlHttpRequest.send(xml);</p>
<p>var resultXml = xmlHttpRequest.responseText;<br />
alert(resultXml);</p>
<p>

Use the SoapLogger tool to get the Soap request

https://nishantrana.me/2014/08/27/updated-soaplogger-for-crm-2013-to-generate-javascript/

Hope it helps..

 

Advertisements

Fixed: Action not appearing in available Message in Plugin Registration tool CRM 2013\ CRM 2015


Hi,

We created a new Action and wanted to trigger a plugin on call of the Action.

However that Action was not appearing in available messages while registering step for Plugin.

Finally got to know that in case of Online need to restart the plugin registration tool for that action to appear in message.

 

Simple Refresh or Reloading of the organization will not work.

Hope it helps..