Saturday, 23 March 2013

Attachments in SharePoint (Part I)

A list item in SharePoint can have multiple attachments.  Below is a code snippet in c#.Net to add an attachment to a list item programmatically.
This example demonstrates how a file, uploaded through a file upload control is added as an attachment to a list item.
using (SPSite oSPsite = new SPSite(http: //website Url/))
{
     using (SPWeb oSPWeb = oSPsite.OpenWeb())
      {
           oSPWeb.AllowUnsafeUpdates = true;
            // Fetch the List
            SPList list = oSPWeb.Lists["MyList"];
            // Get the List item
            SPListItem listItem = list.GetItemById(1);
           
            // Get the Attachment collection
            SPAttachmentCollection attachmentCollection = listItem.Attachments;
            Stream attachmentStream;
            Byte[] attachmentContent;
             // Get the file from the file upload control
            if (this.fileDocAttach.HasFile)
            {
                     attachmentStream = this.fileDocAttach.PostedFile.InputStream;
                 
                     attachmentContent = new Byte[attachmentStream.Length];
                     attachmentStream.Read(attachmentContent, 0, (int)attachmentStream.Length);
                    attachmentStream.Close();
                    attachmentStream.Dispose();
                     // Add the file to the attachment collection
                     attachmentCollection.Add(this.fileDocAttach.FileName, attachmentContent);
            }
            // Update th list item
            listItem.Update();
            oSPWeb.AllowUnsafeUpdates = false;
       }
 }

 

Adding an Attachment to a List Item Programmatically in SharePoint

As we can have multiple attachments against a list item, it has always been a requirement to fetch these attachments and get it displayed within a custom webpage.
 
Below is code snippet in c#.net for fetching all the attachements for a specific list item.
try
            {
                // Set the Site Url
                SPSite objSite = new SPSite("http://Testsite/");
 
                using (SPWeb objWeb = objSite.OpenWeb())
                {
                    objWeb.AllowUnsafeUpdates = true;
 
                    // Get the List
                    SPList objList = objWeb.Lists["MyList"];
 
                    // Get the item by ID
                    SPListItem objItem = objList.GetItemById(1);
 
                    // Get the attachments of the item
                    SPAttachmentCollection objAttchments = objItem.Attachments;
 
                    // Iterate the attachments
                    foreach (string fileName in objItem.Attachments)
                    {
                        // Perform action on the extracted attachment
                    }
 
                   objWeb.AllowUnsafeUpdates = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

No comments:

Post a Comment