Sunday, 27 September 2015

Sharepoint List items Add attachment & Delete attachments


Adding Items with Attachment

string _Str_File_Name = FileUpload1.PostedFile.FileName;
            byte[] contents = GetFileStream();
            itemId = 0;
            using (SPSite site = new SPSite(SPContext.Current.Web.Url.ToString()))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["Check"];
                    web.AllowUnsafeUpdates = true;
                    SPListItem item = list.Items.Add();
                    item["Title"] = "Title1";
                    item["Citys1"] = "CCC1";
                    if ((_Str_File_Name != null) && (contents.Length > 0))
                    {
                        SPAttachmentCollection fileAttach = item.Attachments;
                        fileAttach.Add(_Str_File_Name, contents);
                    }
                    item.Update();
                    Page.Session["ItemId"] = item.ID;
                    Page.Session["ItemFileName"] = _Str_File_Name;
                    itemId = item.ID;
                    strFileName = _Str_File_Name;
                    Label1.Text = _Str_File_Name + "  " + itemId.ToString();
                }
            }
        }
        private byte[] GetFileStream()
        {
            //fuAttachment is your file select control
            if (!FileUpload1.HasFile)
                return null;
            Stream fStream = FileUpload1.PostedFile.InputStream;
            byte[] contents = new byte[fStream.Length];
            fStream.Read(contents, 0, (int)fStream.Length);
            fStream.Close();
            return contents;
        }

Deleting the Attachment / Attachments Only

 public static void DeleteAllAttachements(int iid)
        {
            using (SPSite site = new SPSite(SPContext.Current.Web.Url.ToString()))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPListItem listItem = web.Lists["Check"].GetItemById(iid);
                    List<string> fileNames = new List<string>();
                    foreach (string fileName in listItem.Attachments)
                    {
                        fileNames.Add(fileName);
                    }
                    foreach (string fileName in fileNames)
                    {
                        listItem.Attachments.Delete(fileName);
                    }
                    listItem.Update();
                }
            }
        }

//for Update the attachment
            string _Str_File_Name = FileUpload1.PostedFile.FileName;
            byte[] contents = GetFileStream();
            try
            {
                using (SPSite site = new SPSite(SPContext.Current.Web.Url.ToString()))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists["Check"];
                        web.AllowUnsafeUpdates = true;
                        // Delete List item
                        // Update the List item by ID
                        SPListItem itemToUpdate = list.GetItemById(31);
                        if ((_Str_File_Name != null) && (contents.Length > 0))
                        {
                            SPAttachmentCollection fileAttach = itemToUpdate.Attachments;
                            fileAttach.Add(_Str_File_Name, contents);
                        }
                        //itemToUpdate["Description"] = "Changed Description";
                        itemToUpdate.Update();
                        Label2.Text = "Udatead the achment pls check";
                    }
                }
            }
            catch (Exception ex)
            {
                Label2.Text = ex.Message;
            } 

No comments:

Post a Comment