Domanda

Sto scrivendo un ricevitore di eventi su una libreria documenti per creare un nuovo elenco dal file Excel caricato. Esegue l'evento oggetto () e ItemUpdated () ().

Prima di chiamare un metodo per aggiungere nuovi articoli all'elenco che ho bisogno di cancellare gli elementi vecchi, inizialmente ho provato questo con il modo tradizionale (Iterating in ordine decrescente ed elimina tutti i record) e ora con 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 è un po 'di cosa migliore ma ancora non accurato il problema è che mentre Debuggin una volta che il controllo raggiunge la linea var deleteBatch = site.OpenWeb().ProcessBatchData(BuildBatchDeleteCommand(site.OpenWeb().Lists[listName]));

Torna al controllo nella schermata di caricamento del documento e la maggior parte dei tempi non colpisce il codice rimanente, tuttavia se i record sono veramente bassi (<100) quindi elaborano e prende il controllo alla riga successiva (InsertIntoList(ExcelRecords, site.OpenWeb().Lists[listName].Items);) .

Non è in grado di capire se è un problema di discussione, se faccio funzionare il mio filo mettono per dormire per qualche tempo fino a quando il ProcessBatch finisca il suo lavoro e il controllo sposta sulla riga successiva.

La funzione completa sembra

 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 per favore indicami la giusta direzione. Ti apprezzo davvero l'aiuto e il tempo.

Vishal

È stato utile?

Soluzione

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);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top