Download Attachments From List Items In SharePoint 2013 Online Console Application
Download Attachments From List Items In SharePoint 2013 Online Console Application
Introduction
In this post, I will show how to connect to SharePoint 2013 Online, get the list item attachment(s) from the SharePoint 2013 Online custom list and download it to the local folder of our drive.
Prerequisites
The prerequisites required are:
- Knowledge of SharePoint Client Object Model
- Knowledge of C# and Console Application
- Reference of files i.e. "Microsoft.SharePoint.Client.dll" and "Microsoft.SharePoint.Client.Runtime.dll" in Console Application
Let's Start...
Step 1. Create a new custom list in SharePoint 2013 Online as "TestAttachmentList"
Step 2. Add an item to a list and attach few files with it
Step 3. Just to check, view the item you have added and see if it has files attached.
For example, I have created an item with "Title" as "Test 1" and have added 3 text files as shown below:
For example, I have created an item with "Title" as "Test 1" and have added 3 text files as shown below:
Step 4. Now, create a console application in Visual Studio and add two reference files to your solution i.e. "Microsoft.SharePoint.Client.dll" and "Microsoft.SharePoint.Client.Runtime.dll"
Step 5. In your Console Application's main function, write below code with appropriate changes i.e. SharePoint 2013 Online site URL, SharePoint 2013 Online credentials, file path to create folder and save attachments:
using (ClientContext clientContext = newClientContext("https://mysite.sharepoint.com/sites/Site"))
{
{
SecureString passWord = new SecureString();
foreach (char c in "mypassword".ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = newSharePointOnlineCredentials("sunny.bahree@mysite.com", passWord);
Web web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();
CamlQuery query = new CamlQuery();
query.ViewXml = @"";
List oList = clientContext.Web.Lists.GetByTitle("TestAttachmentList");
clientContext.Load(oList);
clientContext.ExecuteQuery();
ListItemCollection items = oList.GetItems(query);
clientContext.Load(items);
clientContext.ExecuteQuery();
foreach (ListItem listItem in items)
{
Folder folder = web.GetFolderByServerRelativeUrl(clientContext.Url +"/Lists/TestAttachmentList/Attachments/" + listItem["ID"]);
clientContext.Load(folder);
try
{
clientContext.ExecuteQuery();
}
catch (ServerException ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("No Attachment for ID " + listItem["ID"].ToString());
}
FileCollection attachments = folder.Files;
clientContext.Load(attachments);
clientContext.ExecuteQuery();
DirectoryInfo dir = Directory.CreateDirectory(@"C:/Downloads/" +listItem["ID"].ToString());
foreach (Microsoft.SharePoint.Client.File oFile in folder.Files)
{
Console.WriteLine("Found Attachment for ID " + listItem["ID"].ToString());
FileInfo myFileinfo = new FileInfo(oFile.Name);
using (WebClient client1 = new WebClient())
{
client1.Headers["Accept"] = "/";
client1.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
client1.Credentials = clientContext.Credentials;
client1.DownloadFile("https://mysite.sharepoint.com" +oFile.ServerRelativeUrl, @"C:/Downloads/" + listItem["ID"].ToString() + "/" + oFile.Name);
Console.WriteLine("Downloading " + oFile.ServerRelativeUrl);
}
}
}
Console.WriteLine("All files have downloaded!");
Console.ReadLine();
}
In above code, we can see that by passing our SharePoint 2013 Online credentials, we can connect to the SharePoint 2013 Online site and from there we can access the custom list along with custom list's all items with their attachments.
To separate the attachments attached per item, we are creating separate folder per item in a our drive with the item ID as a folder name and in that folder we are storing the respective attachments for an item.
Once we run the console application, it will create folder and add files to the folder similar to this:
.
As we can see in above screenshot, after creating a folder (i.e. "1"), we get all the files we have attached to the list item in it.
If I will open each of the files, it will show me the content I have added into them as shown below:
If I will open each of the files, it will show me the content I have added into them as shown below:
Complete Code
Here is the complete code of the Console Application:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using Microsoft.SharePoint.Client;
using System.IO;
using System.Net;
namespace ConsoleAPPDownloadSPAttachments
{
class Program
{
static void Main(string[] args)
{
using (ClientContext clientContext = newClientContext("https://mysite.sharepoint.com/sites/Site"))
{
SecureString passWord = new SecureString();
foreach (char c in "mypassword".ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = newSharePointOnlineCredentials("sunny.bahree@mysite.com", passWord);
Web web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();
CamlQuery query = new CamlQuery();
query.ViewXml = @"";
List oList = clientContext.Web.Lists.GetByTitle("TestAttachmentList");
clientContext.Load(oList);
clientContext.ExecuteQuery();
ListItemCollection items = oList.GetItems(query);
clientContext.Load(items);
clientContext.ExecuteQuery();
foreach (ListItem listItem in items)
{
Folder folder = web.GetFolderByServerRelativeUrl(clientContext.Url +"/Lists/TestAttachmentList/Attachments/" + listItem["ID"]);
clientContext.Load(folder);
try
{
clientContext.ExecuteQuery();
}
catch (ServerException ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("No Attachment for ID " + listItem["ID"].ToString());
}
FileCollection attachments = folder.Files;
clientContext.Load(attachments);
clientContext.ExecuteQuery();
DirectoryInfo dir = Directory.CreateDirectory(@"C:/Downloads/" +listItem["ID"].ToString());
DirectoryInfo dir = Directory.CreateDirectory(@"C:/Downloads/" +listItem["ID"].ToString());
foreach (Microsoft.SharePoint.Client.File oFile in folder.Files)
{
Console.WriteLine("Found Attachment for ID " + listItem["ID"].ToString());
FileInfo myFileinfo = new FileInfo(oFile.Name);
using (WebClient client1 = new WebClient())
{
client1.Headers["Accept"] = "/";
client1.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
client1.Credentials =clientContext.Credentials;
client1.DownloadFile("https://mysite.sharepoint.com" + oFile.ServerRelativeUrl, @"C:/Downloads/" +listItem["ID"].ToString() + "/" + oFile.Name);
client1.DownloadFile("https://mysite.sharepoint.com" + oFile.ServerRelativeUrl, @"C:/Downloads/" +listItem["ID"].ToString() + "/" + oFile.Name);
Console.WriteLine("Downloading " + oFile.ServerRelativeUrl);
}
}
}
Console.WriteLine("All files have downloaded!");
Console.ReadLine();
}
}
}
}
Conclusion
In this post, we learnt how to connect to SharePoint 2013 Online site using Console Application and then get the items from SharePoint 2013 Online custom list along with attachments and download the attachments to our local drive folder.
Comments
Post a Comment