Check if File already Exists in SharePoint List

Check if File already Exists in SharePoint List:-

How to upload a document in SharePoint List. And Check if File already Exists in SharePoint List.We can find If the attachment already exists in SharePoint list.In this post, I will also describe you, How to delete the file if already exist. Get Attachments from SharePoint List

The code is here:-

[AdSense-A]

public bool FileUploadToList()
        {
            bool FileExist = false; bool Success = false;
            if (FileUploadControl.HasFile)
            {

                try
                {
                    using (SPSite SPsite = new SPSite("SiteURL"))
                    {

                        using (SPWeb SPWeb = SPsite.OpenWeb())
                        {                                                      
                            SPList List = SPWeb.Lists["ListName"];
                            SPQuery query = new SPQuery();
                            string qry = "<Where> <Eq><FieldRef Name='ColumnInternalName' /><Value Type='Text'>" + ColumnValue + "</Value> </Eq></Where>";
                            query.Query = qry;
                            SPListItemCollection itemcoll = List.GetItems(query);
                            SPListItem item = itemcoll[0];
                            string[] _Str_Attachments = GetAttachmentCollection(item);
                            if (_Str_Attachments != null)
                            {
                                int len = _Str_Attachments.Length;
                                SPFolder sourceItemAttachmentsFolder = item.Web.Folders["Lists"].SubFolders[item.ParentList.Title].SubFolders["Attachments"].SubFolders[item.ID.ToString()];

                                foreach (SPFile file in sourceItemAttachmentsFolder.Files)
                                {
                                    string FileName = file.Name.ToString();
                                    if (FileUploadControl.HasFile)
                                    {
                                        if (FileName == FileUploadControl.FileName)
                                        {
                                            FileExist = true;
                                            break;
                                        }

                                    }
                                }
                            }
                            if (FileExist == true)
                            {
                                ScriptManager.RegisterStartupScript(this.Page, GetType(), "srcitp", "alert('File Already Exist'); ", true);
                            }
                            else
                            {
                                item.Attachments.Add(FileUploadControl.FileName, FileUploadControl.FileBytes);
                                item.Update();
                                Success = true;
                            }


                        }
                    }
                }
                catch (Exception)
                {

                    throw;
                }
            }
            else
            {
                Success = true;
            }
            return Success;
        }

 


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!