(iv) List attachments
The attachment to the list is also part of the file system, which is attached to the list item on the normal list (the document library has no attachments), and its operation is very similar to the operation of the document in some places and in the document library.
1, the attachment read
An attachment to a list entry can be accessed using the SPListItem attachments property, which is the spattachmentcollection type. This type is a special type in SharePoint and is special in that there is no spattachment type. So what is spattachmentcollection in this collection? is the file name of the attachment. By using the SPWeb GetFile method, you can get the SPFile object that corresponds to the attachment, and then you can read and modify the same as the SPFile object. The Read method can refer to the following program:
1:usingnew SPSite ("Http://sp2010/book"))
2: {
3: using (SPWeb Web = site.) OpenWeb ())
4: {
5: SPListItem item = web. list["chapters"]. Items[0];
6: string URLPrefix = Item. Attachments.urlprefix;
7: foreach (string in item. Attachments)
8: {
9: SPFile file = web. GetFile (URLPrefix + attname);
Ten: ":" + file. Length.tostring ());
One : }
: }
: }
As you can see, a key point in this program, in addition to GetFile, is the Spattachmentcollection URLPrefix property. All attachments to the list are saved in the attachments directory at the root of the list, and subfolders are divided by the ID of the list entry, using the URLPrefix property directly to obtain the full path to the folder where all attachments for a list entry are located.
2. Add and Remove Attachments
Attachment additions and deletions are similar to file uploads and deletions. In addition, the use of the collection class-The Spattachmentcollection add method, and the difference between the upload file is that the Add method has no other overloads, can only specify the file name of the attachment and byte[] form the contents of the files, This means that the attachment of the list entry is not suitable for large files, and deletes the attachments using the Delete method and the Recycle method of the Spattachmentcollection collection class.
However, as with the update of the list entry, after modifying the field values, you need to call SPListItem's Update method for the changes to take effect, after using the spattachmentcollection add, delete, and recycle methods, You must update the list entry to make the attachment update effective. To make the attachment easier to operate, Spattachmentcollection provides three similar methods: Addnow, Deletenow, and Recyclenow, which can be applied directly after uploading or deleting an attachment using these three methods. There is no need to update the list entries.
Access files and folders for the SharePoint server-side object Model (Part 4)