<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MSCRM Blogger &#187; forms</title>
	<atom:link href="http://mscrmblogger.com/tag/forms/feed/" rel="self" type="application/rss+xml" />
	<link>http://mscrmblogger.com</link>
	<description>Achieving it all with Microsoft Dynamics CRM™</description>
	<lastBuildDate>Fri, 09 Jul 2010 14:57:43 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Set a default email template for new emails on cases.</title>
		<link>http://mscrmblogger.com/2009/10/02/set-a-default-email-template-for-new-emails-on-cases/</link>
		<comments>http://mscrmblogger.com/2009/10/02/set-a-default-email-template-for-new-emails-on-cases/#comments</comments>
		<pubDate>Fri, 02 Oct 2009 22:31:58 +0000</pubDate>
		<dc:creator>Carlton Colter</dc:creator>
				<category><![CDATA[Scripts]]></category>
		<category><![CDATA[crm]]></category>
		<category><![CDATA[crm 4]]></category>
		<category><![CDATA[crm4]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jscript]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[microsoft dynamics CRM 4]]></category>
		<category><![CDATA[onload event]]></category>

		<guid isPermaLink="false">http://mscrmblogger.com/?p=152</guid>
		<description><![CDATA[<p>In response to <a href="http://social.microsoft.com/Forums/en-US/crmdevelopment/thread/2d2c28ba-891d-4ebc-9c54-92c7cc21a034">Peter Bergman's post on the CRM forums</a>, I traversed Microsoft's code.  I determined that the ApplyTemplate loads a specific page, and on that page, the ok button sets the information on the calling page.  I melded the ApplyTemplate and ok methods together to create LoadTemplate provided below.  With the right parameters, it will set the default template.</p>]]></description>
			<content:encoded><![CDATA[<p>In response to <a href="http://social.microsoft.com/Forums/en-US/crmdevelopment/thread/2d2c28ba-891d-4ebc-9c54-92c7cc21a034">Peter Bergman&#8217;s post on the CRM forums</a>, I traversed Microsoft&#8217;s code.  I determined that the ApplyTemplate loads a specific page, and on that page, the ok button sets the information on the calling page.  I melded the ApplyTemplate and ok methods together to create LoadTemplate provided below.  With the right parameters, it will set the default template.</p>
<pre name="code" class="javascript">
function LoadTemplate(templateid, objectid, objecttypecode)
{
    var command = new RemoteCommand("EmailTemplateService", "GetInstantiatedEmailTemplate");
    command.SetParameter("templateId", templateid );
    command.SetParameter("objectId", objectid);
    command.SetParameter("objectTypeCode", objecttypecode);

    var result = command.Execute();

    if (result.Success)
    {
        var o = new Object();
        o.EmailBody = "";
        o.EmailSubject = "";
        if(typeof(result.ReturnValue) == "string")
        {
            oXml = CreateXmlDocument(false);
            oXml.loadXML(result.ReturnValue);
            o.EmailBody = oXml.selectSingleNode("template/body").text;
            o.EmailSubject = oXml.selectSingleNode("template/subject").text;

            crmForm.all.description.InsertValue( o.EmailBody  );
            if( !IsNull( o.EmailSubject ) &#038;&#038; o.EmailSubject.length > 0 )
            {
                crmForm.all.subject.DataValue =
                    CrmEncodeDecode.CrmHtmlDecode(o.EmailSubject).substr(0, crmForm.all.subject.MaxLength);
            }
        }
    }
}
</pre>
<p>To set the default template, you need the templateid, you can get this by opening the template and hitting CTRL-N, or you can just operate in tab mode and see all the id&#8217;s and urls at all times. I prefer that view&#8230;  The below code uses the templateid to set a default email template when a new email is created from a case, account, and contact.  You could easily extend this for other templates and entities.  Currently I am setting them all to the same template, which won&#8217;t work.</p>
<pre name="code" class="javascript">
var CRM_FORM_TYPE_CREATE = 1;
if (crmForm.FormType==CRM_FORM_TYPE_CREATE)
{
    var otype = 0;
    var tmpid = '';
    switch (crmForm.all.regardingobjectid.DataValue[0].typename)
    {
        case 'incident': otype=112; tmpid='{F8E179BF-8E7C-DA11-BF9A-00142216345E}'; break;
        case 'account':  otype=1;   tmpid='{F8E179BF-8E7C-DA11-BF9A-00142216345E}'; break;
	case 'contact':  otype=2;   tmpid='{F8E179BF-8E7C-DA11-BF9A-00142216345E}'; break;
        default: return;
    }

    var oid = crmForm.all.regardingobjectid.DataValue[0].id;

    if (tmpid==undefined || tmpid==null) return;

    LoadTemplate(tmpid, oid, otype);
}
</pre>
<p>This could be extended using my <a href="http://mscrmblogger.com/2009/09/30/get-current-users-business-unit-for-hiding-fields/">business unit</a> lookup or getting roles assigned to a user..</p>
<p>Let me know if you have any questions &#8211; or if you figure out a way to get the id of a template from the name of one&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://mscrmblogger.com/2009/10/02/set-a-default-email-template-for-new-emails-on-cases/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hide Fields or Tabs by Role with Javascript</title>
		<link>http://mscrmblogger.com/2009/09/30/hide-fields-or-tabs-by-role-with-javascript/</link>
		<comments>http://mscrmblogger.com/2009/09/30/hide-fields-or-tabs-by-role-with-javascript/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 13:13:32 +0000</pubDate>
		<dc:creator>Carlton Colter</dc:creator>
				<category><![CDATA[Scripts]]></category>
		<category><![CDATA[crm]]></category>
		<category><![CDATA[crm4]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jscript]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[microsoft dynamics CRM 4]]></category>
		<category><![CDATA[onload event]]></category>
		<category><![CDATA[roles]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://mscrmblogger.com/?p=131</guid>
		<description><![CDATA[Do you need to hide fields or tabs based on security roles.  Here is an easy way to add the functionality to your CRM, use my code example.]]></description>
			<content:encoded><![CDATA[<p>Here are a set of methods to assist in hiding tabs or sections based on role, it is a modified version of <a href="http://jianwang.blogspot.com/2008/01/crm-40-check-current-users-security.html" target="_blank">Jimmy Wang&#8217;s version</a>.</p>
<p>First, we need a set of functions to facilitate our our process.  We need to get the roles of the current user, <b><i>GetCurrentUserRoles</i></b>.  Then we need to see if the user has the role, <b><i>UserHasRole</i></b>.  Finally we can hide the fields (<b><i>HideFieldByRole</i></b>) or tabs (<b><i>HideTabByRole</i></b>).</p>
<p>
<b>GetCurrentUserRoles</b><br />
The first thing we need to do is get a list of roles according to the current user.  There are a couple of different ways to do this.  You could use the RemoteCommand to get the current user and then their roles, a blog post by <a href="http://www.crowehorwath.com/cs/blogs/crm/archive/2008/05/08/hide-show-fields-in-crm-4-0-based-on-security-role.aspx" target="_blank">Zahara Hirani on Hide Show Fields in CRM 4 based on security role</a> outlines this.  However, I do not want to make multiple request for the same information.  I want to get the roles for the current user in one line.  Below shows the GetCurrentUserRoles that implements a RetrieveMultiple query to get the names of the roles that a user has in one request.</p>
<pre name="code" class="javascript">
function GetCurrentUserRoles()
{
  var xml = &quot;&quot; +
    &quot;&lt;?xml version=\&quot;1.0\&quot; encoding=\&quot;utf-8\&quot;?&gt;&quot; +
    &quot;&lt;soap:Envelope xmlns:soap=\&quot;&quot; +
    &quot;http://schemas.xmlsoap.org/soap/envelope/&quot; +
    &quot;\&quot; xmlns:xsi=\&quot;http://www.w3.org/2001/XMLSchema-instance\&quot;&quot; +
    &quot; xmlns:xsd=\&quot;http://www.w3.org/2001/XMLSchema\&quot;&gt;&quot; +
    GenerateAuthenticationHeader() +
    &quot; &lt;soap:Body&gt;&quot; +
    &quot; &lt;RetrieveMultiple xmlns=\&quot;&quot; +
    &quot;http://schemas.microsoft.com/crm/2007/WebServices\&quot;&gt;&quot; +
    &quot; &lt;query xmlns:q1=\&quot;&quot; +
    &quot;http://schemas.microsoft.com/crm/2006/Query&quot; +
    &quot;\&quot; xsi:type=\&quot;q1:QueryExpression\&quot;&gt;&quot; +
    &quot; &lt;q1:EntityName&gt;role&lt;/q1:EntityName&gt;&quot; +
    &quot; &lt;q1:ColumnSet xsi:type=\&quot;q1:ColumnSet\&quot;&gt;&quot; +
    &quot; &lt;q1:Attributes&gt;&quot; +
    &quot; &lt;q1:Attribute&gt;name&lt;/q1:Attribute&gt;&quot; +
    &quot; &lt;/q1:Attributes&gt;&quot; +
    &quot; &lt;/q1:ColumnSet&gt;&quot; +
    &quot; &lt;q1:Distinct&gt;false&lt;/q1:Distinct&gt;&quot; +
    &quot; &lt;q1:LinkEntities&gt;&quot; +
    &quot; &lt;q1:LinkEntity&gt;&quot; +
    &quot; &lt;q1:LinkFromAttributeName&gt;roleid&lt;/q1:LinkFromAttributeName&gt;&quot; +
    &quot; &lt;q1:LinkFromEntityName&gt;role&lt;/q1:LinkFromEntityName&gt;&quot; +
    &quot; &lt;q1:LinkToEntityName&gt;systemuserroles&lt;/q1:LinkToEntityName&gt;&quot; +
    &quot; &lt;q1:LinkToAttributeName&gt;roleid&lt;/q1:LinkToAttributeName&gt;&quot; +
    &quot; &lt;q1:JoinOperator&gt;Inner&lt;/q1:JoinOperator&gt;&quot; +
    &quot; &lt;q1:LinkEntities&gt;&quot; +
    &quot; &lt;q1:LinkEntity&gt;&quot; +
    &quot; &lt;q1:LinkFromAttributeName&gt;systemuserid&lt;/q1:LinkFromAttributeName&gt;&quot; +
    &quot; &lt;q1:LinkFromEntityName&gt;systemuserroles&lt;/q1:LinkFromEntityName&gt;&quot; +
    &quot; &lt;q1:LinkToEntityName&gt;systemuser&lt;/q1:LinkToEntityName&gt;&quot; +
    &quot; &lt;q1:LinkToAttributeName&gt;systemuserid&lt;/q1:LinkToAttributeName&gt;&quot; +
    &quot; &lt;q1:JoinOperator&gt;Inner&lt;/q1:JoinOperator&gt;&quot; +
    &quot; &lt;q1:LinkCriteria&gt;&quot; +
    &quot; &lt;q1:FilterOperator&gt;And&lt;/q1:FilterOperator&gt;&quot; +
    &quot; &lt;q1:Conditions&gt;&quot; +
    &quot; &lt;q1:Condition&gt;&quot; +
    &quot; &lt;q1:AttributeName&gt;systemuserid&lt;/q1:AttributeName&gt;&quot; +
    &quot; &lt;q1:Operator&gt;EqualUserId&lt;/q1:Operator&gt;&quot; +
    &quot; &lt;/q1:Condition&gt;&quot; +
    &quot; &lt;/q1:Conditions&gt;&quot; +
    &quot; &lt;/q1:LinkCriteria&gt;&quot; +
    &quot; &lt;/q1:LinkEntity&gt;&quot; +
    &quot; &lt;/q1:LinkEntities&gt;&quot; +
    &quot; &lt;/q1:LinkEntity&gt;&quot; +
    &quot; &lt;/q1:LinkEntities&gt;&quot; +
    &quot; &lt;/query&gt;&quot; +
    &quot; &lt;/RetrieveMultiple&gt;&quot; +
    &quot; &lt;/soap:Body&gt;&quot; +
    &quot;&lt;/soap:Envelope&gt;&quot; +
    &quot;&quot;;  

  var xmlHttpRequest = new ActiveXObject(&quot;Msxml2.XMLHTTP&quot;);  

  xmlHttpRequest.Open(&quot;POST&quot;, &quot;/mscrmservices/2007/CrmService.asmx&quot;, false);
  xmlHttpRequest.setRequestHeader(&quot;SOAPAction&quot;,
    &quot; http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple&quot;);
  xmlHttpRequest.setRequestHeader(&quot;Content-Type&quot;, &quot;text/xml; charset=utf-8&quot;);
  xmlHttpRequest.setRequestHeader(&quot;Content-Length&quot;, xml.length);
  xmlHttpRequest.send(xml);  

  var resultXml = xmlHttpRequest.responseXML;
  return(resultXml);
}
</pre>
<p>The next method is a simple method for searching the XML result from GetCurrentUserRoles.  By passing it the roles you are looking for and the roles the user has, it will determine if there is a match.  The roles it is passed is separated by a pipe (|).</p>
<pre name="code" class="javascript">
function UserHasRole(roleNames, rolesXML)
{
  // split roleNames on pipe
  var matchon = roleNames.split('|');

  if(rolesXML != null)
  {
    //select the node text
    var roles = rolesXML.selectNodes("//BusinessEntity/q1:name");
    if(roles != null)
    {
      for( i = 0; i < roles.length; i++)
      {
        for (j = 0; j < matchon.length; j++)
	{
	  // If there is a match, return true, found
	  if (roles[i].text == matchon[j]) return true;
        }
      }
    }
  }
  //otherwise return false
  return false;
}
</pre>
<p>Finally, we can now implement helper methods to allow the hiding of a tab or a field if a user DOES NOT have a specific role.</p>
</pre>
<pre name="code" class="javascript">
function HideTabByRole(role, roles, tabnumber)
// Tab number starts on 0
{
  var tab = document.getElementById('tab'+tabnumber+'Tab');
  var usrRole = UserHasRole(role, roles);
  if(!usrRole)
  {
    tab.style.display = "none";
  }
}

function HideFieldByRole(role, roles, field,cfield,dfield)
{
  var usrRole = UserHasRole(role, roles);
  if(!usrRole)
  {
    field.style.visibility = 'hidden';
    field.style.position = 'absolute';
    cfield.style.visibility = 'hidden';
    cfield.style.position = 'absolute';
    dfield.visibility = 'hidden';
    dfield.style.position = 'absolute';
  }
}
</pre>
<p>To implement this process, all you have to do is put every piece of code in this article inside your onload with the following customized for your situation.</p>
<pre name="code" class="javascript">
var UserRoles = GetCurrentUserRoles();

HideTabByRole('Account Managers|SystemAdministrators', UserRoles, 3);

HideFieldByRole('Account Managers', UserRoles,
                crmForm.all.parentcustomerid,
                crmForm.all.parentcustomerid_c,
                crmForm.all.parentcustomerid_d);
</pre>
<p>If you have any questions of problems implementing this in your environment, please let me know, and I&#8217;ll do my best to help you get it working</p>
]]></content:encoded>
			<wfw:commentRss>http://mscrmblogger.com/2009/09/30/hide-fields-or-tabs-by-role-with-javascript/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
