protected void DeleteWorkItem(SPWorkItemCollection itemsToProcess, SPWorkItem currentItem) { int itemIndex = -1; int count = itemsToProcess.Count; for (int i = 0; i < count; i++) { if (itemsToProcess[i].Id == currentItem.Id) { itemIndex = i; break; } } if (itemIndex > -1) { try { using (SPSite site = new SPSite(currentItem.SiteId)) { using (SPWeb web = site.OpenWeb(currentItem.WebId)) { SPWorkItemCollection deletableCollection = itemsToProcess.SubCollection(site, web, (uint)itemIndex, (uint)itemIndex + 1); deletableCollection.DeleteWorkItem(currentItem.Id); } } } catch (Exception ex) { ex.ToString(); } } }
/// <summary> /// Deletes and of the TimerJob WorkItems for the GlymaExportWorkItemTimerJob if they still exist and are in error state. /// </summary> /// <param name="job">The ExportJob</param> /// <param name="site">The SPSite</param> /// <param name="web">The SPWeb</param> private void CleanupErrorWorkItems(ExportJob job, SPSite site, SPWeb web) { //ensure that only jobs in error state are attempted to be removed if (job.Status == ExportStatus.Error) { try { SPWorkItemCollection workItemsCollection = new SPWorkItemCollection(site, GlymaExportWorkItemTimerJob.WorkItemTypeId); uint colCount, rowCount = 0; object workItems = null; site.GetWorkItems(workItemsCollection, out colCount, out rowCount, out workItems); if (workItemsCollection.Count > 0) { SPWorkItemCollection subCollection = workItemsCollection.SubCollection(site, web, 0, (uint)workItemsCollection.Count); subCollection.DeleteWorkItem(job.Id); } } catch (Exception) { } } }
/// <summary> /// Deletes an ExportJob if it's in the Scheduled, Completed, or Error ExportStates. /// </summary> /// <param name="job">The ExportJob to delete</param> /// <returns>The ExportJob that was deleted</returns> public ExportJobResponse DeleteExportJob(ExportJob job) { ExportJobResponse response = new ExportJobResponse(); try { Guid webID = SPContext.Current.Web.ID; Guid siteID = SPContext.Current.Site.ID; SPSecurity.RunWithElevatedPrivileges(delegate() { SPList exportsList = null; using (SPSite site = new SPSite(siteID)) { using (SPWeb web = site.OpenWeb(webID)) { if (web != null && site != null) { exportsList = web.TryGetList(web.GetServerRelativeListUrlPrefix() + "GlymaExports"); if (exportsList != null) { SPQuery query = new SPQuery(); query.Query = "<Where>" + "<Eq><FieldRef Name='Title' /><Value Type='Text'>" + job.Id.ToString() + "</Value></Eq>" + "</Where>"; SPListItemCollection exports = exportsList.GetItems(query); // There can only be one ExportJob with the job ID (unique values enforced in SP list for Title column) if (exports.Count > 0) { SPListItem exportItem = exports[0]; if (exportItem != null) { string exportStatusStr = exportItem["ExportStatus"] as string; ExportStatus exportStatus = (ExportStatus)Enum.Parse(typeof(ExportStatus), exportStatusStr, true); if (exportStatus == ExportStatus.Scheduled) { SPWorkItemCollection workItemsCollection = new SPWorkItemCollection(site, GlymaExportWorkItemTimerJob.WorkItemTypeId); uint colCount, rowCount = 0; object workItems = null; site.GetWorkItems(workItemsCollection, out colCount, out rowCount, out workItems); //gets all work items for this site of the type GlymaExportWorkItemTimerJob if (workItemsCollection.Count > 0) { // Delete the work item that this export job created SPWorkItemCollection subCollection = workItemsCollection.SubCollection(site, web, 0, (uint)workItemsCollection.Count); subCollection.DeleteWorkItem(job.Id); } } if (exportStatus == ExportStatus.Scheduled || exportStatus == ExportStatus.Completed || exportStatus == ExportStatus.Error) { exportItem.Delete(); //delete the item after it has been cancelled } else if (exportStatus == ExportStatus.Processing) { throw new Exception("The export job is currently processing and cannot be deleted."); } } response.ExportJob = job; } } else { throw new Exception("Failed to find the Glyma Exports list."); } } else { throw new Exception("The SPSite and/or the SPWeb were null."); } } } }); } catch (Exception ex) { ExportError error = new ExportError() { ErrorMessage = "Failed to cancel the Glyma map export job." }; throw new FaultException <ExportError>(error, ex.ToString()); } return(response); }