Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
    
Are you trying to use this control on in server-side code, JavaScript, XSLT. How are you trying to get the values? –  Chris Beckett 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
add comment

3 Answers 3

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.

share|improve this answer
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 –  Toni Frankola May 27 '11 at 10:46
add comment

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>
 <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')}"/>
   <script>
      var elm = document.getElementById("idAttachmentsTable");
      if (elm == null || elm.rows.length == 0)
      document.getElementById("idAttachmentsRow").style.display='none';
   </script>
 </td>
</tr>

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>
        </TABLE>

What do you want to do with the files?

share|improve this answer
    
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. –  PirateEric Aug 2 '11 at 20:01
add comment

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:

HttpRequest.Files

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. :)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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