Sharepoint folder creation Using C#
////////apconfig.xml////////////
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="spurl" value="https://****.crm8.dynamics.com"/>
<add key="username" value="******@***.onmicrosoft.com"/>
<add key="password" value="********"/>
<add key="entity" value="Case"/>
<add key="Path" value="C:\Sharepoint"/>
</appSettings>
</configuration>
////////////helper class////////////////////////////
using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SharePointManager
{
public class SPServiceHelper
{
public static string SPUrl;
private static bool _duplicatefound;
private static List<string> _fileNames;
public static List<string> FileNames
{
set { _fileNames = value; }
get { return _fileNames; }
}
public static bool Duplicatefound
{
set { _duplicatefound = value; }
get { return _duplicatefound; }
}
/// <summary>
/// To connect to SharePoint
/// </summary>
public static bool GetService(string spURL, string spUserName, string spPwd)
{
_duplicatefound = false;
_fileNames = new List<string>();
using (ClientContext context = new ClientContext(spURL))
{
bool isConnected = false;
try
{
var ss = new System.Security.SecureString();
Array.ForEach(spPwd.ToCharArray(), (c) => { ss.AppendChar(c); });
context.Credentials = new SharePointOnlineCredentials(spUserName, ss);
var web = context.Web;
context.Load(web);
context.ExecuteQuery();
if (context.Web != null)
{
return isConnected = true;
}
}
catch (Exception ex)
{
return false;
}
return false;
}
}
}
}
/////////////Main Class//////////////////////////////////////////////////////////////////////////////////////////
using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SharePointManager
{
class Program
{
static string servUrl = "";
static string uname = "";
static string password = "";
static string path = "";
static void Main(string[] args)
{
servUrl = ConfigurationManager.AppSettings["spurl"];
uname = ConfigurationManager.AppSettings["username"];
password = ConfigurationManager.AppSettings["password"];
path = ConfigurationManager.AppSettings["Path"];
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
if (string.IsNullOrEmpty(servUrl) || string.IsNullOrEmpty(uname) || string.IsNullOrEmpty(password))
return;
bool isService = SPServiceHelper.GetService(servUrl, uname, password);
if (isService)
{
Console.WriteLine("Sharepoint service created");
RetrieveData();
}
}
public static void RetrieveData()
{
ClientContext context = new ClientContext(servUrl);
var ss = new System.Security.SecureString();
Array.ForEach(password.ToCharArray(), (c) => { ss.AppendChar(c); });
context.Credentials = new SharePointOnlineCredentials(uname, ss);
Web web = context.Web;
ListCollection announcementsList = context.Web.Lists;
context.Load(announcementsList);
context.ExecuteQuery();
foreach (List listItem in announcementsList)
{
if (listItem.Title.ToLower() == ConfigurationManager.AppSettings["entity"].ToLower())
{
Console.WriteLine("Processing...");
int fileCount = 0;
ListCollection subfolders = context.Web.Lists;
CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
ListItemCollection item = listItem.GetItems(query);
context.Load(item);
context.ExecuteQuery();
foreach (ListItem fileitem in item)
{
fileCount++;
context.Load(fileitem);
context.ExecuteQuery();
string[] fileDirectory = fileitem.FieldValues["FileRef"].ToString().Split('/');
string temppath = path;
foreach (string folderName in fileDirectory)
{
if (!string.IsNullOrEmpty(folderName))
{
if (folderName.Contains('.'))
continue;
temppath = temppath + "\\" + folderName;
if (!Directory.Exists(temppath))
{
Directory.CreateDirectory(temppath);
}
}
}
if (string.IsNullOrEmpty(fileitem.FieldValues["File_x0020_Size"].ToString()))
{
context.Load(fileitem.Folder);
context.ExecuteQuery();
continue;
}
temppath = path + fileitem.FieldValues["FileDirRef"].ToString();
var fileRef = fileitem.FieldValues["FileRef"].ToString();
var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, fileRef);
var fileName = Path.Combine(temppath, fileitem.FieldValues["FileLeafRef"].ToString());
using (var fileStream = System.IO.File.Create(fileName))
{
fileInfo.Stream.CopyTo(fileStream);
}
}
Console.WriteLine("Total : " + fileCount + " files are copied to '" + path + "'");
Console.ReadKey();
}
}
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="spurl" value="https://****.crm8.dynamics.com"/>
<add key="username" value="******@***.onmicrosoft.com"/>
<add key="password" value="********"/>
<add key="entity" value="Case"/>
<add key="Path" value="C:\Sharepoint"/>
</appSettings>
</configuration>
////////////helper class////////////////////////////
using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SharePointManager
{
public class SPServiceHelper
{
public static string SPUrl;
private static bool _duplicatefound;
private static List<string> _fileNames;
public static List<string> FileNames
{
set { _fileNames = value; }
get { return _fileNames; }
}
public static bool Duplicatefound
{
set { _duplicatefound = value; }
get { return _duplicatefound; }
}
/// <summary>
/// To connect to SharePoint
/// </summary>
public static bool GetService(string spURL, string spUserName, string spPwd)
{
_duplicatefound = false;
_fileNames = new List<string>();
using (ClientContext context = new ClientContext(spURL))
{
bool isConnected = false;
try
{
var ss = new System.Security.SecureString();
Array.ForEach(spPwd.ToCharArray(), (c) => { ss.AppendChar(c); });
context.Credentials = new SharePointOnlineCredentials(spUserName, ss);
var web = context.Web;
context.Load(web);
context.ExecuteQuery();
if (context.Web != null)
{
return isConnected = true;
}
}
catch (Exception ex)
{
return false;
}
return false;
}
}
}
}
/////////////Main Class//////////////////////////////////////////////////////////////////////////////////////////
using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SharePointManager
{
class Program
{
static string servUrl = "";
static string uname = "";
static string password = "";
static string path = "";
static void Main(string[] args)
{
servUrl = ConfigurationManager.AppSettings["spurl"];
uname = ConfigurationManager.AppSettings["username"];
password = ConfigurationManager.AppSettings["password"];
path = ConfigurationManager.AppSettings["Path"];
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
if (string.IsNullOrEmpty(servUrl) || string.IsNullOrEmpty(uname) || string.IsNullOrEmpty(password))
return;
bool isService = SPServiceHelper.GetService(servUrl, uname, password);
if (isService)
{
Console.WriteLine("Sharepoint service created");
RetrieveData();
}
}
public static void RetrieveData()
{
ClientContext context = new ClientContext(servUrl);
var ss = new System.Security.SecureString();
Array.ForEach(password.ToCharArray(), (c) => { ss.AppendChar(c); });
context.Credentials = new SharePointOnlineCredentials(uname, ss);
Web web = context.Web;
ListCollection announcementsList = context.Web.Lists;
context.Load(announcementsList);
context.ExecuteQuery();
foreach (List listItem in announcementsList)
{
if (listItem.Title.ToLower() == ConfigurationManager.AppSettings["entity"].ToLower())
{
Console.WriteLine("Processing...");
int fileCount = 0;
ListCollection subfolders = context.Web.Lists;
CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
ListItemCollection item = listItem.GetItems(query);
context.Load(item);
context.ExecuteQuery();
foreach (ListItem fileitem in item)
{
fileCount++;
context.Load(fileitem);
context.ExecuteQuery();
string[] fileDirectory = fileitem.FieldValues["FileRef"].ToString().Split('/');
string temppath = path;
foreach (string folderName in fileDirectory)
{
if (!string.IsNullOrEmpty(folderName))
{
if (folderName.Contains('.'))
continue;
temppath = temppath + "\\" + folderName;
if (!Directory.Exists(temppath))
{
Directory.CreateDirectory(temppath);
}
}
}
if (string.IsNullOrEmpty(fileitem.FieldValues["File_x0020_Size"].ToString()))
{
context.Load(fileitem.Folder);
context.ExecuteQuery();
continue;
}
temppath = path + fileitem.FieldValues["FileDirRef"].ToString();
var fileRef = fileitem.FieldValues["FileRef"].ToString();
var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, fileRef);
var fileName = Path.Combine(temppath, fileitem.FieldValues["FileLeafRef"].ToString());
using (var fileStream = System.IO.File.Create(fileName))
{
fileInfo.Stream.CopyTo(fileStream);
}
}
Console.WriteLine("Total : " + fileCount + " files are copied to '" + path + "'");
Console.ReadKey();
}
}
}
}
}
thank you
ReplyDelete