Attach multiple files to a SPListItem

As SharePoint developer you sometimes get to play with advanced BCS and variations but sometimes you just simply have to create a form where customers can enter information and upload multiple files.

One thing I had to keep in mind while creating this form, was the security. Even with elevated privileges you don’t have the right to read files from the users file system (Access denied Exception).
So in the form I used a ASP FileUpload and saved the file to a map on my server.

image

As you can see, I use a ASP GridView to visualize the uploads. In combination with a DataTable I can save the file locations in the ViewState and collect them at my next post back.
Don’t forget to also give your user the opportunity to remove his uploads.

image

After validating my form and creating a new item in the custom list, I add the uploaded attachments to the item as follows:

image

One import line in the code above is attachments = newApplicationItem.Attachments;. Always do this even if you know it is a new item and doesn’t have any attachments yet.
Once the item is saved, don’t forget the clean up the server!

image

Reacties

Populaire posts