Click here to Skip to main content
15,881,027 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How to download attachments from list items using client object model?

For the Reference , see the Code To download attachments from list items using Server side Object Model.
"
SPWeb web = new SPSite("<site url="">").OpenWeb();
//Open List
SPList list = web.Lists["<listname>"];
//Get the item
SPListItem item = list.Items[1];
//Get the folder
SPFolder folder = web.Folders["Lists"].SubFolders[strListName].SubFolders["Attachments"].SubFolders[item.ID.ToString()];

foreach(SPFile file in folder.Files)
{
byte[] binFile = file.OpenBinary();
System.IO.FileStream fstream = System.IO.File.Create("c:\\MyDownloadFolder\\"; + file.Name);
fstream.Write(binFile, 0, binFile.Length);
}

"
Posted

/* Microsoft SQL Server Integration Services Script Task
Write scripts using Microsoft Visual C# 2008.
The ScriptMain is the entry point class of the script.
*/

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Xml;
using System.Linq;
using System.Web.Services;
using System.Net;
using System.Configuration.Assemblies;
using System.IO;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{

#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion

/*
The execution engine calls this method when the task executes.
To access the object model, use the Dts property. Connections, variables, events,
and logging features are available as members of the Dts property as shown in the following examples.

To reference a variable, call Dts.Variables["MyCaseSensitiveVariableName"].Value;
To post a log entry, call Dts.Log("This is my log text", 999, null);
To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, true);

To use the connections collection use something like the following:
ConnectionManager cm = Dts.Connections.Add("OLEDB");
cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;";

Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.

To open Help, press F1.
*/

public void Main()
{
try
{
String SharePointLink = Dts.Variables["SharePointLink"].Value.ToString();
String ListName = Dts.Variables["ListName"].Value.ToString();
String DirectoryForSharepointFiles = Dts.Variables["DirectoryForSPFiles"].Value.ToString();
String SharePointUsername = Dts.Variables["SharePointUsername"].Value.ToString();
String SharepointPwd = Dts.Variables["SharepointPwd"].Value.ToString();
String DomainName = Dts.Variables["DomainName"].Value.ToString();
String myFileExtension = "";

NetworkCredential credentials = new NetworkCredential(SharePointUsername,SharepointPwd,DomainName);
using (ClientContext clientContext = new ClientContext(SharePointLink))
{
clientContext.Credentials = credentials;

//Get the Site Collection
Site oSite = clientContext.Site;
clientContext.Load(oSite);
clientContext.ExecuteQuery();
//MessageBox.Show(oSite.Url);

// Get the Website
Web oWeb = clientContext.Web;
clientContext.Load(oWeb);
clientContext.ExecuteQuery();
//MessageBox.Show(oWeb.Title);

// CAML Query
CamlQuery query = new CamlQuery();
query.ViewXml = @"<query><orderby><fieldref name="Attachments" ascending="True">";

// Get The List Name
List oList = clientContext.Web.Lists.GetByTitle(ListName);
clientContext.Load(oList);
clientContext.ExecuteQuery();
//MessageBox.Show(oList.Title);

// Get the ListItem Name
ListItemCollection items = oList.GetItems(query);
clientContext.Load(items);
clientContext.ExecuteQuery();

// Dispaly listItem properties
foreach (ListItem listItem in items)
{
Folder folder = oWeb.GetFolderByServerRelativeUrl(oSite.Url + oWeb.ServerRelativeUrl + "/Lists/Tasks/Attachments/" + listItem["ID"]);
clientContext.Load(folder);
clientContext.ExecuteQuery();

FileCollection attachments = folder.Files;
clientContext.Load(attachments);
clientContext.ExecuteQuery();

foreach (Microsoft.SharePoint.Client.File oFile in folder.Files)
{
FileInfo myFileinfo = new FileInfo(oFile.Name);
myFileExtension = myFileinfo.Extension.ToLower();
String sFileextension = (Dts.Variables["FeedFormat"].Value.ToString().Trim()).ToLower();
if (myFileExtension.Equals(sFileextension))
{
WebClient client1 = new WebClient();
client1.Credentials = credentials;
byte[] fileContents = client1.DownloadData(oSite.Url + oFile.ServerRelativeUrl);
FileStream fStream = new FileStream(@DirectoryForSharepointFiles + oFile.Name, FileMode.Create);
fStream.Write(fileContents, 0, fileContents.Length);
fStream.Close();
//delete or move to Document Library yet to implement code here
}
}
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
Dts.TaskResult = (int)ScriptResults.Success;
}
}
}
 
Share this answer
 
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Xml;
using System.Linq;
using System.Web.Services;
using System.Net;
using System.Configuration.Assemblies;
using System.IO;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
public void Main()
{
try
{
String SharePointLink = Dts.Variables["SharePointLink"].Value.ToString();
String ListName = Dts.Variables["ListName"].Value.ToString();
String DirectoryForSharepointFiles = Dts.Variables["DirectoryForSPFiles"].Value.ToString();
String SharePointUsername = Dts.Variables["SharePointUsername"].Value.ToString();
String SharepointPwd = Dts.Variables["SharepointPwd"].Value.ToString();
String DomainName = Dts.Variables["DomainName"].Value.ToString();
String myFileExtension = "";
NetworkCredential credentials = new NetworkCredential(SharePointUsername,SharepointPwd,DomainName);
using (ClientContext clientContext = new ClientContext(SharePointLink))
{
clientContext.Credentials = credentials;
//Get the Site Collection
Site oSite = clientContext.Site;
clientContext.Load(oSite);
clientContext.ExecuteQuery();
// Get the Website
Web oWeb = clientContext.Web;
clientContext.Load(oWeb);
clientContext.ExecuteQuery();
// CAML Query
CamlQuery query = new CamlQuery();
query.ViewXml = @"<query><orderby><fieldref name="Attachments" ascending="True">";
// Get The List Name
List oList = clientContext.Web.Lists.GetByTitle(ListName);
clientContext.Load(oList);
clientContext.ExecuteQuery();
// Get the ListItem Name
ListItemCollection items = oList.GetItems(query);
clientContext.Load(items);
clientContext.ExecuteQuery();
// Dispaly listItem properties
foreach (ListItem listItem in items)
{
Folder folder = oWeb.GetFolderByServerRelativeUrl(oSite.Url + oWeb.ServerRelativeUrl + "/Lists/Tasks/Attachments/" + listItem["ID"]);
clientContext.Load(folder);
clientContext.ExecuteQuery();
FileCollection attachments = folder.Files;
clientContext.Load(attachments);
clientContext.ExecuteQuery();
foreach (Microsoft.SharePoint.Client.File oFile in folder.Files)
{
FileInfo myFileinfo = new FileInfo(oFile.Name);
myFileExtension = myFileinfo.Extension.ToLower();
String sFileextension = (Dts.Variables["FeedFormat"].Value.ToString().Trim()).ToLower();
if (myFileExtension.Equals(sFileextension))
{
WebClient client1 = new WebClient();
client1.Credentials = credentials;
byte[] fileContents = client1.DownloadData(oSite.Url + oFile.ServerRelativeUrl);
FileStream fStream = new FileStream(@DirectoryForSharepointFiles + oFile.Name, FileMode.Create);
fStream.Write(fileContents, 0, fileContents.Length);
fStream.Close();
}
}
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
Dts.TaskResult = (int)ScriptResults.Success;
}
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900