I am trying to download the files in our sharepoint document library but can’t find any items or files.
I’m not quite certain where the error is as itemcount
is showing the correct number of items.
<code>static void Main(string[] args)
{
string siteUrl = ConfigurationManager.AppSettings["sp_url"]; // Your SharePoint site URL
string serverUrl = siteUrl;
string name = ConfigurationManager.AppSettings["user1"];
string pword = ConfigurationManager.AppSettings["pword1"];
string dldirectory = ConfigurationManager.AppSettings["downloadpath"];
string libraryname = ConfigurationManager.AppSettings["libraryname"];
SecureString securepassword = new SecureString();
foreach (char c in pword.ToCharArray())
{
securepassword.AppendChar(c);
using (ClientContext context = new ClientContext(siteUrl))
{
context.Credentials = new SharePointOnlineCredentials(name, securepassword);
CamlQuery query = new CamlQuery();
query.ViewXml = "<View Scope='RecursiveAll'><Query></Query></View>";
var items = context.Web.Lists.GetByTitle(libraryname).GetItems(query);
context.Load(items);
context.ExecuteQuery();
string localFolderPath = @"C:temp"; // Specify your local folder path
foreach (var item in items)
{
Microsoft.SharePoint.Client.File f = item.File;
context.Load(f);
context.ExecuteQuery();
string localFilePath = Path.Combine(localFolderPath, f.Name);
using (var localFileStream = new FileStream(localFilePath, FileMode.Create))
{
var fileInformation = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, f.ServerRelativeUrl);
fileInformation.Stream.CopyTo(localFileStream);
}
Console.WriteLine($"{f.Name} has been downloaded successfully.");
}
}
}
}
</code>
<code>static void Main(string[] args)
{
string siteUrl = ConfigurationManager.AppSettings["sp_url"]; // Your SharePoint site URL
string serverUrl = siteUrl;
string name = ConfigurationManager.AppSettings["user1"];
string pword = ConfigurationManager.AppSettings["pword1"];
string dldirectory = ConfigurationManager.AppSettings["downloadpath"];
string libraryname = ConfigurationManager.AppSettings["libraryname"];
SecureString securepassword = new SecureString();
foreach (char c in pword.ToCharArray())
{
securepassword.AppendChar(c);
using (ClientContext context = new ClientContext(siteUrl))
{
context.Credentials = new SharePointOnlineCredentials(name, securepassword);
CamlQuery query = new CamlQuery();
query.ViewXml = "<View Scope='RecursiveAll'><Query></Query></View>";
var items = context.Web.Lists.GetByTitle(libraryname).GetItems(query);
context.Load(items);
context.ExecuteQuery();
string localFolderPath = @"C:temp"; // Specify your local folder path
foreach (var item in items)
{
Microsoft.SharePoint.Client.File f = item.File;
context.Load(f);
context.ExecuteQuery();
string localFilePath = Path.Combine(localFolderPath, f.Name);
using (var localFileStream = new FileStream(localFilePath, FileMode.Create))
{
var fileInformation = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, f.ServerRelativeUrl);
fileInformation.Stream.CopyTo(localFileStream);
}
Console.WriteLine($"{f.Name} has been downloaded successfully.");
}
}
}
}
</code>
static void Main(string[] args)
{
string siteUrl = ConfigurationManager.AppSettings["sp_url"]; // Your SharePoint site URL
string serverUrl = siteUrl;
string name = ConfigurationManager.AppSettings["user1"];
string pword = ConfigurationManager.AppSettings["pword1"];
string dldirectory = ConfigurationManager.AppSettings["downloadpath"];
string libraryname = ConfigurationManager.AppSettings["libraryname"];
SecureString securepassword = new SecureString();
foreach (char c in pword.ToCharArray())
{
securepassword.AppendChar(c);
using (ClientContext context = new ClientContext(siteUrl))
{
context.Credentials = new SharePointOnlineCredentials(name, securepassword);
CamlQuery query = new CamlQuery();
query.ViewXml = "<View Scope='RecursiveAll'><Query></Query></View>";
var items = context.Web.Lists.GetByTitle(libraryname).GetItems(query);
context.Load(items);
context.ExecuteQuery();
string localFolderPath = @"C:temp"; // Specify your local folder path
foreach (var item in items)
{
Microsoft.SharePoint.Client.File f = item.File;
context.Load(f);
context.ExecuteQuery();
string localFilePath = Path.Combine(localFolderPath, f.Name);
using (var localFileStream = new FileStream(localFilePath, FileMode.Create))
{
var fileInformation = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, f.ServerRelativeUrl);
fileInformation.Stream.CopyTo(localFileStream);
}
Console.WriteLine($"{f.Name} has been downloaded successfully.");
}
}
}
}