Hello @sravya shivapuram ,
Welcome to Q&A Forum!
To dynamically copy the files and folders using CSOM, follow below code.
public void CloneLibraryItems(string srcLibrary, string srcUrl, string destUrl, string userName, SecureString pwd)
{
string srclibraryname = string.Empty;
string fileName = string.Empty;
string folderPath = string.Empty;
try
{
ClientContext srcContext = new ClientContext(srcUrl);
ClientContext destContext = new ClientContext(destUrl);
srcContext.Credentials = new SharePointOnlineCredentials(userName, pwd);
srcContext.RequestTimeout = Timeout.Infinite;
Web srcWeb = srcContext.Web;
List srcList = srcWeb.Lists.GetByTitle(srcLibrary);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = “<View Scope=’RecursiveAll’><Where><Eq><FieldRef Name='_ModerationStatus' /><Value Type='ModStat'>Approved</Value></Eq></Where></View>”;
ListItemCollection itemColl = srcList.GetItems(camlQuery);
srcContext.Load(itemColl);
srcContext.ExecuteQuery();
destContext.Credentials = new SharePointOnlineCredentials(userName, pwd);
destContext.RequestTimeout = Timeout.Infinite;
Web destWeb = destContext.Web;
destContext.Load(destWeb);
destContext.ExecuteQuery();
string _path = destWeb.ServerRelativeUrl;
if (itemColl.Count > 0)
{
srclibraryname = itemColl[0].FieldValues[“FileDirRef”].ToString();
string[] srcurlSplit = srclibraryname.Split(‘/’);
srclibraryname = srcurlSplit[srcurlSplit.Count() – 1];
foreach (ListItem doc in itemColl)
{
if (doc.FileSystemObjectType == FileSystemObjectType.File)
{
fileName = doc[“FileRef”].ToString();
string[] fileNames = fileName.Split(new string[] { srclibraryname }, StringSplitOptions.None);
fileName = fileNames[fileNames.Count() – 1];
OSP.File file = doc.File;
srcContext.Load(file);
srcContext.ExecuteQuery();
FileInformation fileInfo = OSP.File.OpenBinaryDirect(srcContext, file.ServerRelativeUrl);
OSP.File.SaveBinaryDirect(destContext, _path + “/” + srclibraryname + fileName, fileInfo.Stream, true);
}
else if (doc.FileSystemObjectType == FileSystemObjectType.Folder)
{
folderPath = doc[“FileRef”].ToString();
string[] fileNames = folderPath.Split(new string[] { srclibraryname }, StringSplitOptions.None);
folderPath = fileNames[fileNames.Count() – 1];
folderPath = folderPath.TrimStart(new Char[] { ‘/’ });
//Console.WriteLine(“Folder Path :” + folderPath);
OSP.Folder folder = CreateFolder(destContext.Web, srcLibrary, folderPath);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Reference:
Thanks,
Echo Du
=================================
Updated Answer =============================
Hi @sravya shivapuram ,
private static CopyFiles()
{
string source = "https://abc.sharepoint.com/sites/applications/appname/123456/";
OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
string userName = "";
string password = "";
var secure = new SecureString();
foreach (var c in password.ToCharArray()) secure.AppendChar(c);
var context = authMgr.GetSharePointOnnlineAuthenticatedContextTenant(sourceUrl,userName,password);
try
{
List targetList = context.Web.Lists.GetByTitle("");
camlQuery oQuery = CamlQuery.CreateAllItemsQuery();
ListItemCollection oCollection = targetList.GetItems(oQuery);
context.Load(oCollection);
context.ExecuteQuery();
string srcUrl = "https://abc.sharepoint.com/sites/applications/appname/123456/Shared%20Documents/"
string desUrl = "https://abc.sharepoint.com/sites/Archive/Records/"
MoveCopyOptions mco = new MoveCopyOptions
{
KeepBoth = false
};
foreach(ListItem oItem in oCollection)
{
string a = oItem["FileRef"].ToString();
int startInd = a.LastIndexOf("/")+1;
int lastInd = (a.Length - a.LastIndexOf("/"))-1;
string docTit = a.Substring(startInd,lastInd);
Console.WriteLine(a.Substring(startInd,lastInd));
MoveCopyUtil.CopyFile(context, srcUrl+docTit, desUrl+docTit, true, mco);
}
context.ExecuteQuery();
}
}
Thanks,
Echo Du
===========================================
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.