Sharepoint list attachments
Using out of the box sharepoint functionality, it is easy to add and view attachments to list items.
However, a common issue in WSS 3.0 is that customised list views do not support attachments ... or, "attachement". The error "This form was customized not working with attachement" is displayed.
The only supported way of storing attachments to a list item, is to use the default newform.aspx page, associated with the given list.
This works, but has a number of restrictions:
Branding
It is difficult to brand or customise the upload form. Even though an upload.aspx file is associated with the list, it is not used (by default). The actual upload form which is used, is located in the 'Layouts' directory, on the webserver; any customisation of this would change the uploading form for all sites in the site collection. This is not desirable.
Views
The default newform.aspx used a listview to display the fields associated with the list. Unfortunately, there is no easy way to change the fields that are displayed to the user; all fields associated with the list are shown by default.
Customisation
With respect to general customisation of the newform.aspx, it is also possible to view the data using dataview webparts, and custom list forms. Unfortunately, both of these approaches appear to strip support for attachments; dataview webparts do not support an upload control, and custom list forms throw the error in the screenshot above.
Solution
In order to overcome the above issues, it is fairly simple to implement a custom webpart using the Visual Studio extensions for WSS.
The general process is as follows:
Setup the project
Firstly, create a new webpart project, using the VSeWSS. Then, change the class declaration to inherit from
Microsoft.SharePoint.WebPartPages.WebPart
The existing class definition inherits from the standard ASP.NET 2.0 WebPart class – this ensures interoperability with existing .NET webparts, but does not provide access to all of the sharepoint WebPart features.
Create the code structure
The default WebPart project contains a single overridable method "Render". The render method outputs straight html, via the use of a HtmlTextWriter. This method is not useful for our purposes: delete it. Create two new methods: CreateChildControls(), and RenderWebPart() – both should be defined as "protected override void".
The CreateChildControls() method instantiates the ASP.net controls that you want to use – in this case, a HtmlInputFile (for the attachment). Make sure you declare the controls globally. The RenderWebPart() method adds the controls to the Html, using the format
myControl.RenderControl(output)
Add your submission eventhandler
In your CreateChildControls() method, add a button with an event handler – this will be used to submit the entire form. Make sure you also render this button in the RenderWebPart() method.
In the eventhandler, create a SPList object pointing to the list you want to add an item to, and add an SPListItem to the list, using:
SPListItem myItem = myList.items.add()
Now, add any properties to the listitem using:
myItem["Title"] = "{title here}";
And, add the attachment, using:
Stream fstream = myHtmlInputFile.PostedFile.InputStream;
byte[] contents = new byte[fstream.length];
fstream.Read(contents, 0, (int)fstream.length);
fstream.Close();
fstream.Dispose();
myItem.Attachments.Add(Path.GetFileName(myHtmlInputFile.PostedFile.FileName), contents);
Finally, make sure you commit these changes to the listitem, using
myItem.Update();
Summary
Hopefully, a future sharepoint release will solve the current implementation issues with list attachments. In the meantime, you may need to either use the default sharepoint behaviour (without customisation), or create a custom webpart. If you choose to follow the latter route, the preceding instructions should provide you with all of the tools you need to get started.