In WSS 3.0, I've got an edit form which includes an AttachmentsField control. It corresponds to the list of existing attachments, with delete links. In a normal edit form, if you delete an attachment and hit Save, it'll then remove the attachment.

I want to replicate this functionality for something similar, but I'm unsure how to extract the current list of attachments stored in the control. I need to do this in order to reflect any changes based on that.

The Value property of the AttachmentsField is apparently null, whether or not there are any attachments and whether or not I've made any changes on the control. If I check AttachmentsField.Controls and recursively loop through it, there's a Literal in it with an ID of AttachmentsList, but I'm not sure how to extract the data from it (it errors if I try to print its Text property). I can't find any other avenues of entry to try and access the data.

How do I extract the list of attachments currently rendered in the AttachmentsField control?

  • Are you trying to use this control on in server-side code, JavaScript, XSLT. How are you trying to get the values? May 26 '11 at 18:11
  • @Chris Currently? C# server-side. I'm open to anything that'll prevent me from having to create my own pseudo-AttachmentsField control as a workaround for this edit form.
    – Grace Note
    May 26 '11 at 18:12

I had a glance in the AttachmentsField.UpdateFieldValueInItem call to see where it gets the list of files from for its own use.

It appears to use this property: AttachmentsField.Context.Request.Files, the documentation for which is here:


Edit: I suddenly realised that you may be trying to access the control in order to save the attachments. If that's the case then AttachmentsField.UpdateFieldValueInItem is in itself the answer. :)


Looking at a custom EditForm.aspx page in Sharepoint Designer, the form code looks like this:

<tr id="idAttachmentsRow">
 <td nowrap="true" valign="top" class="ms-formlabel" width="20%">
  <SharePoint:FieldLabel ControlMode="Edit" FieldName="Attachments" runat="server"/>
 <td valign="top" class="ms-formbody" width="80%">
  <SharePoint:FormField runat="server" id="AttachmentsField" ControlMode="Edit" FieldName="Attachments" __designer:bind="{ddwrt:DataBind('u','AttachmentsField','Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Attachments')}"/>
      var elm = document.getElementById("idAttachmentsTable");
      if (elm == null || elm.rows.length == 0)

Wouldn't you want to mimmic that code?

Edit: Attaching a file to a list item results in default EditForm code like this:

<TABLE border=0 cellpadding=0 cellspacing=0 id=idAttachmentsTable>
            <TR id={7D99341E-8DFE-4EC1-83E9-46B1AAECFA71}><TD class="ms-vb"><span dir="ltr"><a tabindex=1 onclick="DispDocItemEx(this, 'FALSE', 'FALSE', 'FALSE', 'SharePoint.OpenDocuments.3')" href="https://my.domain.com/sites/spsite/Lists/Calendar/Attachments/3/features.xls">features.xls</a></span>&nbsp;&nbsp;&nbsp;&nbsp;</TD><TD class="ms-propertysheet"><IMG alt='Delete' SRC="/_layouts/images/rect.gif">&nbsp;<a tabindex=1 href="javascript:RemoveAttachmentFromServer('{7D99341E-8DFE-4EC1-83E9-46B1AAECFA71}',1)">Delete</a></TD></TR>

What do you want to do with the files?

  • I mimicked that in the past, but that just gets you the control. It doesn't really explain how to get the attachments from the control.
    – Grace Note
    Aug 2 '11 at 19:29
  • Since it returns a table with an id of idAttachmentsTable, you could use some client side script to do something with it. Aug 2 '11 at 20:01

If I understood your question correctly you want to retrieve the list of attachments of current list item in server-side code. In such a case there is Attachments property of SPListItem.

  • 1
    No, I would like to retrieve the list of attachments of the current list item as rendered in the AttachmentsField control. Attachments on the SPListItem doesn't update until the form is submitted, and I can't submit that data to update the attachments if I can't properly read the AttachmentsField control.
    – Grace Note
    May 27 '11 at 10:26
  • Then I have no idea, but you will probably need to do it on client-side e.g. via JQuery May 27 '11 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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