Conditional Content in SharePoint Markup

In SharePoint, there are several web parts that allow us to have different contents depending on some conditions:

  • LoginView (ASP.NET): allows the definition of templates per authenticated or anonymous user:
<asp:LoginView runat="server">


        <!-- anonymous content -->



        <!-- authenticated content -->



        <asp:RoleGroup Roles="Admin">


                <!-- admin content -->





<SharePoint:SPSecurityTrimmedControl runat="server" PermissionMode="All" PermissionContext="CurrentSite" Permissions="ManageWeb">

    <!-- secure content -->


  • EditModePanel: for displaying contents in a web part page depending on its edit mode:
<PublishingWebControls:EditModePanel runat="server" PageDisplayMode="Edit">

    <!-- edit content -->


<SharePoint:UIVersionedContent runat="server" UIVersion="4">


        <!-- content for SharePoint 2010 -->

        <!-- no code is run if does not match UIVersion -->





<SharePoint:VersionedPlaceholder runat="server" UIVersion="4">

    <!-- content for SharePoint 2010 -->

    <!-- code is always run but not rendered if does not match UIVersion -->


  • AuthoringContainer: displays content depending on whether the current user has write or read rights on the current page or if it has an associated list item:
<PublishingWebControls:AuthoringContainer runat="server" DisplayAudience="ReadersOnly">

    <!-- content for readers -->


<PublishingWebControls:DeviceChannelPanel runat="server" IncludedChannels="iPhone">

    <!-- content for iPhones -->


  • DataViewWebPart: allows the passing of parameters and the usage of XSL for rendering logic;

I imagine you are now rolling your eyes: DataViewWebPart? how come? Well, because it doesn’t need to point to a specific list or view (unlike XsltListViewWebPart), it is very useful for markup-based customizations that will only depend on parameters:

   1: <WebPartPages:DataFormWebPart runat="server" Title="Conditional Content">

   2:     <ParameterBindings>

   3:         <ParameterBinding Name="MyParameter" Location="QueryString(MyParameter)"/>

   4:     </ParameterBindings>

   5:     <XSL>

   6:         <xsl:stylesheet version="1.0" xmlns:xsl="" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl asp" xmlns:asp="System.Web.UI.WebControls">                                                  

   7:             <xsl:param name="MyParameter"/>                                                                                                                                        

   8:             <xsl:template match="/">

   9:                 <asp:Label runat="server" Text="Some content, just to show ASP.NET controls inside a SharePoint DataFormWebPart"/>

  10:                 <xsl:choose>

  11:                     <xsl:when test="$MyParameter=''">

  12:                         No parameter...

  13:                     </xsl:when>

  14:                     <xsl:otherwise>

  15:                         Allright! <xsl:value-of select="$MyParameter"/>

  16:                     </xsl:otherwise>

  17:                 </xsl:choose>                                                                                                

  18:             </xsl:template>

  19:         </xsl:stylesheet>

  20:     </XSL>                                        

  21: </WebPartPages:DataFormWebPart>

You can use this technique for:

  • Including scripts and stylesheets;
  • Including server-side controls.

It’s just a matter of rolling out some XSL to the rescue!

You may be already familiar with the available parameters, but you can find the full list here:

Another SharePoint Designer-only solution that may come in handy! 😉

Published by

Ricardo Peres

Technical Evangelist at Simplifydigital. Microsoft MVP.

Leave a Reply

Your email address will not be published. Required fields are marked *