/// <summary> /// Synchronous before event that occurs before an existing item is completely deleted. /// </summary> /// <param name="properties"> /// A Microsoft.SharePoint.SPItemEventProperties object that represents properties of the event handler. /// </param> public override void ItemDeleting(SPItemEventProperties properties) { //props.projectname = properties.ListItem.ID.ToString() + ";#" + properties.ListItem.Title; //props.web = properties.ListItem.Web; propStruct props = new propStruct(); SPWeb web = properties.ListItem.Web; { SortedList <string, DataTable> lstListDataToDelete = new SortedList <string, DataTable>(); foreach (SPList list in web.Lists) { string fieldname = ""; foreach (SPField field in list.Fields) { if (field.SchemaXml.Contains("Type=\"Lookup") && field.SchemaXml.ToLower().Contains("{" + properties.ListId.ToString().ToLower() + "}")) { fieldname = field.InternalName; SPSiteDataQuery query = new SPSiteDataQuery(); query.QueryThrottleMode = SPQueryThrottleOption.Override; //Used to set/reset throttling while retrieving records using CAML query. query.Lists = "<Lists><List ID=\"" + list.ID.ToString() + "\"/></Lists>"; //query.RowLimit = (uint)0; query.Query = "<Where><Eq><FieldRef Name=\"" + field.InternalName + "\" LookupId=\"True\"/><Value Type=\"Lookup\">" + properties.ListItemId + "</Value></Eq></Where>"; query.ViewFields = "<FieldRef Name=\"Title\"/>"; DataTable dt = web.GetSiteData(query); if (dt != null && dt.Rows.Count > 0) { lstListDataToDelete.Add(list.Title, dt); } } } if (fieldname != "") { } } props.lstListDataToDelete = lstListDataToDelete; props.weburl = web.Url; } Thread thrDownload = new Thread(new ParameterizedThreadStart(processDelete)); thrDownload.IsBackground = true; thrDownload.Start(props); }
//static void processList(SPList list, string fieldname, string projectname, SPWeb web) //{ // try // { // Hashtable hash = new Hashtable(); // foreach (SPListItem lItem in list.Items) // { // string pj = lItem[fieldname].ToString(); // if (pj == projectname || pj.Substring(pj.IndexOf(";#")).Length <= 2) // if (list.Title == CoreFunctions.getConfigSetting(web, "EPMLiveTaskCenter") || list.Title == "Resource Center") // list.Items[lItem.UniqueId].Delete(); // else // list.Items[lItem.UniqueId].Recycle(); // //hash.Add(lItem.UniqueId, " "); // } // } // catch (Exception ex) // { // logException("DeletingProject", ex.Message, ex.StackTrace); // } //} static void processDelete(object data) { try { bool isListHasEnabledThrottling = false; propStruct properties = (propStruct)data; SPSecurity.RunWithElevatedPrivileges( delegate() { using (SPSite site = new SPSite(properties.weburl)) { using (SPWeb web = site.OpenWeb()) { web.AllowUnsafeUpdates = true; foreach (string listname in properties.lstListDataToDelete.Keys) { try { SPList list = web.Lists[listname]; isListHasEnabledThrottling = list.EnableThrottling; list.EnableThrottling = false; //This will reset threshold for this list while cascade delete large number of items list.Update(); DataTable dt = properties.lstListDataToDelete[listname]; foreach (DataRow dr in dt.Rows) { try { list.GetItemById(int.Parse(dr["ID"].ToString())).Recycle(); } catch { } } list.EnableThrottling = isListHasEnabledThrottling; //Set threshold back to the list... list.Update(); } catch { } } } } }); //SPWeb spWeb = properties.web; //spWeb.AllowUnsafeUpdates = true; //string listid = spWeb.Lists["Project Center"].ID.ToString(); //for (int i = 0; i < spWeb.Lists.Count; i++) //{ // string fieldname = ""; // foreach (SPField field in spWeb.Lists[i].Fields) // { // if (field.SchemaXml.Contains("Type=\"Lookup") && field.SchemaXml.ToLower().Contains("{" + listid.ToLower() + "}")) // { // fieldname = field.Title; // } // } // if (fieldname != "") // processList(spWeb.Lists[i], fieldname, properties.projectname); //} } catch (Exception ex) { logException("DeletingProject", ex.Message, ex.StackTrace); } }