I am working with a开发者_StackOverflow list view. I want a column to have look similar to the document library column named "Type (icon linked to document)" column. I should also be able to set the path this hyperlinked icon should open. I tried a lot with existing site columns but could still not figure out how to do this. Has anyone implemented this earlier. Please share your expertise. Thanks in advance.
In terms of out-of-the-box capability, you cannot directly implement such a column exactly. The Type (icon linked to document) uses a computed field type. Setting those up, especially for a simple objective as this, is very complicated and requires deploying solutions. However, there are other ways to accomplish this.
The basic thing you want to achieve is a linked picture, basically. A combination of the URL field's Hyperlink functionality and picture functionality. There are two approaches you can take to this which can be done without needing to deploy a solution. I'll list both and you can try whichever one, or both, that you feel is more to your taste.
Option One: URL Field Modification
This one involves changing the FLDTYPES.XML located in the 12 hive on your SharePoint server. Naturally, it is recommended you make a back-up of your current file before doing this. The FLDTYPES.XML is located at \Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\XML. This change will let us use the default URL field in the SharePoint UI, when configured to show a HyperLink, to show a picture under specified circumstances.
After you back up the file as something lke FLDTYPES.OLD or what-have-you, open FLDTYPES.XML in a text editor and search for the URL field. The line <Field Name="TypeName">URL</Field>
will be at the start, so just search for that. Now, in the large fieldtype definition will be a field switch. Except for the comment that I manually added right now, it looks like the following.
<FieldSwitch>
<Expr><Property Select="Format"/></Expr>
<Case Value="Image">
<FieldSwitch>
<Expr><Property Select="Width"/></Expr>
<Case Value="">
<HTML><![CDATA[<IMG SRC="]]></HTML><Column HTMLEncode="TRUE"/><HTML>" ALT="</HTML><Column2 HTMLEncode="TRUE"/><HTML><![CDATA[">]]></HTML>
</Case>
<Default>
<HTML><![CDATA[<IMG SRC="]]></HTML><Column HTMLEncode="TRUE"/><HTML>" ALT="</HTML><Column2 HTMLEncode="TRUE"/><HTML><![CDATA[" WIDTH="]]></HTML><Property Select="Width"/><HTML><![CDATA[" HEIGHT="]]></HTML><Property Select="Height"/><HTML><![CDATA["> ]]></HTML>
</Default>
</FieldSwitch>
</Case>
<Default>
<HTML><![CDATA[<A HREF="]]></HTML><Column HTMLEncode="TRUE"/><HTML><![CDATA[">]]></HTML>
<Switch>
<Expr><Column2/></Expr>
<Case Value="">
<Column HTMLEncode="TRUE"/>
</Case>
<Default>
<Column2 HTMLEncode="TRUE"/> <!-- This is where we work -->
</Default>
</Switch>
<HTML><![CDATA[</A>]]></HTML>
</Default>
</FieldSwitch>
Now, we want to replace the <Column2 HTMLEncode="TRUE"/>
at the line I put the comment on. This is the logic for what displays for a URL field when it is set to HyperLink, and the description is filled out, which currently will just show that description. We want to change it so that when it this description is the URL of an image, then it will show that image. The following is an example of what I use.
<Switch>
<Expr>
<GetFileExtension>
<Column2/>
</GetFileExtension>
</Expr>
<Case Value="pnG">
<HTML><![CDATA[<IMG STYLE="border:0" SRC="]]></HTML>
<Column2 HTMLEncode="TRUE"/>
<HTML><![CDATA[">]]></HTML>
</Case>
<Default>
<Column2 HTMLEncode="TRUE"/>
</Default>
</Switch>
Basically, it treats the description as a filename, checks if it has an extension, and in the case of a match, it will render it as an image without a border. Now, I use pnG because this switch-case comparison is case-sensitive, but the source url for an image is not. So, when I create a field and specify the description as "/_layouts/images/FlagRed.pnG", then it will show the FlagRed picture that I have saved as "/_layouts/images/FlagRed.png". This way, when I do want to show an actual image text url, then I can just not capitalize the end. Of course, you'll have to substitute the "pnG" in the case statement for whatever file extension you use, and add extra cases when you have more. Save the file when you are done.
After that is all setup, run IISRESET on the machine. Once this is complete, your URL fields will now be able to render image links. All you have to do when creating a new URL field is specify it as a HyperLink type, put the destination link as the URL, and put the image source url as the Description. Add the URL column to your list view the same way you'd do with any other field type.
Option Two: Calculated HTML Fields
Instead of messing with the server, you can instead use some javascript in a Content Editor Web Part. Basically, insert a Content Editor Web Part on the page with your list view, make sure it is below the list view. By inputting a script in this CEWP, you can run it to change the display for the list view. This particular case, we use a script that allows HTML to be rendered as HTML in a Calculated column.
The following should have all the resources you need to get the script: Using Calculated Columns to Write HTML. There's a lot of reading here, but there is a lot of potential you can get from implementing this script, so I recommend it. When you've finally setup the CEWP with the script, we'll need two fields in your list.
- One is a text field, this won't be shown in your list view but it will be on your edit form. This will be where they specify the hyperlink's path.
- Then you'll need to create a Calculated Column that outputs as a single line of text. Use a formula like the following:
=CONCATENATE("<a href='",{0},"'><img src='",{1},"' style='border:0' /></a>")
- Replace {0} with the name of text field you created in square brackets. So, if you called it "Path", then it should be [Path].
- Replace {1} with the image source url for your icons. If there are multiple options for the image, you either have to add another text field to specify the image url, or use some IF logic. Check this article for the syntax of Calculated Column formulas to set up that logic.
Add the calculated column to your list view. Now, when the text field is filled out, you'll get your image link.
Hope that at least one of these two helps!
精彩评论