简体   繁体   中英

SharePoint 2013 How to get list item attachment url programmatically?

I have an SPlist (ina publishing site) with the following fields: "Title", "MessageContent", "ID", "Attachments". In my ListView control I present the contents of this SPlist in a formatted table.

In the listview I have successfully bounded the properties to the child controls and I am presenting the data correctly. I need the following logic: If there is an attachment for this item, then display the paperclip image as hyperlink to the attachment. Image is located in this sharepoint site's images library. How would you do it? Note (I am not using for each loop to display the content. I am using the existing control functionality and bind each property (SPlist field from datatable)). Thanks in advance.

Code: Ascx

<ItemTemplate>
    <td class="tdItemMessage">
        <table class="tableItemMessage">
            <tr>
                <td class="tdMessageConent tdMessageCreated">
                    <div class="divMessageCreated">
                        <asp:Label ID="lblMessageDate" runat="server" OnDataBinding="lblMessageDate_DataBinding" Text='<%# Bind("Created")%>'></asp:Label></div>
                </td>
                <td class="tdMessageConent">
                    <div class="divMessageTitle"><%# Eval("Title")%> </div>
                </td>
            </tr>
            <tr>
                <td class="tdMessageConent tdMessageCreated">
                </td>

                <td class="tdMessageConent">
                    <div class="divMessageShortMessage"><asp:Label ID="lblShortMessage" runat="server" OnDataBinding="txtBox1_DataBound" Text='<%# Bind("MessageContent")%>' ToolTip='<%# Bind("ID")%>'></asp:Label></div>
                </td>
                <td class="attachment">
                    <div id="attachPic" class="msgAttach" runat="server" dir="rtl">

                     **** I WANT TO INSERT PAPERCLIP IMAGE WITH LINK TO ATTACHMENT (if exists) HERE ****

                   </div>
                </td>
            </tr>
        </table>
    </td>
</ItemTemplate>

I have dug it out myself:

    Private Sub ListViewMsg_ItemDataBound(sender As Object, e As ListViewItemEventArgs) Handles ListViewMsg.ItemDataBound
    Dim list As SPList = SPContext.Current.Web.Lists(ListId)
    Dim view As SPView = list.Views(ViewId)
    Dim lItems As SPListItemCollection = list.GetItems(view)
    Dim lItem As SPListItem = lItems.GetItemById(CType(e.Item.FindControl("lblShortMessage"), Label).ToolTip) <<----[NOTE] I bounded the ID column from the list to this property.
    If lItem.Attachments.Count > 0 Then
        For indx As Integer = 0 To lItem.Attachments.Count - 1
            Try
                Dim itemURL As String = lItem.Attachments.UrlPrefix & lItem.Attachments(0)
                CType(e.Item.FindControl("attachPic"), HtmlControls.HtmlGenericControl).InnerHtml = "<a href=" & itemURL & "><img width=14 height=24 src=http://Site_Collection_Name/sites/devplayground/PublishingImages/PaperClip.gif /></a>"
            Catch ex As Exception
                MyUtilities.Alert(Me, ex.Message)
            End Try
        Next
    End If
   End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM