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.

Advertisement
WSS Practice: Attach files to ListItems

WSS Practice: Uploading a document

Well, I’ve mentioned uploading via HTTP before, but I must confess, I’d not used the object model. Well, it turns out that it’s just the same as uploading an image (just without thumbnails getting generated…
SPList docs = site.Lists["Documents"];
FileStream fs = new FileStream(@"c:SomeDocument.txt",FileMode.Open,FileAccess.Read);
docs.RootFolder.Files.Add(docs.RootFolder.Url + "/SomeDocument.txt",fs, true);

WSS Practice: Uploading a document

Uploading Files to SharePoint using the Web Services

Thankfully, someone seems to have looked at other ways of uploading files via the web services – this time using a direct PUT command. That is an approach which is a little odd, to be honest, and I suppose not really Web Services so much as plain old web-server. Still, I’m going to have a look at this approach – and it’d be great if it works.

Uploading Files to SharePoint using the Web Services