This topic has not yet been rated - Rate this topic

AttachmentUpload Class

Represents the form part for an attachment upload.

Namespace:  Microsoft.SharePoint.WebControls
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public sealed class AttachmentUpload : FormComponent
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
AttachmentUpload
Description

The Microsoft.SharePoint.WebControls.AttachmentUpload class inherits from the Microsoft.SharePoint.WebControls.FormComponent class which is the foundational control for form and field rendering controls. The AttachmentUpload control is visible in the shipped software when attachments are enabled on a list, located on the default list toolbar.

The AttachmentUpload control checks whether the SPList.EnableAttachments property is true, determining whether the control should be visible or not. For display the control uses the RenderingTemplate with ID “AttachmentUpload", which will be out an image and the text "Attach File" with appropriate JavaScript references. It should be noted that the browser will also be tested for whether it is IE or FireFox. If the browser is not either, then form will be set with MIME type used to encoding multipart/form-data in order for the page form to allow file uploads while the remaining WebForms are not affected. 

The Usage Scenario

The primary usage of AttachmentUpload is internal used in combination with ListFormWebPart and DataFormWebPart controls. Within custom development, when heavily customizing the ListForm WebForms using AttachmentUpload control is frequently used because of attachment support considerations with SharePoint Designer. 

In the below, I am creating the required proxy objects by hydrating a SPSite and SPWeb by using a literal URL value. Following, a SPList object is hydrated by looking for the list with the name "My List". An AttachmentUpload control is created, and the AttachmentUpload.ListId and AttachmentUpload.ItemId properites are set to reference from the previously created SPList object. Following, the AttachmentUpload control is added to the current instance control collection. 

C# Code Example

protected override void CreateChildControls()
{
using (SPSite site = new SPSite("My Url"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["My List"];

AttachmentUpload upload = new AttachmentUpload();
upload.ListId = list.ID;
upload.ItemId = list.Items[0].ID;
upload.ID = "MyActions";
Controls.Add(upload);
}
}
}

Visual Basic .NET Code Example

Protected Overloads Overrides Sub CreateChildControls()
Using site As New SPSite("My Url")
Using web As SPWeb = site.OpenWeb()
Dim list As SPList = web.Lists("My Lists")

Dim upload As New AttachmentUpload()
upload.ListId = list.ID
upload.ItemId = list.Items(0).ID
upload.ID = "MyActions"
Controls.Add(upload)
End Using
End Using
End Sub

Adam Buenz
SharePoint Foundation MVP - http://www.sharepointsecurity.com