Create edit item link in Xslt

11/10/2013 11:19

Create Edit item link for Content query web part

 

Sometimes you want to create edit link to list item (not the aspx page) in Content query web part. In this example, Item title is used like header and contains link to item itself in popup window.

 

Use this templates and code in ItemStyle.xsl 

 

<!--Create edit item link-->
      <xsl:variable name="trimmedUrl">
                 <xsl:call-template name="trimFileNameFromUrl">
                   <xsl:with-param name="Url"
                        select="@FileRef"/>
                 </xsl:call-template>
      </xsl:variable>
     <div class="itemHeader">
   <h3>
    <a href="javascript:NewItem2(event,'{concat($trimmedUrl, '/DispForm.aspx?ID=', @ID)}')">
      <xsl:value-of select="@Title" disable-output-escaping="yes"/>
    </a>
     </h3>
    </div>

  <xsl:template name="trimFileNameFromUrl">
    <xsl:param name="Url"/>
    <!--Reverse string first-->
    <xsl:variable name="reversedUrl">
      <xsl:call-template name="reverse">
        <xsl:with-param name="input"
             select="$Url"/>
      </xsl:call-template>
    </xsl:variable>
    <!--Get substring after /-->
    <xsl:variable name="trimmedUrl">
      <xsl:value-of select='substring-after($reversedUrl, "/")'/>
    </xsl:variable>
    <!--Reverse string back-->
    <xsl:variable name='result'>
      <xsl:call-template name="reverse">
        <xsl:with-param name="input"
             select="$trimmedUrl"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="$result"/>
  </xsl:template>
 
 
  <xsl:template name="reverse">
    <xsl:param name="input"/>
    <xsl:variable name="len" select="string-length($input)"/>
    <xsl:choose>
      <!-- Strings of length less than 2 are trivial to reverse -->
      <xsl:when test="$len &lt; 2">
        <xsl:value-of select="$input"/>
      </xsl:when>
      <!-- Strings of length 2 are also trivial to reverse -->
      <xsl:when test="$len = 2">
        <xsl:value-of select="substring($input,2,1)"/>
        <xsl:value-of select="substring($input,1,1)"/>
      </xsl:when>
      <xsl:otherwise>
        <!-- Swap the recursive application of this template to
               the first half and second half of input -->
        <xsl:variable name="mid" select="floor($len div 2)"/>
        <xsl:call-template name="reverse">
          <xsl:with-param name="input"
               select="substring($input,$mid+1,$mid+1)"/>
        </xsl:call-template>
        <xsl:call-template name="reverse">
          <xsl:with-param name="input"
               select="substring($input,1,$mid)"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>