Dynamics CRM 2011 Portal Development – Snippet/SiteSetting Expression
Dynamics CRM 2011 Portal Development
Snippet Expression
More information regarding to ASP.NET expression can be found ASP.NET Expressions Overview and ExpressionBuilder Class.
There are 7 expression builders contained in microsoft.xrm.portal.dll assembly.
- CrmSiteMapExpressionBuilder
- CurrentContactExpressionBuilder
- PortalContextExpressionBuilder
- SiteSettingExpressionBuilder
- SnippetExpressionBuilder
- TimeZoneExpressionBuilder
- WebsiteExpressionBuilder
Expression builders need to be registered in web.config
<configuration> <system.web> <compilation debug="true" targetFramework="4.0"> <expressionBuilders> <add expressionPrefix="Snippet" type="Microsoft.Xrm.Portal.Web.Compilation.SnippetExpressionBuilder, Microsoft.Xrm.Portal"/> <add expressionPrefix="SiteSetting" type="Microsoft.Xrm.Portal.Web.Compilation.SiteSettingExpressionBuilder, Microsoft.Xrm.Portal"/> <add expressionPrefix="CrmSiteMap" type="Microsoft.Xrm.Portal.Web.Compilation.CrmSiteMapExpressionBuilder, Microsoft.Xrm.Portal"/> <add expressionPrefix="Context" type="Microsoft.Xrm.Portal.Web.Compilation.PortalContextExpressionBuilder, Microsoft.Xrm.Portal"/> </expressionBuilders> </compilation> </system.web> </configuration>
Snippet Expression
Snippet Expression evaluated to Value attribute (adx_value) of Content Snippet entity instance (adx_contentsnippet) contained in portalbase solution.
Syntax:
<%$ Snippet: Name=somename, Default=defaultvalue, Format=stringformat, Portal=someportal %> <%$ Snippet: somename, defaultvalue, stringformat, someportal %>
Snippet Expression takes a format of comma separated name value pairs, any pair is optional and the name of the pair is optional as well. If name is omitted, then the sequence is significant.
The first name value pair (Name) is the value of Name attribute (adx_name) of Content Snippet entity (adx_contentsnippet) contained in portalbase solution. The value of the expression will be evaluated as the value of Value attribute (adx_value) of the same entity instance.
The second name value pair (Default) is the value to be taken for the result of the expression evaluation if an instance of Content Snippet entity (adx_contentsnippet) with the specified Name attribute (adx_name) does not exist for the portal in the connected Microsoft Dynamics CRM 2011 instance.
The third name value pair (Format) is going to be passed as a parameter to string.Format(format, adx_value) and the value is substituted for the expression syntax.
The fourth name value pair (Portal) is checked against configured portals in the web.config file and used to create IPortalContext instance
<microsoft.xrm.portal> <portals> <add name="Customer Portal"/> </portals> </microsoft.xrm.portal>
This value should be the value of Name attribute (adx_name) of Website entity (adx_website) contained in portalbase solution.
Assume we have the following website created in CRM
two different snippets created in each website
two portals configured in web.config for each website inside CRM
<microsoft.xrm.portal> <portals> <add name="Customer Portal"/> <add name="Test Portal"/> </portals> </microsoft.xrm.portal>
The following expression
<table> <thead> <tr> <th>Name</th> <th>Default</th> <th>Format</th> <th>Portal</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>Custom Snippet A</td> <td>defaultA</td> <td>Value: {0}</td> <td>Customer Portal</td> <td> <asp:Literal runat="server" Text="<%$ Snippet: Name=Custom Snippet A, Default=defaultA, Format=Value: {0}, Portal=Customer Portal %>" /> </td> </tr> <tr> <td>Custom Snippet A</td> <td>defaultA</td> <td>Value: {0}</td> <td>Test Portal</td> <td> <asp:Literal runat="server" Text="<%$ Snippet: Name=Custom Snippet A, Default=defaultA, Format=Value: {0}, Portal=Test Portal %>" /> </td> </tr> <tr> <td>Test Snippet B</td> <td>defaultB</td> <td>Value: {0}</td> <td>Customer Portal</td> <td> <asp:Literal runat="server" Text="<%$ Snippet: Name=Test Snippet B, Default=defaultB, Format=Value: {0}, Portal=Customer Portal %>" /> </td> </tr> <tr> <td>Test Snippet B</td> <td>defaultB</td> <td>Value: {0}</td> <td>Test Portal</td> <td> <asp:Literal runat="server" Text="<%$ Snippet: Name=Test Snippet B, Default=defaultB, Format=Value: {0}, Portal=Test Portal %>" /> </td> </tr> </tbody> </table>
will produce the result
SiteSetting Expression
SiteSetting Expression works the same way as Snippet Expression. It takes the same 4 name and value pairs but read value from Site Settings (adx_sitesetting) entity contained in portalbase solution
This was originally posted here.
*This post is locked for comments