We have a SharePoint 2013 on-premises team site and we have a document library which have around 29,000 +++ items, and those files are structured under around 4,000 folders and sub-folder. now inside our Server-side event receiver, i am using the following method to copy files and folder, as follow:-
private void copyfiles(SPItemEventProperties properties, SPListItem currentItem, string from, string to)
{
try
{
SPFolder folder = properties.Web.GetFolder(properties.Web.ServerRelativeUrl + "/Shared Documents/" + currentItem["ReferenceNumber"] + "/" + from + "/");
SPDocumentLibrary currentDL = (SPDocumentLibrary)properties.Web.GetList(properties.Web.ServerRelativeUrl + "/Shared Documents/");
SPListItem softemplete = null;
TaxonomyFieldValue taxFieldValue = currentItem["CustomerName"] as TaxonomyFieldValue;
var label = taxFieldValue.Label;
var titlewithoutspecialchar = currentItem["Title"].ToString().Replace("~", " ").Replace("\"", " ").Replace("#", " ").Replace("%", " ").Replace("&", " ").Replace("*", " ").Replace(":", " ")
.Replace("<", " ").Replace(">", " ").Replace("?", " ").Replace("/", " ").Replace("\\", " ").Replace("{", " ").Replace("}", " ").Replace("|", " ").Replace(".", " ");
if (folder.ItemCount > 0)
{
SPList list = properties.Web.GetList(properties.Web.ServerRelativeUrl + "/Shared Documents/");
SPQuery query = new SPQuery();
query.Folder = folder;
SPListItemCollection listitem = list.GetItems(query);
foreach (SPListItem i in listitem)
{
if ((i.Name.ToLower().Contains(currentItem["ReferenceNumber"].ToString().ToLower() + " -") &&
(i.Name.ToLower().Contains("sof") || i.Name.ToLower().Contains("pof") || i.Name.ToLower().Contains("qo"))
)
||
i.Name.ToLower().Contains("request for approval"))
{
softemplete = i;
if (softemplete != null)
{
byte[] fileBytes = softemplete.File.OpenBinary();
string destUrl = properties.Web.ServerRelativeUrl + "/" + currentDL.RootFolder.Url + "/" +
currentItem["ReferenceNumber"] + "/" + to + "/" + currentItem["ReferenceNumber"] + " - " + label + " - " + titlewithoutspecialchar.Trim() + ".xlsx";
if (i.Name.ToLower().Contains("request for approval.oft"))
{
destUrl = properties.Web.ServerRelativeUrl + "/" + currentDL.RootFolder.Url + "/" +
currentItem["ReferenceNumber"] + "/"+to+"/Request for Approval.oft";
}
else if (i.Name.ToLower().Contains("request for approval.xlsm"))
{
destUrl = properties.Web.ServerRelativeUrl + "/" + currentDL.RootFolder.Url + "/" +
currentItem["ReferenceNumber"] + "/" + to + "/Request for Approval.xlsm";
}
try
{
SPFile destFile = currentDL.RootFolder.Files.Add(destUrl, fileBytes, false);
}
catch
{
}
}
}
}
}
}
catch
{
}
}
Now the this method is taking quite long to complete the copy (sometimes around 10 seconds), although i am using CAML query to only search the files inside the desired folder usingquery.Folder = folder;
. so can anyone provide any advice about improving this method? as i think it is already tuned to the max, is this correct?