WSS Practice: Attach files to ListItems

Similar to uploading a file, how would we attach a file to an SPListItem? This is slightly different, as a listitem may have more than one attachment….

Well, my code is:

byte[] bytes = File.ReadAllBytes(@"c:somefile.txt");
myListItem.Attachments.AddNow("somefile.txt", bytes);

Yup, not quite the same. I guess it’s ‘cos for Document Libraries, the document is the item, whereas for normal lists, the items may have zero or more documents attached.

Actually, I must try attaching a file to a Document item. I wonder if you can do that?

Edit: Nope, you can’t. Attachments are disabled, and if you try and enable them you get the SPExceptionAttachments are not allowed for Document Libraries and Surveys“. Which is fair enough.

Advertisements
WSS Practice: Attach files to ListItems

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s