How to add attachment to SharePoint list item programmatically

In this SharePoint tutorial, we will discuss how to add attachment to SharePoint list item programmatically c#. We will use here SharePoint server object model to programmatically add attachments to SharePoint list item.

Add attachment to SharePoint list item programmatically c#

To work with the SharePoint server object model code, make sure you have installed SharePoint on your server where you are executing the code.

If you have SharePoint installed on the same server, you can also create a windows application or console application to run the code.

But here particularly on this example, I have created a visual web part in SharePoint and deployed the web part to the SharePoint site collection.

In the SharePoint visual web part, I have added a C#.Net file upload control into it. Also, it has a submit button. On the button submit, the attachment will be saved to the SharePoint list item.

add attachment to sharepoint list item programmatically
add attachment to sharepoint list item programmatically
     <asp:FileUpload ID="FUAttachQID" runat="server" />

Next, we have to get the File size and File Extension using the below code. So add the below code in your .cs page.

 private System.Collections.Generic.List<DTO.Attachment> GetAttachments()
        {
            List<DTO.Attachment> attachments = new List<DTO.Attachment>();
            var fileUpload = FUAttachQID.FileName.ToString();
            var fileSize = FUAttachQID.FileBytes.ToString();
            var fileContents = FUAttachQID.FileContent.ToString();
            if (FUAttachQID.HasFile)
            {
                string fileExtension = System.IO.Path.GetExtension(FUAttachQID.PostedFile.FileName).ToLower();
                int fileLengthInKB = FUAttachQID.PostedFile.ContentLength / 1024;
                attachments.Add(new DTO.Attachment { file = FUAttachQID.FileBytes, Title = string.Format(fileUpload, fileExtension) });

            }
            return attachments;
        }
    }

Next call the GetAttachments() method to insert the attachment into the SharePoint List.

var attachments = GetAttachments();
var spContext = SPContext.Current;
                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    using (SPSite spSite = new SPSite(spContext.Site.Url))
                    {
                        using (SPWeb spWeb = spSite.AllWebs[subsiteName])
                        {
                            spWeb.AllowUnsafeUpdates = true;
                            SPList spList = spWeb.GetList(string.Format(@"{0}/Lists/{1}/AllItems.aspx", spWeb.Url, ListName));
                            SPListItem item = null;
                           
                      item = spList.AddItem();
                             
                foreach (DTO.Attachment attachment in attachments)
                                {
                item.Attachments.Add(attachment.Title, attachment.file);
                                }
                                item.Update();
                                spWeb.AllowUnsafeUpdates = false;
                            }
                        }
                    });

You can the attachment is added to the SharePoint list item.

add attachment to sharepoint list item programmatically c#

You may like the following SharePoint tutorials:

In this SharePoint tutorial, we will discuss how to add an attachment to sharepoint list item programmatically using C#.Net (SharePoint server object model code).

  • >