1

I have a custom field for storing an attachment name. I'm trying to display url for this attachment on list view instead of just the name.

I've tried to overide GetFieldValue in field definition class, but I couldn't find any way to get item id required to generate url.

public override object GetFieldValue(string value)
        {
            var id = ((ARPFileFieldControl)FieldRenderingControl).ListItem.ID;
            string html = String.Format("<a href=\"{0}\">{1}</a>", String.Format("Attachments/{0}/{1}", id, value), value);
            return base.GetFieldValue(html);
        }

This works only when I click the item on list view. When page loads it throws exception because ListItem is not initialized.

I've tried to create custom xsl to do the job, but also without success.

  <xsl:template match="FieldRef[@FieldType='Field Uploader']" mode="body" >
    <xsl:param name="thisNode" select="." />

    <a href="{$RootSiteUrl}/Attachments/{$thisNode/@ID}/{$thisNode/@*[name()=current()/@Name]}"><xsl:value-of select="$thisNode/@*[name()=current()/@Name]" /></a>

  </xsl:template >

When I try to match field by ID it works fine, but that's not an option because I use this field multiple times and want it to be used in future without changing code every time someone adds new list.

Are there any other ways I can achieve this?

3 Answers 3

1
Did you try this :

1-Display your library (Example: Shared Documents)
2-Click Settings and Library Settings
3-Click Advanced Settings
4-Check "Allow management of content types" and then OK
5-In the Settings page scroll down to Content Types and click "Add from 
existing 6-site content types"
7-Select "Link to a Document", click Add and then Click OK
8-Go back to your library and click the New dropdown and select "Link to a 
Document"
Enter a display name (Document Name) and the URL (must start with http://) 
and click OK

Click this link: Click

0

You can do one thing, create one event receiver on item adding to set your URL one column and access that column in your XSLT.It will never cause an issue as you don't have to worry about your custom column data SharePoint will look after it.

To add Event receiver use following code :

    public override void ItemAdded(SPItemEventProperties properties)  
    {  
        base.ItemAdded(properties);  
        using (SPWeb web = properties.OpenWeb())  
        {  
            try  
            {  
                SPListItem currentItem = properties.ListItem;  
                currentItem["CustomField"] =//set your URL;                     
                currentItem.Update();  
            }  
            catch (Exception ex)  
            {  
                throw ex;  
            }  
        }

http://www.c-sharpcorner.com/UploadFile/5b1f04/create-a-custom-list-level-event-receiver-for-item-added-in/

2
  • 1
    Note that a Calculated Column can not get the ID value, only on Item Update or Formula update will that value be set/calculated in the Formula output. So you need an extra Workflow to save that ID. OR do it all Client-Side and extract the ID from the TR iid attribute Commented May 8, 2017 at 9:56
  • yes agree with Danny, you can set your field on item added or updated event receiver. Commented May 8, 2017 at 10:00
0

You could get that ID for your URL/Hyperlink Client Side, when the link is clicked

also does not require any server Side code

Undocumented by Microsoft, so not supported, but has worked fine for the past 8 years..

Set a Calculated Column to output as Number, then it executes Strings as HTML

Formula:

="<a onclick=""{var ID=GetAncestor(this,'TR').id.split(',')[1];"
&"alert(String.format('Attachments/{0}/{1}', ID, '" & [value] &"'));"
&"}"">"
&"open Attachment" & [value]
&"</a>"

and then instead of an alert trigger a window.location

Be aware of all Pros & Cons: http://www.viewmaster365.com/#/How

iCSR

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.