download file from sharepoint list CSOM

  • Thread starter Thread starter msdnpublic1234
  • Start date Start date
M

msdnpublic1234

Guest
I have a SSIS package which has a C# downloading files from sharepoint list using CSOM.Everything looks fine until the list item ID is the same always.The fileID changed suddenly and the files were not being recognized.How do i modify my code to adapt to the fileID changes so that we rely on file names instead and retrieve files by name and not ID.

Here is my code:


using Microsoft.SharePoint.Client;
using System;
using System.IO;
using System.Security;
using System.Text.RegularExpressions;


namespace ST_3cec968c693et6789e433
{
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
public void Main()
{
var site = (string)Dts.Variables["$Package::SiteUrl"].Value;
var library = (string)Dts.Variables["$Package::Library"].Value;
var directory = (string)Dts.Variables["$Package::fileDestination"].Value;
var ErrorFile = (string)Dts.Variables["$Package::ErrorLog"].Value;
var filename_1 = (string)Dts.Variables["User::fileName_1"].Value;
var filename_2 = (string)Dts.Variables["User::fileName_2"].Value;
try
{

var documentName_1 = Regex.Replace(filename_1, "[#%&*:<>?/|]", String.Empty);
var documentName_2 = Regex.Replace(filename_2, "[#%&*:<>?/|]", String.Empty);
var context = new ClientContext(site);
var securePassword = new SecureString();
var password = "abcd123";

foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);
context.Credentials = new SharePointOnlineCredentials(@"abcclient@xyz.com", securePassword);

var list = context.Web.Lists.GetByTitle(library);

// Get the list item from the Documents list whose Id is 7,5
//Be watchful of list item ID change---edited on 7/20/2018---ID is 26,23
// Note that this is the ID of the item in the list, not a reference to its position.
var listItem_1 = list.GetItemById(26);
var listItem_2 = list.GetItemById(23);

context.Load(list);
// Load only the title
context.Load(listItem_1, i => i.File);
context.Load(listItem_2, i => i.File);
context.ExecuteQuery();

var fileRef_1 = listItem_1.File.ServerRelativeUrl;
var fileRef_2 = listItem_2.File.ServerRelativeUrl;

//Download the files from Sharepoint
var fileInfo_1 = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, fileRef_1);
var fileInfo_2 = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, fileRef_2);

var fileName_1 = Path.Combine(directory,(string)listItem_1.File.Name);
using (var fileStream = System.IO.File.Create(fileName_1))
{
fileInfo_1.Stream.CopyTo(fileStream);
}

var fileName_2= Path.Combine(directory, (string)listItem_2.File.Name);
using (var fileStream = System.IO.File.Create(fileName_2))
{
fileInfo_2.Stream.CopyTo(fileStream);
}

}
catch (Exception exp)
{
var message = String.Format("{0} - {1}", exp.Message,Environment.NewLine);
System.IO.File.AppendAllText(ErrorFile, "Date :" + DateTime.Now.ToString() + "\t" + "Message:" + message);

}
Dts.TaskResult = (int)ScriptResults.Success;

}

enum ScriptResults { Success, Failure }

}
}

Continue reading...
 
Back
Top