Search This Blog

Tuesday, March 17, 2015

Working with SPList Attachements


Here, I am going to share my experience with SPList Attachments.


ADDING AN ATTACHMENT TO AN ITEM IN SPLIST :
 private void AddNewAttachment(int ItemID)
        {

            try
            {

                SPList myList = SPContext.Current.Web.Lists["Item List"];

                SPListItem myNewItem = myList.GetItemById(ItemID);

                if (fileUpload.PostedFile != null && fileUpload.HasFile)
                {

                    Stream fStream = fileUpload.PostedFile.InputStream;

                    byte[] contents = new byte[fStream.Length];

                    fStream.Read(contents, 0, (int)fStream.Length);

                    fStream.Close();

                    fStream.Dispose();

                    SPAttachmentCollection attachments = myNewItem.Attachments;

                    string fileName = Path.GetFileName(fileUpload.PostedFile.FileName);

                    attachments.Add(fileName, contents);

                    myNewItem.Update();

                }

            }

            catch (Exception eAdd)
            {

                string errAdd = eAdd.Message;

            }

        }

DELETING ATTACHMENTS FROM SPLIST :
private void DeleteAttachment(int ItemID)
        {

            try
            {

                SPList myList = SPContext.Current.Web.Lists["Item List"];

                SPListItem delItem = myList.GetItemById(ItemID);

                SPAttachmentCollection attachments = delItem.Attachments;

                if (attachments != null && attachments.Count > 0)
                {
                    foreach (string file in attachments)
                    {
                        if (file != String.Empty)
                        {
                            attachments.Delete(file);
                        }
                    }
                }
                delItem.Update();
            }

            catch (Exception eDel)
            {

                string errDel = eDel.Message;

            }
        }

DOWNLOADING THE ATTACHMENT :
Find the download link first, when click on the link it will download the file.

private void DownloadAttachment(int ItemID)
        {
            try
            {
                string attachmentUrl = "", sectionAttachments = "";

                SPList myList = SPContext.Current.Web.Lists["Item List"];

                SPListItem attItem = myList.GetItemById(ItemID);

                 SPAttachmentCollection attachments = attItem.Attachments;
                if (attachments != null && attachments.Count > 0)
                {
                    sectionAttachments = "<table width='100%' cellpadding='5'><tr><td class='jobAidSectionName'>Attachment(s):</td></tr>";
                    foreach (string file in attachments)
                    {
                        if (file != String.Empty)
                        {
                            attachmentUrl = "/Lists/" + myList + "/Attachments/" + ItemID.ToString() + "/" + file.ToString();
                            sectionAttachments = sectionAttachments + "<tr><td><a class='documentInlineLink' style='padding-left:0px;' href='" + attachmentUrl + "'>- " + file + "</a></td></tr>";
                        }
                    }
                    sectionAttachments += "</table>";
                }
            }
            catch (Exception eDwn)
            {
                string errDwn = eDwn.Message;
            }
        }

No comments:

Post a Comment