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.
<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.
You may like the following SharePoint tutorials:
- How to get logged in user count in SharePoint
- Create a timer job in SharePoint 2016/2013 Programmatically
- Different ways to make column or Field Readonly in SharePoint 2013/2016/Online list
- SharePoint create folder programmatically using CSOM
- How to get access token in SharePoint Online using CSOM and use in Postman or Google Rest client
- How to activate SharePoint Publishing Feature Programmatically using CSOM and PowerShell
- Get all lists and document libraries programmatically using CSOM in SharePoint Online
- Get SharePoint List Name and GUID using PowerShell and CSOM
- Cannot contact web site or the web site does not support sharepoint online credentials
- How to delete all items from SharePoint list
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).
Rajkiran is currently working as a SharePoint Consultant in India . Rajkiran having 7+ years of experience in Microsoft Technologies such as SharePoint 2019/2016/2013/2010, MOSS 2007,WSS 3.0, Migration, Asp.Net, C#.Net, Sql Server, Ajax, jQuery etc.He is C#Corner MVP (2 Times).
What is the DTO in System.Collections.Generic.List???