Quantcast
Channel: The Blog - SharePoint
Viewing all articles
Browse latest Browse all 32

SharePoint: How to replace filename with title

0
0

Sometimes, users want to display a meta data such as title instead of filename which is SharePoint default. It is a quite tricky task and not easily done. I tried to find the most elegant way to achieve it and finally found a very good article:

HOWTO Move the ECB from the Filename to Title, in a DocLib, preserving all native functionality.

Actually, he explained very well why we need it and how to make it. Even he provide a working code. Thanks a lot!

For me, it's a same situation but slightly different solution. I used to use feature activation to add the new content type.

First, you need to define your own content type:

<Field ID="{8D8B5AEC-F908-445F-9532-86492BBC22B2}" 
		Name="TitleWithLink"
		StaticName="TitleWithLink"
		DisplayName="Title" 
		Type="Computed" 
		ShowInDisplayForm="FALSE" 
		ShowInEditForm="FALSE" 
		ShowInNewForm="FALSE"
		ReadOnly="TRUE"
		Description="Link to Title or FileName"
		AuthoringInfo="(Title linked to document with edit menu)"
		Overwrite="TRUE"
		ClassInfo="Menu"><FieldRefs><FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title"/><FieldRef ID="{687c7f94-686a-42d3-9b67-2782eac4b4f8}" Name="FileLeafRef" /><FieldRef ID="{56605df6-8fa1-47e4-a04c-5b384d59609f}" Name="FileDirRef" /><FieldRef ID="{30bb605f-5bae-48fe-b4e3-1f81d9772af9}" Name="FSObjType" /><FieldRef ID="{1d22ea11-1e32-424e-89ab-9fedbadb6ce1}" Name="ID" /><FieldRef ID="{105f76ce-724a-4bba-aece-f81f2fce58f5}" Name="ServerUrl" /><FieldRef ID="{0c5e0085-eb30-494b-9cdd-ece1d3c649a2}" Name="HTML_x0020_File_x0020_Type" /><FieldRef ID="{39360f11-34cf-4356-9945-25c44e68dade}" Name="File_x0020_Type" /><FieldRef ID="{9d30f126-ba48-446b-b8f9-83745f322ebe}" Name="LinkFileNameNoMenu" /><FieldRef ID="{BA3C27EE-4791-4867-8821-FF99000BAC98}" Name="PermMask" /><FieldRef ID="{1df5e554-ec7e-46a6-901d-d85a3881cb18}" Name="Author" /><FieldRef ID="{3881510a-4e4a-4ee8-b102-8ee8e2d0dd4b}" Name="CheckedOutUserId" /><FieldRef ID="{c63a459d-54ba-4ab7-933a-dcf1c6fadec2}" Name="_SourceUrl" /><FieldRef ID="{26d0756c-986a-48a7-af35-bf18ab85ff4a}" Name="_HasCopyDestinations" /><FieldRef ID="{6b4e226d-3d88-4a36-808d-a129bf52bccf}" Name="_CopySource" /><FieldRef ID="{fdc3b2ed-5bf2-4835-a4bc-b885f3396a61}" Name="_ModerationStatus" /><FieldRef ID="{7841bf41-43d0-4434-9f50-a673baef7631}" Name="_UIVersion" /><FieldRef ID="{03e45e84-1992-4d42-9116-26f756012634}" Name="ContentTypeId" /></FieldRefs></Field><!-- Parent ContentType: Document (0x0101) --><ContentType ID="0x0101009a9eeb8ac41b4abeb5db9580c647dc31"
               Name="Custom Document"
               Group="Custom Content Types"
               Description="Custom Document Type"
               Inherits="TRUE"
               Version="1"
               Overwrite="TRUE"><FieldRefs><FieldRef<FieldRef
		ID="{8D8B5AEC-F908-445F-9532-86492BBC22B2}"
		Name="TitleWithLink" /></FieldRefs></ContentType>

Second, add xsl file in the Layouts folder:

<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema"
                xmlns:d="http://schemas.microsoft.com/sharepoint/dsp"
                version="1.0" exclude-result-prefixes="xsl msxsl ddwrt"
                xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"
                xmlns:asp="http://schemas.microsoft.com/ASPNET/20"
                xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:SharePoint="Microsoft.SharePoint.WebControls"
                xmlns:ddwrt2="urn:frontpage:internal" ddwrt:oob="true"><xsl:output method="html" indent="no"/><xsl:template match="FieldRef[@Name='TitleWithLink']" mode="Computed_body" priority="9.0"><xsl:param name="thisNode" select="."/><xsl:param name="ShowAccessibleIcon" select="0"/><xsl:param name="folderUrlAdditionalQueryString" select="''"/><xsl:param name="Position" select="1" /><xsl:variable name="ID"><xsl:call-template name="ResolveId"><xsl:with-param name="thisNode" select ="$thisNode"/></xsl:call-template></xsl:variable><xsl:variable name="PermMask"><xsl:choose><xsl:when test="$thisNode/@PermMask != ''"><xsl:value-of select="$thisNode/@PermMask"/></xsl:when><xsl:otherwise><xsl:value-of select="$ExternalDataListPermissions"/></xsl:otherwise></xsl:choose></xsl:variable><xsl:choose><xsl:when test="$EcbMode or $NoAJAX"><xsl:choose><xsl:when test="$MasterVersion=4"><!-- Client JS uses 'itx' string to decide whether to create AJAX menu or not --><!-- If AJAX is enabled, then we must include the 'itx' string at the end of the class --><xsl:variable name="ClassName"><xsl:choose><xsl:when test="$NoAJAX or $EcbMode">ms-vb</xsl:when><xsl:otherwise>ms-vb itx</xsl:otherwise></xsl:choose></xsl:variable><div class="{$ClassName}" onmouseover="OnItem(this)" CTXName="ctx{$ViewCounter}" id="{$ID}" Field="{@Name}"
						  Url="{$thisNode/@FileRef.urlencodeasurl}" DRef="{$thisNode/@FileDirRef}" Perm="{$PermMask}" Type="{$thisNode/@HTML_x0020_File_x0020_Type}"
						  Ext="{$thisNode/@File_x0020_Type}"
						  OType="{$thisNode/@FSObjType}"
						  COUId="{$thisNode/@CheckedOutUserId}" HCD="{$thisNode/@_HasCopyDestinations.value}"
						  CSrc="{$thisNode/@_CopySource}" MS="{$thisNode/@_ModerationStatus.}" CType="{$thisNode/@ContentType}"
						  CId="{$thisNode/@ContentTypeId}" UIS="{$thisNode/@_UIVersion}" SUrl="{$thisNode/@_SourceUrl}"
						  Icon="{$thisNode/@HTML_x0020_File_x0020_Type.File_x0020_Type.mapall}"
						  EventType="{$thisNode/@EventType}"><xsl:if test="$IsDocLib"><xsl:attribute name="sred"><xsl:value-of select="$thisNode/@serverurl.progid"/></xsl:attribute><xsl:attribute name="defaultio"><xsl:value-of select="$XmlDefinition/List/@DefaultItemOpen"/></xsl:attribute><xsl:attribute name="cout"><xsl:value-of select="$thisNode/@IsCheckedoutToLocal"/></xsl:attribute></xsl:if><xsl:call-template name="TitleOrFileNameBody"><xsl:with-param name="thisNode" select="$thisNode" /><xsl:with-param name="ShowAccessibleIcon" select="$ShowAccessibleIcon"/><xsl:with-param name="folderUrlAdditionalQueryString" select="$folderUrlAdditionalQueryString"/></xsl:call-template></div><!-- render the markup for list item chevron from server side --><div class="s4-ctx" onmouseover="OnChildItem(this.parentNode); return false;"><span>&#160;</span><a onfocus="OnChildItem(this.parentNode.parentNode); return false;" onclick="PopMenuFromChevron(event); return false;" href="javascript:;" title="{$open_menu}"></a><span>&#160;</span></div></xsl:when><xsl:otherwise><table height="100%" cellspacing="0" class="ms-unselectedtitle" onmouseover="OnItem(this)" CTXName="ctx{$ViewCounter}" id="{$ID}"
						 Url="{$thisNode/@FileRef.urlencodeasurl}" DRef="{$thisNode/@FileDirRef}" Perm="{$PermMask}" Type="{$thisNode/@HTML_x0020_File_x0020_Type}"
						 Ext="{$thisNode/@File_x0020_Type}"
						 OType="{$thisNode/@FSObjType}"
						 COUId="{$thisNode/@CheckedOutUserId}" HCD="{$thisNode/@_HasCopyDestinations.value}"
						 CSrc="{$thisNode/@_CopySource}" MS="{$thisNode/@_ModerationStatus.}" CType="{$thisNode/@ContentType}"
						 CId="{$thisNode/@ContentTypeId}" UIS="{$thisNode/@_UIVersion}" SUrl="{$thisNode/@_SourceUrl}"
						 DocIcon="{$thisNode/@HTML_x0020_File_x0020_Type.File_x0020_Type.mapall}"
						 EventType="{$thisNode/@EventType}"><xsl:if test="$IsDocLib"><xsl:attribute name="sred"><xsl:value-of select="$thisNode/@serverurl.progid"/></xsl:attribute><xsl:attribute name="defaultio"><xsl:value-of select="$XmlDefinition/List/@DefaultItemOpen"/></xsl:attribute><xsl:attribute name="cout"><xsl:value-of select="$thisNode/@IsCheckedoutToLocal"/></xsl:attribute></xsl:if><tr><td width="100%" class="ms-vb"><xsl:call-template name="TitleOrFileNameBody"><xsl:with-param name="thisNode" select="$thisNode" /><xsl:with-param name="ShowAccessibleIcon" select="$ShowAccessibleIcon"/><xsl:with-param name="folderUrlAdditionalQueryString" select="$folderUrlAdditionalQueryString"/></xsl:call-template></td><td><img src="http://www.codeproject.com/_layouts/images/blank.gif" width="13" style="visibility:hidden" alt="" /></td></tr></table></xsl:otherwise></xsl:choose></xsl:when><xsl:otherwise><xsl:choose><xsl:when test="$MasterVersion=4"><div class="ms-vb itx" onmouseover="OnItem(this)" CTXName="ctx{$ViewCounter}" id="{$ID}" Field="{@Name}" Perm="{$PermMask}" EventType="{$thisNode/@EventType}"><xsl:call-template name="TitleOrFileNameBody"><xsl:with-param name="thisNode" select="$thisNode" /><xsl:with-param name="ShowAccessibleIcon" select="$ShowAccessibleIcon"/><xsl:with-param name="folderUrlAdditionalQueryString" select="$folderUrlAdditionalQueryString"/></xsl:call-template></div><!-- render the markup for list item chevron from server side --><div class="s4-ctx" onmouseover="OnChildItem(this.parentNode); return false;"><span>&#160;</span><a onfocus="OnChildItem(this.parentNode.parentNode); return false;" onclick="PopMenuFromChevron(event); return false;" href="javascript:;" title="{$open_menu}"></a><span>&#160;</span></div></xsl:when><xsl:otherwise><table height="100%" cellspacing="0" class="ms-unselectedtitle itx" onmouseover="OnItem(this)" CTXName="ctx{$ViewCounter}" id="{$ID}" Field="{@Name}" Perm="{$PermMask}" EventType="{$thisNode/@EventType}"><tr><td width="100%" class="ms-vb"><xsl:call-template name="TitleOrFileNameBody"><xsl:with-param name="thisNode" select="$thisNode" /><xsl:with-param name="ShowAccessibleIcon" select="$ShowAccessibleIcon"/><xsl:with-param name="folderUrlAdditionalQueryString" select="$folderUrlAdditionalQueryString"/></xsl:call-template></td><td><img src="http://www.codeproject.com/_layouts/images/blank.gif" width="13" style="visibility:hidden" alt="" ddwrt:insideECB=""/></td></tr></table></xsl:otherwise></xsl:choose></xsl:otherwise></xsl:choose></xsl:template><xsl:template name="TitleOrFileNameBody"><xsl:param name="thisNode" select="."/><xsl:param name="ShowAccessibleIcon" select="0"/><xsl:param name="folderUrlAdditionalQueryString" select="''"/><xsl:choose><!-- if it's a folder, call LinkFilenameNoMenu and let the system do it's normal thing with it--><xsl:when test="$thisNode/@FSObjType='1'"><xsl:call-template name="LinkFilenameNoMenu"><xsl:with-param name="thisNode" select="$thisNode"/><xsl:with-param name="folderUrlAdditionalQueryString" select="$folderUrlAdditionalQueryString"/></xsl:call-template></xsl:when><xsl:otherwise><!-- warning: this code has optimization in webpart. Change it must change the webpart code too!--><a onfocus="OnLink(this)" href="{$thisNode/@FileRef}" onmousedown="return VerifyHref(this,event,'{$XmlDefinition/List/@DefaultItemOpen}','{$thisNode/@HTML_x0020_File_x0020_Type.File_x0020_Type.mapcon}','{$thisNode/@serverurl.progid}')"
				   onclick="return DispEx(this,event,'TRUE','FALSE','{$thisNode/@File_x0020_Type.url}','{$thisNode/@File_x0020_Type.progid}','{$XmlDefinition/List/@DefaultItemOpen}','{$thisNode/@HTML_x0020_File_x0020_Type.File_x0020_Type.mapcon}','{$thisNode/@HTML_x0020_File_x0020_Type}','{$thisNode/@serverurl.progid}','{$thisNode/@CheckoutUser.id}','{$Userid}','{$XmlDefinition/List/@ForceCheckout}','{$thisNode/@IsCheckedoutToLocal}','{$thisNode/@PermMask}')"><xsl:call-template  name="TitleValue"><xsl:with-param name="thisNode" select="$thisNode"/><xsl:with-param name="ShowAccessibleIcon" select="$ShowAccessibleIcon"/></xsl:call-template></a><xsl:if test="$thisNode/@Created_x0020_Date.ifnew='1'"><xsl:call-template name="NewGif"><xsl:with-param name="thisNode" select="$thisNode"/></xsl:call-template></xsl:if></xsl:otherwise></xsl:choose></xsl:template><xsl:template name="TitleValue"><xsl:param name="thisNode" select="."/><xsl:param name="ShowAccessibleIcon" select="0"/><xsl:variable name="titlevalue" select="$thisNode/@Title"/><xsl:choose><xsl:when test="$titlevalue=''"><!-- no title? empty title? k. filename. --><xsl:value-of select="$thisNode/@FileLeafRef.Name" /></xsl:when><xsl:otherwise><xsl:choose><xsl:when test="$HasTitleField"><!-- if it's also at top level, it's already encoded, decode it.--><xsl:value-of disable-output-escaping="yes" select="$titlevalue" /></xsl:when><xsl:otherwise><xsl:choose><xsl:when test="string-length($titlevalue) = 0"><!-- no title? empty title? k. filename. --><xsl:value-of select="$thisNode/@FileLeafRef.Name" /></xsl:when><xsl:otherwise><xsl:value-of select="$titlevalue" /></xsl:otherwise></xsl:choose></xsl:otherwise></xsl:choose></xsl:otherwise></xsl:choose><xsl:choose><xsl:when test="$ShowAccessibleIcon"><img src="http://www.codeproject.com/_layouts/images/blank.gif" class="ms-hidden" border="0" width="1" height="1" alt="{$idPresEnabled}" /></xsl:when><xsl:otherwise></xsl:otherwise></xsl:choose></xsl:template></xsl:stylesheet>

Next, add this new file type to document list

if (!oList.Fields.ContainsFieldWithStaticName("TitleWithLink"))
                        {
                            oList.Fields.Add(web.AvailableFields[new Guid("8D8B5AEC-F908-445F-9532-86492BBC22B2")]);
                            oList.Update();
                        }
var view = oList.DefaultView;
view.ViewFields.DeleteAll();
view.ViewFields.Add("DocIcon");
view.ViewFields.Add("TitleWithLink");
view.ViewFields.Add("Modified");

 

Finally, deploy it and activate the feature, you will have a linked title with menu (called the Edit Control Block Menu or just "ECB menu")

 

 

 


Viewing all articles
Browse latest Browse all 32

Latest Images

Trending Articles





Latest Images