SP ProcessBatchData dentro del receptor de eventos, problema de rendimiento lento
-
10-12-2019 - |
Pregunta
Estoy escribiendo un receptor de eventos en una biblioteca de documentos para crear una nueva lista desde el archivo de Excel cargado. Se ejecuta en el evento itemadaDed () y itemupDated ().
Antes de llamar a un método para agregar nuevos elementos a la lista, necesito borrar elementos antiguos, inicialmente intenté esto con la forma tradicional (iterando en orden descendente y eliminé todos los registros) y ahora con el ProcessBatchData
using (SPSite site = new SPSite(properties.WebUrl))
{
if (site.OpenWeb().Lists[listName].Items.Count > 0)
{
var deleteBatch = site.OpenWeb().ProcessBatchData(BuildBatchDeleteCommand(site.OpenWeb().Lists[listName]));
}
InsertIntoList(ExcelRecords, site.OpenWeb().Lists[listName].Items);
}
ProcessBatachData es algo que mejor, pero aún no es preciso, el problema es que, si bien, si el control, una vez que el control llega a la línea var deleteBatch = site.OpenWeb().ProcessBatchData(BuildBatchDeleteCommand(site.OpenWeb().Lists[listName]));
Toma el control de nuevo a la pantalla de carga del documento y la mayoría de las veces no pulsa el código restante, sin embargo, si los registros son realmente bajos (<100), luego procesan y toma el control a la siguiente línea (InsertIntoList(ExcelRecords, site.OpenWeb().Lists[listName].Items);
) .
No puede averiguar si es un problema de hilo, si hago que este trabajo me ponga el hilo para dormir durante algún tiempo hasta que el ProcessBACK finalice su trabajo y el control de control a la siguiente línea.
La función completa parece
public override void ItemUpdated(SPItemEventProperties properties)
{
UpdateMarketingCalendar(properties);
base.ItemUpdated(properties);
}
private void UpdateMarketingCalendar(SPItemEventProperties properties)
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
try
{
using (SPSite site = new SPSite(properties.WebUrl))
{
SPList list = site.OpenWeb().Lists[listName];
bool recordsRetrieved;
DataTable ExcelRecords = GetRecordsFromExcel(properties.ListItem, out recordsRetrieved);
if (recordsRetrieved)
{
if (list.ItemCount > 0)
{
var deleteBatch = site.OpenWeb().ProcessBatchData(BuildBatchDeleteCommand(list));
}
InsertIntoList(ExcelRecords, list.Items);
}
}
}
});
}
Geeks Por favor, apúntame a la dirección correcta. Realmente aprecio tu ayuda y tiempo.
vishal
Solución
This is probably not the root cause of your performance issues, but part of it. Your code is written in a quite inefficient way, opening webs to the right and left and looking for the list multiple times. Something like this does the same thing but more efficiently:
If your feature is web scoped:
var web = properties.Feature.Parent as SPWeb;
if site scoped
var site = properties.Feature.Parent as SPSite;
if(site == null) return;
var web = site.OpenWeb(properties.WebUrl);
and as MdMazzotti pointed out you would actually need to dispose the web in this scenario, I would use the using statement, replace the last line with this:
using(var web = site.OpenWeb(properties.WebUrl)){
and add an extra }
at the end of the following
:
if(web == null || !web.Exists) return;
var list = web.Lists[listName]
if (list != null && list.Items.Count > 0)
{
var deleteBatch = web.ProcessBatchData(BuildBatchDeleteCommand(list));
}
InsertIntoList(ExcelRecords, list.Items);
}