NewForm with autocomplete attachments

Jun 8, 2010 at 3:47 PM


I'm a newbie to SPServices, and trying to customize the NewForm to autocomplete 3 attachments for each new list item.

Is this possible using SPServices?




Jun 8, 2010 at 3:54 PM


Welcome to SPServices! Can you explain a little more about what you are trying to do?  Are you trying to pre-fill the attachment names or something else?  If so, where would the names come from?


Jun 8, 2010 at 4:19 PM

I need to create a custom list and each new item to be pre-fill with 3 word documents.

I created a document library to hold these documents, but attaching them to a NewForm gave me an "access is denied" error.



Jun 8, 2010 at 4:25 PM

That sounds like it is a permissions issue.  Are the three documents to be used as templates for each list item or something?


Jun 8, 2010 at 4:34 PM

Yes, they will be used as templates. I've seen you have AddAttachment operation, how that works? Can this be used to pre-fill a new form?



Jun 8, 2010 at 4:49 PM

George, just a quick question before I draft my reply... are you on a WSS environment or MOSS?

Jun 8, 2010 at 4:54 PM

We are on MOSS 2007.

Jun 8, 2010 at 5:36 PM
Edited Jun 8, 2010 at 5:39 PM


I don't have a MOSS VM available to test at the moment, but I do have an initial concern out of the gate from a process perspective. With WSS (can't speak to MOSS without testing) attachments on lists open as Read Only. This means that if you're going to attach these templates for your users to complete, the end user would have to download the template, complete it, then upload a new version manually. If MOSS allows you to open attachments in edit mode then this isn't a show stopper, but if it handles attachments like WSS you may want to rethink your approach and just supply the users with a link to the library containing the templates; otherwise you're just creating extra work for the end user. We can easily add a line to the form with a link to the library which may be better for the end user.

For actually attaching them, I think you've got to combine a few functions together. Firstly, you can't attach a file without knowing the ID of the list item you're attaching it to, so you can't call the AddAttachment method until after the record has been completed. You can use the SPRedirectWithID function of this library to redirect to another page (ie: NewItem_attachments.aspx), which will then contain your AddAttachment code. The second challenge is that the AddAttachment operation requires a base64 encoded string of the file contents, not the name of the file (there's always a lot of confusion over this). You need to read the physical contents of the file, encode it to base64 and stream that variable to the web service. SharePoint also has a move/copy web service which makes this quite a bit easier as you can specify origin and destination name, but you can't use that to go from a document library to a list.

Attachments are one of those things that aren't handled really well client side. The web services give you a ton of capability, but you've got to find a way to stream the base64 encoded contents of the file which in practice isn't very easy. For example, if I had a text file with the contents of "this is my test file", I'd have to pass the following to the web service's "attachment" parameter, "dGhpcyBpcyBteSB0ZXN0IGZpbGU=". You can see how complex this gets when we start dealing with large office documents. I've seen several cases where the memory consumption alone prohibits this process, especially when you start talking about doing multiple files at a time.

As far as permissions go, the user creating the list item will need to have read access to the library containing the templates, as the web service and encoding code will run under their credentials. I think the "access denied" error you're seeing is probably just being spat out because the function is confused; not positive it's a permissions issue.

If you've got some code you're working on, feel free to post it.


Jun 8, 2010 at 5:53 PM


This helps; we might need to work around these options.

Thank you,


Jun 8, 2010 at 6:10 PM

Feel free to post if you need some help trying to get it working; like I said there's generally a lot of confusion about how to use the file based web services.