/// <summary> /// Registers an event receiver if required. /// </summary> /// <param name="this"> /// The this. /// </param> /// <param name="eventReceiverType"> /// The event receiver type. /// </param> /// <param name="assemblyName"> /// The assembly name. /// </param> /// <param name="className"> /// The class name. /// </param> /// <param name="synchronization"> /// The synchronization. /// </param> public static void RegisterEventReceiverIfRequired(this SPList @this, SPEventReceiverType eventReceiverType, string assemblyName, string className, SPEventReceiverSynchronization synchronization = SPEventReceiverSynchronization.Default) { // Validation.ArgumentIsNotNull(() => @this); // Validation.ArgumentIsNotNullOrWhitespace(() => assemblyName); // Validation.ArgumentIsNotNullOrWhitespace(() => className); SPEventReceiverDefinition eventReceiverDefinition = @this.EventReceivers.OfType<SPEventReceiverDefinition>().FirstOrDefault(receiver => receiver.Type == eventReceiverType && receiver.Assembly.Equals(assemblyName) && receiver.Class.Equals(className)); if (eventReceiverDefinition == null) { @this.EventReceivers.Add(eventReceiverType, assemblyName, className); eventReceiverDefinition = @this.EventReceivers.OfType<SPEventReceiverDefinition>().First(receiver => receiver.Type == eventReceiverType && receiver.Assembly.Equals(assemblyName) && receiver.Class.Equals(className)); eventReceiverDefinition.Synchronization = synchronization; eventReceiverDefinition.Update(); Type type = Type.GetType(string.Format(CultureInfo.InvariantCulture, "{0}, {1}", className, assemblyName)); if (type != null && typeof(StateMachineItemEventReceiverBase).IsAssignableFrom(type) && [email protected](SystemFieldNames.ChangeDetectionFieldName)) { @this.Fields.Add(SystemFieldNames.ChangeDetectionFieldName, SPFieldType.Text, false); @this.Update(); IEnumerable<SPField> changeDetectionFields = @this.Fields.OfType<SPField>().Where(field => SystemFieldNames.ChangeDetectionFieldName.Equals(field.Title)).ToList(); foreach (SPField changeDetectionField in changeDetectionFields) { changeDetectionField.Hidden = true; changeDetectionField.Update(); } @this.Update(); } } }
/// <summary> /// Event Receiver Info (List) /// </summary> /// <param name="list">The list</param> /// <param name="type">The event receiver type</param> /// <param name="syncType">The synchronization type</param> public EventReceiverInfo(ListInfo list, SPEventReceiverType type, SPEventReceiverSynchronization syncType) { this.List = list; this.EventOwner = EventReceiverOwner.List; this.ReceiverType = type; this.SynchronizationType = syncType; }
public EventInfos(Guid listID, string recClass, string recAssembly, SPEventReceiverType recType) { ListID = listID; SClass = recClass; SAssembly = recAssembly; Type = recType; }
/// <summary> /// Adds the event handler to the content type. /// </summary> /// <param name="contentType">The content type.</param> /// <param name="name">The name of the event.</param> /// <param name="assembly">The assembly containin the event receiver.</param> /// <param name="className">Name of the event receiver class.</param> /// <param name="type">The event receiver type.</param> /// <param name="sequence">The sequence.</param> /// <param name="sync">The synchronization type.</param> public void AddEventHandler( SPContentType contentType, string name, string assembly, string className, SPEventReceiverType type, int sequence, SPEventReceiverSynchronization sync) { // Guard if (contentType == null) { throw new ArgumentNullException("contentType"); } SPEventReceiverDefinition definition = GetEventHandler(contentType.EventReceivers, name, type); if (definition == null) { contentType.EventReceivers.Add(type, assembly, className); definition = GetEventHandler(contentType.EventReceivers, className, type); } definition.Name = name; definition.SequenceNumber = sequence; definition.Synchronization = sync; definition.Update(); }
private void OkButton_Click(object sender, EventArgs e) { try { foreach (string typeName in this.TypesCheckedListBox.CheckedItems) { try { Type selectedObjectType = this.SelectedObject.GetType(); SPEventReceiverType eventType = (SPEventReceiverType)Enum.Parse(typeof(SPEventReceiverType), typeName); int maxSequence = 9999; SPEventReceiverDefinitionCollection definitions = null; string parentName = string.Empty; if (typeof(SPWeb).IsAssignableFrom(selectedObjectType)) { SPWeb web = (SPWeb)this.SelectedObject; parentName = web.Name; definitions = web.EventReceivers; } else if (typeof(SPList).IsAssignableFrom(selectedObjectType)) { SPList list = (SPList)this.SelectedObject; parentName = list.Title; definitions = list.EventReceivers; } else { throw new NotSupportedException("The selected object type is not supported."); } if (definitions != null) { this.m_ParentForm.LastSelectedGACObjectType = this.SelectedType; Utility.ProcessEventHookAction(this, this.SelectedObject, typeName, this.SelectedAssembly, this.SelectedType, this.UseNextSequenceNumberCheckBox.Checked); } } catch (Exception exception) { MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand); } } MessageBox.Show(this, "The selected events have been hooked. The event sequence should be reviewed to ensure that events execute in the expected order.", "Event Hooks Added", MessageBoxButtons.OK, MessageBoxIcon.Information); this.DialogResult = System.Windows.Forms.DialogResult.OK; this.Close(); } catch (Exception ex) { this.HandleException(ex); } }
public void HandleSecurityRules(SPListItem listItem, SPEventReceiverType eventType) { SPSecurity.RunWithElevatedPrivileges(delegate() { SecuritySettings settings = listItem.ParentList.GetCustomSettings<SecuritySettings>(TVMCORPFeatures.TVS); if (settings == null) return; settings.Rules.Sort(delegate(Rule rule1, Rule rule2) { return rule1.Order.CompareTo(rule2.Order); }); foreach (Rule r in settings.Rules) { bool ruleQualified = ValidateRule(r, listItem, eventType); if (ruleQualified) { bool itemQuailfied = ValidateItem(listItem, r); if (itemQuailfied) { ApplySecuritySettings(listItem, r); break; } } } }); }
public bool EventReceiverDefinitionExist(SPEventReceiverDefinitionCollection collection, SPEventReceiverType type, string assemblyFullName, string classFullName) { if (collection == null) { throw new ArgumentNullException("collection"); } if (string.IsNullOrEmpty(assemblyFullName)) { throw new ArgumentNullException("assemblyFullName"); } if (string.IsNullOrEmpty(classFullName)) { throw new ArgumentNullException("classFullName"); } // If there is nothing in the collection we don't even need to check. if (collection.Count <= 0) { return false; } // Get the event receiver if it exists. SPEventReceiverDefinition eventReceiver = this.GetEventReceiverDefinition(collection, type, assemblyFullName, classFullName); return eventReceiver != null; }
public SPEventReceiverDefinition GetEventReceiverDefinition(SPEventReceiverDefinitionCollection collection, SPEventReceiverType type, string assemblyFullName, string classFullName) { if (collection == null) { throw new ArgumentNullException("collection"); } if (string.IsNullOrEmpty(assemblyFullName)) { throw new ArgumentNullException("assemblyFullName"); } if (string.IsNullOrEmpty(classFullName)) { throw new ArgumentNullException("classFullName"); } foreach (SPEventReceiverDefinition eventReceiver in collection) { bool isCorrectType = eventReceiver.Type == type; bool isCorrectAssembly = string.Compare(eventReceiver.Assembly, assemblyFullName, StringComparison.OrdinalIgnoreCase) == 0; bool isCorrectClass = string.Compare(eventReceiver.Class, classFullName, StringComparison.OrdinalIgnoreCase) == 0; if (isCorrectType && isCorrectAssembly && isCorrectClass) { return eventReceiver; } } return null; }
// Uncomment the method below to handle the event raised before a feature is deactivated. public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { using (SPSite site = properties.Feature.Parent as SPSite) { using (SPWeb web = site.RootWeb) { SPList oList = web.Lists[Constants.WebRegistry.ListTitle]; const SPEventReceiverType _eventType = SPEventReceiverType.ItemAdding; const SPEventReceiverType _eventType2 = SPEventReceiverType.ItemDeleting; const SPEventReceiverType _eventType3 = SPEventReceiverType.ItemUpdating; for (int i = oList.EventReceivers.Count - 1; i >= 0; i--) { if (oList.EventReceivers[i].Type.Equals(_eventType) || oList.EventReceivers[i].Type.Equals(_eventType2) || oList.EventReceivers[i].Type.Equals(_eventType3)) { try { oList.EventReceivers[i].Delete(); } catch (Exception e) { Logger.WriteLog(Logger.Category.High, "DevyatkinEventReceiver", e.ToString()); } } } SPList list = web.Lists.TryGetList(Constants.WebRegistry.ListTitle); web.Lists.Delete(list.ID); } } }
/// <summary> /// Event Receiver Info (Content Type) /// </summary> /// <param name="contentType">The content type</param> /// <param name="type">The event receiver type</param> /// <param name="syncType">The synchronization type</param> public EventReceiverInfo(ContentTypeInfo contentType, SPEventReceiverType type, SPEventReceiverSynchronization syncType) { this.ContentType = contentType; this.ReceiverType = type; this.EventOwner = EventReceiverOwner.ContentType; this.SynchronizationType = syncType; }
public static void Register(SPList list, SPEventReceiverType eventType, SPEventReceiverSynchronization synchronization, int sequenceNumber, string className) { if (!IsRegistered(list, eventType, className)) { list.ParentWeb.AllowUnsafeUpdates = true; var assemblyName = typeof(IronItemEventReceiver).Assembly.FullName; var typeName = typeof(IronItemEventReceiver).FullName; var receiver = list.EventReceivers.Add(); receiver.Type = eventType; receiver.Assembly = assemblyName; receiver.Synchronization = synchronization; receiver.HostType = SPEventHostType.List; receiver.HostId = list.ID; receiver.Class = typeName; receiver.Name = className; receiver.Data = className; receiver.SequenceNumber = sequenceNumber; receiver.Update(); list.Update(); list.ParentWeb.AllowUnsafeUpdates = false; } }
private SPEventReceiverDefinition AddEventReceiverDefinition(SPList list, SPEventReceiverType type, string assemblyName, string className, SPEventReceiverSynchronization syncType, int sequenceNumber) { SPEventReceiverDefinition eventReceiverDefinition = null; // Try Parse the Assembly Name var classType = Type.GetType(string.Format(CultureInfo.InvariantCulture, "{0}, {1}", className, assemblyName)); if (classType != null) { var assembly = Assembly.GetAssembly(classType); var isAlreadyDefined = list.EventReceivers.Cast <SPEventReceiverDefinition>().Any(x => (x.Class == className) && (x.Type == type)); // If definition isn't already defined, add it to the list if (!isAlreadyDefined) { eventReceiverDefinition = list.EventReceivers.Add(); eventReceiverDefinition.Type = type; eventReceiverDefinition.Assembly = assembly.FullName; eventReceiverDefinition.Synchronization = syncType; eventReceiverDefinition.Class = className; eventReceiverDefinition.SequenceNumber = sequenceNumber; eventReceiverDefinition.Update(); list.Update(); } } return(eventReceiverDefinition); }
public ListDefinition AndEvent <TEventClass>(SPEventReceiverType eventType) { if (!events.ContainsKey(eventType)) { events.Add(eventType, typeof(TEventClass)); } return(this); }
/// <summary> /// Event Receiver Info (Content Type) /// </summary> /// <param name="contentType">The content type</param> /// <param name="type">The event receiver type</param> /// <param name="syncType">The synchronization type</param> /// <param name="assemblyName">The full name of the Assembly</param> /// <param name="className">The fullname of the Type/Class </param> public EventReceiverInfo(ContentTypeInfo contentType, SPEventReceiverType type, SPEventReceiverSynchronization syncType, string assemblyName, string className) { this.ContentType = contentType; this.ReceiverType = type; this.SynchronizationType = syncType; this.AssemblyName = assemblyName; this.ClassName = className; }
/// <summary> /// Event Receiver Info (List) /// </summary> /// <param name="list">The list</param> /// <param name="type">The event receiver type</param> /// <param name="syncType">The synchronization type</param> /// <param name="assemblyName">The full name of the Assembly</param> /// <param name="className">The fullname of the Type/Class </param> public EventReceiverInfo(ListInfo list, SPEventReceiverType type, SPEventReceiverSynchronization syncType, string assemblyName, string className) { this.List = list; this.ReceiverType = type; this.SynchronizationType = syncType; this.AssemblyName = assemblyName; this.ClassName = className; }
public static void Unregister(SPList list, SPEventReceiverType eventType, string className) { var receiver = list.EventReceivers.OfType <SPEventReceiverDefinition>().FirstOrDefault(e => e.Class == className && (e.Type & eventType) == eventType); if (receiver != null) { receiver.Delete(); } }
public static void Unregister(SPContentType ct, SPEventReceiverType eventType, string className, bool updateChildren) { var receiver = ct.EventReceivers.OfType <SPEventReceiverDefinition>().FirstOrDefault(e => e.Class == className && (e.Type & eventType) == eventType); if (receiver != null) { receiver.Delete(); ct.Update(updateChildren); } }
private void AddEventReceiverToWeb(SPWeb currentWeb, SPEventReceiverType type) { SPEventReceiverDefinition def = currentWeb.EventReceivers.Add(); def.Name = "Web Event Receiver - " + type.ToString(); def.Type = type; def.Assembly = Assembly.GetExecutingAssembly().FullName; def.Class = eventRcvClass; def.Update(); currentWeb.Update(); }
/// <summary> /// This method is fake. All the parameters are required, even though not all of them are used /// </summary> private void AddEventReceiver( EventReceiverManager eventReceiver, SPList list, string assembly, string className, SPEventReceiverType type, ref XElement element) { element.Add(DummyString); AddEventReceiverWasCalled = true; }
public bool HasEventReceiver(SPList taskList, SPEventReceiverType type, string assembly, string className) { string assemblyLow = assembly.ToLowerInvariant(); string classNameLow = className.ToLowerInvariant(); Func <SPEventReceiverDefinition, bool> predicate = er => er.Assembly.ToLowerInvariant() == assemblyLow && er.Class.ToLowerInvariant() == classNameLow && er.Type == type; return(taskList.EventReceivers.Cast <SPEventReceiverDefinition>().Any(predicate)); }
//public static List<SPFile> GetRelateddocuments(this SPListItem item) //{ // List<SPFile> listFile = new List<SPFile>(); // string contractNumberFieldName = item.Web.Site.GetFeaturePropertyValue(Constants.Workflow.INTEL_WF_FEATURE_ID, Constants.Workflow.CONTRACT_NUMBER_FIELD_NAME); // if (string.IsNullOrEmpty(contractNumberFieldName) || !item.Fields.ContainsField(contractNumberFieldName)) // return null; // if (item[contractNumberFieldName] == null) return null; // string contentTypeFieldName = item.Web.Site.GetFeaturePropertyValue(Constants.Workflow.INTEL_WF_FEATURE_ID, Constants.Workflow.CONTENT_TYPE_FIELD_NAME); // if (string.IsNullOrEmpty(contentTypeFieldName) || !item.Web.Site.RootWeb.AvailableFields.ContainsField(contentTypeFieldName)) // return null; // string includeWithDeliveryFieldName = item.Web.Site.GetFeaturePropertyValue(Constants.Workflow.INTEL_WF_FEATURE_ID, Constants.Workflow.INCLUDE_WITH_DELIVERY_FIELD_NAME); // if (string.IsNullOrEmpty(includeWithDeliveryFieldName) || !item.Web.Site.RootWeb.AvailableFields.ContainsField(includeWithDeliveryFieldName)) // return null; // string relatedContractNumberFieldName = item.Web.Site.GetFeaturePropertyValue(Constants.Workflow.INTEL_WF_FEATURE_ID, Constants.Workflow.RELATED_CONTRACT_NUMBER_FIELD_NAME); // if (string.IsNullOrEmpty(relatedContractNumberFieldName) || !item.Web.Site.RootWeb.AvailableFields.ContainsField(relatedContractNumberFieldName)) // return null; // SPField contractNumberField = item.Fields[contractNumberFieldName]; // SPField contentTypeField = item.Web.Site.RootWeb.AvailableFields[contentTypeFieldName]; // SPField includeWithDeliveryField = item.Web.Site.RootWeb.AvailableFields[includeWithDeliveryFieldName]; // SPField relatedContractNumberField = item.Web.Site.RootWeb.AvailableFields[relatedContractNumberFieldName]; // SearchCriteria criteriaContentType = new SearchCriteria(); // criteriaContentType.FieldId = contentTypeField.Id.ToString(); // criteriaContentType.FieldType = contentTypeField.Type; // criteriaContentType.Operator = Operators.Equal; // criteriaContentType.Value = "Attachment"; // SearchCriteria criteriaIncludeWithDelivery = new SearchCriteria(); // criteriaIncludeWithDelivery.FieldId = includeWithDeliveryField.Id.ToString(); // criteriaIncludeWithDelivery.FieldType = includeWithDeliveryField.Type; // criteriaIncludeWithDelivery.Operator = Operators.Equal; // criteriaIncludeWithDelivery.Value = "1"; // SearchCriteria criteriaRelatedContractNumber = new SearchCriteria(); // criteriaRelatedContractNumber.FieldId = relatedContractNumberField.Id.ToString(); // criteriaRelatedContractNumber.FieldType = relatedContractNumberField.Type; // criteriaRelatedContractNumber.Operator = Operators.Equal; // criteriaRelatedContractNumber.Value = item[contractNumberFieldName].ToString(); // List<SearchCriteria> searchCriteriaList = new List<SearchCriteria>(); // searchCriteriaList.Add(criteriaContentType); // searchCriteriaList.Add(criteriaIncludeWithDelivery); // searchCriteriaList.Add(criteriaRelatedContractNumber); // SearchDefinition searchDefinition = new SearchDefinition(); // searchDefinition.UseGlobalOperatorOR = false; // searchDefinition.SearchCriteriaList = searchCriteriaList; // FieldSetting contractNumberFieldResult = new FieldSetting(); // contractNumberFieldResult.FieldId = contractNumberField.Id.ToString(); // List<FieldSetting> resultFields = new List<FieldSetting>(); // resultFields.Add(contractNumberFieldResult); // string strCAML = CAMLHelper.GetCAMLQueryFromSearchDefinition(searchDefinition); // SPSiteDataQuery dataQuery = new SPSiteDataQuery(); // dataQuery.Query = strCAML; // dataQuery.Lists = CAMLHelper.GetCAMLListBaseTypeSearch(SearchListBaseType.DocumentLibrary); // dataQuery.ViewFields = CAMLHelper.GetCAMLFieldsResult(item.Web, null, resultFields); // dataQuery.Webs = CAMLHelper.GetCAMLScopeSearch(0); // try // { // DataTable searchResultsData = null; // searchResultsData = item.Web.GetSiteData(dataQuery); // foreach (DataRow row in searchResultsData.Rows) // { // SPList list = item.Web.Lists.GetList(new Guid(row["ListId"].ToString()), false); // SPListItem itemGet = list.GetItemById(Convert.ToInt32(row["ID"].ToString())); // if (itemGet.File != null) // listFile.Add(itemGet.File); // } // } // catch { } // return listFile; //} public static void AddEventReceiver(this SPList list, string eventName, SPEventReceiverType type, string assemblyName, string className) { SPEventReceiverDefinition eventDefinition = list.EventReceivers.Add(); eventDefinition.Name = eventName; eventDefinition.Type = type; eventDefinition.Synchronization = SPEventReceiverSynchronization.Synchronous; eventDefinition.Assembly = assemblyName; eventDefinition.Class = className; eventDefinition.Update(); }
public void DeleteEventReceiverDefinition(SPContentType contentType, SPEventReceiverType type, string className) { var eventReceiverDefinition = contentType.EventReceivers.Cast<SPEventReceiverDefinition>().FirstOrDefault(x => (x.Class == className) && (x.Type == type)); // If definition isn't already defined, add it to the content type if (eventReceiverDefinition != null) { var eventToDelete = contentType.EventReceivers.Cast<SPEventReceiverDefinition>().Where(eventReceiver => eventReceiver.Type == eventReceiverDefinition.Type).ToList(); eventToDelete.ForEach(c => c.Delete()); contentType.Update(true); } }
public void UnlinkEventHandlerToList(string assemblyName, string className, string aspEventReceiverType) { try { var obj = new Helper(SiteUrl, WebUrl, ListName); SPList list = obj.GetList(); var erdc = list.EventReceivers; var eventsToDelete = new List <SPEventReceiverDefinition>(); SPEventReceiverType eventType = SPEventReceiverType.InvalidReceiver; if (aspEventReceiverType == "ItemUpdated") { eventType = SPEventReceiverType.ItemUpdated; } if (aspEventReceiverType == "ItemAdded") { eventType = SPEventReceiverType.ItemAdded; } if (aspEventReceiverType == "ItemDeleting") { eventType = SPEventReceiverType.ItemDeleting; } foreach (SPEventReceiverDefinition erd in erdc) { if (erd != null && erd.Assembly == assemblyName && erd.Type == eventType && erd.Class == className) { try { eventsToDelete.Add(erd); } catch (Exception e) { Utilities.LogException("Error in Clist.UnlinkEventHandlerToList: " + e.Message); } } } foreach (SPEventReceiverDefinition er in eventsToDelete) { er.Delete(); } } catch (SPException ex) { Utilities.LogException("Error in Clist.UnlinkEventHandlerToList: " + ex.Message); } }
public void DeleteListEventReceiverDefinition(SPList list, SPEventReceiverType type, string className) { var eventReceiverDefinition = list.EventReceivers.Cast <SPEventReceiverDefinition>().FirstOrDefault(x => (x.Class == className) && (x.Type == type)); // If definition isn't already defined, add it to the content type if (eventReceiverDefinition != null) { var eventToDelete = list.EventReceivers.Cast <SPEventReceiverDefinition>().Where(eventReceiver => eventReceiver.Type == eventReceiverDefinition.Type).ToList(); eventToDelete.ForEach(c => c.Delete()); list.Update(); } }
/// <summary> /// Adds an event receiver to the specified target /// </summary> /// <param name="url">The URL.</param> /// <param name="contentTypeName">Name of the content type.</param> /// <param name="target">The target.</param> /// <param name="assembly">The assembly.</param> /// <param name="className">Name of the class.</param> /// <param name="type">The type.</param> /// <param name="sequence">The sequence.</param> /// <param name="name">The name.</param> public static void Add(string url, string contentTypeName, TargetEnum target, string assembly, string className, SPEventReceiverType type, int sequence, string name) { using (SPSite site = new SPSite(url)) using (SPWeb web = site.OpenWeb()) { SPContentType contentType = null; SPEventReceiverDefinitionCollection eventReceivers; if (target == TargetEnum.List) { SPList list = Utilities.GetListFromViewUrl(web, url); if (list == null) { throw new Exception("List not found."); } eventReceivers = list.EventReceivers; } else if (target == TargetEnum.Site) eventReceivers = web.EventReceivers; else { try { contentType = web.AvailableContentTypes[contentTypeName]; } catch (ArgumentException) { } if (contentType == null) throw new SPSyntaxException("The specified content type could not be found."); eventReceivers = contentType.EventReceivers; } SPEventReceiverDefinition def = Add(eventReceivers, type, assembly, className, name); if (sequence >= 0) { def.SequenceNumber = sequence; def.Update(); } if (contentType != null) { try { contentType.Update((contentType.ParentList == null)); } catch (Exception ex) { Exception ex1 = new Exception("An error occured updating the content type. Most likely the content type was updated but changes may not have been pushed down to any children.", ex); Logger.WriteException(new ErrorRecord(ex1, null, ErrorCategory.NotSpecified, contentType)); } } } }
/// <summary> /// Log the event to the specified list /// </summary> /// <param name="web"></param> /// <param name="listName"></param> /// <param name="eventType"></param> /// <param name="details"></param> public static void LogEvent(SPWeb web, string listName, SPEventReceiverType eventType, string details) { SPList logList = Common.EnsureLogList(web, listName); SPListItem logItem = logList.Items.Add(); logItem["Title"] = string.Format("{0} triggered at {1}", eventType, DateTime.Now); logItem["Event"] = eventType.ToString(); logItem["Before"] = Common.IsBeforeEvent(eventType); logItem["Date"] = DateTime.Now; logItem["Details"] = details; logItem.Update(); }
//Invoke Added/Updated/Deleted receivers private void InvokeEdReceiver(SPItemEventProperties properties, SPEventReceiverType eventReceiverType, string methodName) { try { var receiverProps = GetEventReceiverType(properties, eventReceiverType); var receiver = Activator.CreateInstance(receiverProps.EventReceiverType); SetParentWeb(receiver, properties.Web); var method = receiverProps.EventReceiverType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public); var receiverParam = method.GetParameters().First(); var eventDisabled = GetEventFiringDisabled(method); bool origDisabledValue = false; try { if (eventDisabled) { origDisabledValue = base.EventFiringEnabled; base.EventFiringEnabled = false; } switch (eventReceiverType) { case SPEventReceiverType.ItemDeleted: method.Invoke(receiver, new[] { (object)properties.ListItemId }); break; default: var entityType = EntityMapper.ToEntity(receiverParam.ParameterType, properties.ListItem); method.Invoke(receiver, new[] { entityType }); break; } } finally { if (eventDisabled) { base.EventFiringEnabled = origDisabledValue; } } ProccessCancel(receiver, properties); } catch (TargetInvocationException tex) { throw tex.InnerException; } }
/// <summary> /// Removes the event receiver. /// </summary> /// <param name="spList">The sp list.</param> /// <param name="assembly">The assembly.</param> /// <param name="className">Name of the class.</param> /// <param name="spEventReceiverType">Type of the sp event receiver.</param> /// <param name="eventReceiverElement">The event receiver element.</param> private void RemoveEventReceiver(SPList spList, string assembly, string className, SPEventReceiverType spEventReceiverType, ref XElement eventReceiverElement) { var eventElement = new XElement("EventReceiver", new XAttribute("Type", spEventReceiverType.ToString()), new XAttribute("Assembly", assembly), new XAttribute("Class", className), new XAttribute("SiteId", spList.ParentWeb.Site.ID), new XAttribute("WebId", spList.ParentWeb.ID), new XAttribute("ListId", spList.ID), new XAttribute("Status", 0)); bool found = false; var events = new List <SPEventReceiverDefinition>(); foreach (SPEventReceiverDefinition spEventReceiverDefinition in spList.EventReceivers.Cast <SPEventReceiverDefinition>() .Where(spEventReceiverDefinition => spEventReceiverDefinition.Assembly.Equals(assembly) && spEventReceiverDefinition.Class.Equals(className) && spEventReceiverDefinition.Type == spEventReceiverType)) { found = true; events.Add(spEventReceiverDefinition); } if (found) { foreach (SPEventReceiverDefinition spEventReceiverDefinition in events) { spEventReceiverDefinition.Delete(); } spList.Update(); eventElement.SetValue("Event receiver successfully uninstalled."); } else { // ReSharper disable PossibleNullReferenceException eventElement.Attribute("Status").SetValue(1); // ReSharper restore PossibleNullReferenceException eventElement.SetValue("Operation not performed. Event receiver was not found."); } eventReceiverElement.Add(new XElement("Data", eventElement)); }
private static void AddReceiverToList(SPList list, string rName, SPEventReceiverType rType, int seq) { if (!list.EventReceivers.Cast <SPEventReceiverDefinition>().Any(r => r.Name == rName)) { SPEventReceiverDefinition _def = list.EventReceivers.Add(); _def.Assembly = Assembly.GetExecutingAssembly().FullName; _def.Class = "Roster.Presentation.EventReceivers.SyncDataReceiver"; _def.Name = rName; _def.Type = rType; _def.SequenceNumber = seq; _def.Synchronization = SPEventReceiverSynchronization.Default; _def.Update(); } }
// 取消对以下方法的注释,以便处理激活某个功能后引发的事件。 public override void FeatureActivated(SPFeatureReceiverProperties properties) { SPWeb oWeb = (SPWeb)properties.Feature.Parent; SPEventReceiverType _eventType = SPEventReceiverType.ItemUpdated; foreach (SPList myList in oWeb.Lists) { if (myList.Title.ToLower().Contains("task") || myList.Title.Contains("任务")) { oWeb.Lists[myList.Title].EventReceivers.Add(_eventType, Assembly.GetExecutingAssembly().FullName, "TaskEventHandler.TaskEventReceiver"); oWeb.Lists[myList.Title].Update(); } } }
/// <summary> /// ��ȡ�¼����������� /// </summary> /// <param name="list"></param> /// <param name="t"></param> /// <param name="eventType"></param> /// <returns></returns> public static SPEventReceiverDefinition GetEventDefinition(SPList list, Type t, SPEventReceiverType eventType) { string assambly = t.Assembly.FullName; string className = t.FullName; for (int i = list.EventReceivers.Count - 1; i >= 0; i--) { SPEventReceiverDefinition def = list.EventReceivers[i]; if (def.Class == className && def.Type == eventType) return def; } return null; }
/// <summary> /// Adds the event handler to the content type. /// </summary> /// <param name="contentType">The content type.</param> /// <param name="name">The name of the event.</param> /// <param name="receiver">The type reference of the event receiver class.</param> /// <param name="type">The event receiver type.</param> /// <param name="sequence">The sequence.</param> /// <param name="sync">The synchronization type.</param> public void AddEventHandler( SPContentType contentType, string name, Type receiver, SPEventReceiverType type, int sequence, SPEventReceiverSynchronization sync) { // Guard if (receiver == null) { throw new ArgumentNullException("receiver"); } AddEventHandler(contentType, name, receiver.Assembly.FullName, receiver.FullName, type, sequence, sync); }
public static void Register(this SPEventReceiverDefinitionCollection collection, string name, Type receiverType, SPEventReceiverType actionsToHandle, SPEventReceiverSynchronization synchronization = SPEventReceiverSynchronization.Synchronous, int sequenceNumber = 11000) { SPEventReceiverDefinition receiverDefinition = collection.Cast<SPEventReceiverDefinition>() .SingleOrDefault(receiver => string.Equals(receiver.Name, name)); if (receiverDefinition == null) { receiverDefinition = collection.Add(); receiverDefinition.Name = name; receiverDefinition.Synchronization = synchronization; receiverDefinition.Type = actionsToHandle; receiverDefinition.SequenceNumber = sequenceNumber; receiverDefinition.Assembly = receiverType.Assembly.ToString(); receiverDefinition.Class = receiverType.FullName; receiverDefinition.Update(); } }
public virtual SPEventReceiverDefinition RegisterEventReceiver(SPEventReceiverDefinitionCollection collection, string eventReceiverName, string assembly, string className, SPEventReceiverType type, SPEventReceiverSynchronization sync, int? sequenceNumber) { var result = collection.Add(); result.Name = eventReceiverName; result.Assembly = assembly; result.Class = className; result.Type = type; result.Synchronization = sync; if (sequenceNumber.HasValue) result.SequenceNumber = sequenceNumber.Value; result.Update(); return result; }
/// <summary> /// Register even receiver for content Type /// </summary> /// <param name="contentType">Content type need to register</param> /// <param name="eventReceiverType">Type of Content Type</param> /// <param name="receiverClassType">Type of class event receiver</param> public static void RegisterEventReceiver(SPContentType contentType, SPEventReceiverType eventReceiverType, Type receiverClassType) { if (contentType == null) { return; } try { // unRegister Event Receiver first UnRegisterEventReceiver(contentType, eventReceiverType); contentType.EventReceivers.Add(eventReceiverType, receiverClassType.Assembly.FullName, receiverClassType.FullName); contentType.Update(true); } catch (Exception ex) { ULSLogging.LogError(ex); } }
/// <summary> /// Gets a specific event handler. /// </summary> /// <param name="sperdcol">The event receiver definitions.</param> /// <param name="className">The event receiver class name.</param> /// <param name="type">The event type.</param> /// <returns>The existing event handler or <c>null</c> if the event handler was not found.</returns> private static SPEventReceiverDefinition GetEventHandler( SPEventReceiverDefinitionCollection sperdcol, string className, SPEventReceiverType type) { if (sperdcol == null) { throw new ArgumentNullException("sperdcol"); } for (int i = 0; i < sperdcol.Count; i++) { SPEventReceiverDefinition definition = sperdcol[i]; if ((definition.Type == type) && (definition.Class == className)) { return definition; } } return null; }
/// <summary> /// Runs the specified command. /// </summary> /// <param name="command">The command.</param> /// <param name="keyValues">The key values.</param> /// <param name="output">The output.</param> /// <returns></returns> public override int Execute(string command, StringDictionary keyValues, out string output) { output = string.Empty; string url = Params["url"].Value.TrimEnd('/'); Common.Lists.AddEventReceiver.TargetEnum target = (Common.Lists.AddEventReceiver.TargetEnum)Enum.Parse(typeof(Common.Lists.AddEventReceiver.TargetEnum), Params["target"].Value, true); SPEventReceiverType type = (SPEventReceiverType)Enum.Parse(typeof(SPEventReceiverType), Params["type"].Value, true); string assembly = Params["assembly"].Value; string className = Params["class"].Value; string contentTypeName = Params["contenttype"].Value; string name = Params["name"].Value; int sequence = -1; if (Params["sequence"].UserTypedIn) { sequence = int.Parse(Params["sequence"].Value); } Common.Lists.AddEventReceiver.Add(url, contentTypeName, target, assembly, className, type, sequence, name); return((int)ErrorCodes.NoError); }
public static void Register(SPContentType ct, SPEventReceiverType eventType, SPEventReceiverSynchronization synchronization, int sequenceNumber, string className, bool updateChildren) { if (!IsRegistered(ct, eventType, className)) { ct.ParentWeb.AllowUnsafeUpdates = true; var assemblyName = typeof(IronItemEventReceiver).Assembly.FullName; var typeName = typeof(IronItemEventReceiver).FullName; var receiver = ct.EventReceivers.Add(); receiver.Type = eventType; receiver.Assembly = assemblyName; receiver.Synchronization = synchronization; receiver.Class = typeName; receiver.Name = className; receiver.Data = className; receiver.SequenceNumber = sequenceNumber; receiver.Update(); ct.Update(updateChildren); ct.ParentWeb.AllowUnsafeUpdates = false; } }
/// <summary> /// UnRegister Event Receiver /// </summary> /// <param name="contentType">Content Type need to unregister</param> /// <param name="eventReceiverType">Receiver Type</param> public static void UnRegisterEventReceiver(SPContentType contentType, SPEventReceiverType eventReceiverType) { if (contentType == null) { return; } try { for (var i = 0; i < contentType.EventReceivers.Count; i++) { var receiver = contentType.EventReceivers[i]; if (receiver.Type.Equals(eventReceiverType)) { receiver.Delete(); break; } } } catch (Exception ex) { ULSLogging.LogError(ex); } }
// Uncomment the method below to handle the event raised after a feature has been activated. public override void FeatureActivated(SPFeatureReceiverProperties properties) { using (SPSite site = properties.Feature.Parent as SPSite) { using (SPWeb web = site.RootWeb) { SPList webRegistry = web.Lists.TryGetList(Constants.WebRegistry.ListTitle); if (webRegistry != null) { const SPEventReceiverType _eventType = SPEventReceiverType.ItemAdding; web.Lists[Constants.WebRegistry.ListTitle].EventReceivers.Add(_eventType, Assembly.GetExecutingAssembly().FullName, "Devyatkin.TracingCreationSites.EventRecivers.WebRegistryEventReceiver.WebRegistryEventReceiver"); const SPEventReceiverType _eventType2 = SPEventReceiverType.ItemDeleting; web.Lists[Constants.WebRegistry.ListTitle].EventReceivers.Add(_eventType2, Assembly.GetExecutingAssembly().FullName, "Devyatkin.TracingCreationSites.EventRecivers.WebRegistryEventReceiver.WebRegistryEventReceiver"); const SPEventReceiverType _eventType3 = SPEventReceiverType.ItemUpdating; web.Lists[Constants.WebRegistry.ListTitle].EventReceivers.Add(_eventType3, Assembly.GetExecutingAssembly().FullName, "Devyatkin.TracingCreationSites.EventRecivers.WebRegistryEventReceiver.WebRegistryEventReceiver"); } else { //throw new Exception("WebRegistryList Error"); Logger.WriteLog(Logger.Category.Unexpected, "Devyatkin.TracingCreationSites.Features.Devyatkin.TracingCreationSites_Feature", "Error in feature activating"); } } } }
/// <summary> /// Adds the event handler to the content type. /// </summary> /// <param name="contentType">The content type.</param> /// <param name="name">The name of the event.</param> /// <param name="receiver">The type reference of the event receiver class.</param> /// <param name="type">The event receiver type.</param> /// <param name="sequence">The sequence.</param> /// <param name="sync">The synchronization type.</param> public static void AddEventHandler( SPContentType contentType, string name, Type receiver, SPEventReceiverType type, int sequence, SPEventReceiverSynchronization sync) { // Guard if (receiver == null) { throw new ArgumentNullException("receiver"); } AddEventHandler(contentType, name, receiver.Assembly.FullName, receiver.FullName, type, sequence, sync); }
private SPEventReceiverDefinition AddEventReceiverDefinition(SPContentType contentType, SPEventReceiverType type, Assembly assembly, string className, SPEventReceiverSynchronization syncType) { SPEventReceiverDefinition eventReceiverDefinition = null; var isAlreadyDefined = contentType.EventReceivers.Cast<SPEventReceiverDefinition>() .Any(x => (x.Class == className) && (x.Type == type)); // If definition isn't already defined, add it to the content type if (!isAlreadyDefined) { eventReceiverDefinition = contentType.EventReceivers.Add(); eventReceiverDefinition.Type = type; eventReceiverDefinition.Assembly = assembly.FullName; eventReceiverDefinition.Synchronization = syncType; eventReceiverDefinition.Class = className; eventReceiverDefinition.Update(); contentType.Update(true); } return eventReceiverDefinition; }
public virtual void UpdateEventReceiver(SPEventReceiverDefinition eventReceiver, string eventReceiverName, string assembly, string className, SPEventReceiverType type, SPEventReceiverSynchronization sync, int? sequenceNumber) { eventReceiver.Name = eventReceiverName; eventReceiver.Assembly = assembly; eventReceiver.Class = className; eventReceiver.Synchronization = sync; if (sequenceNumber.HasValue) eventReceiver.SequenceNumber = sequenceNumber.Value; eventReceiver.Update(); }
static void AddEventReceiver(SPContentType contentType, SPEventReceiverType type, Type target, int sequenceNumber) { // Remove event receiver it is already attached RemoveEventReceiver(contentType, type, target); // Add event receiver var eventReceiver = contentType.EventReceivers.Add(); eventReceiver.Type = type; eventReceiver.Assembly = target.Assembly.FullName; eventReceiver.Class = target.FullName; eventReceiver.SequenceNumber = sequenceNumber; eventReceiver.Update(); contentType.Update(true, false); }
/// <summary> /// Event Receiver Info (List) /// </summary> /// <param name="list">The list</param> /// <param name="type">The event receiver type</param> public EventReceiverInfo(ListInfo list, SPEventReceiverType type) : this(list, type, SPEventReceiverSynchronization.Default) { }
/// <summary> /// Event Receiver Info (Content Type) /// </summary> /// <param name="contentType">The content type</param> /// <param name="type">The event receiver type</param> public EventReceiverInfo(ContentTypeInfo contentType, SPEventReceiverType type) : this(contentType, type, SPEventReceiverSynchronization.Default) { }
public static void AddEventReceiverToContentType(string className, SPContentType contentType, string assemblyName, SPEventReceiverType eventReceiverType, SPEventReceiverSynchronization eventReceiverSynchronization) { className.RequireNotNullOrEmpty("className"); contentType.RequireNotNull("contentType"); assemblyName.RequireNotNullOrEmpty("assemblyName"); eventReceiverType.RequireNotNull("eventReceiverType"); eventReceiverSynchronization.RequireNotNull("eventReceiverSynchronization"); SPEventReceiverDefinition eventReceiver = contentType.EventReceivers.Add(); eventReceiver.Synchronization = eventReceiverSynchronization; eventReceiver.Type = eventReceiverType; eventReceiver.Assembly = assemblyName; eventReceiver.Class = className; eventReceiver.Update(); }
private SPEventReceiverDefinition AddEventReceiverDefinition(SPList list, SPEventReceiverType type, string assemblyName, string className, SPEventReceiverSynchronization syncType, int sequenceNumber) { SPEventReceiverDefinition eventReceiverDefinition = null; // Try Parse the Assembly Name var classType = Type.GetType(string.Format(CultureInfo.InvariantCulture, "{0}, {1}", className, assemblyName)); if (classType != null) { var assembly = Assembly.GetAssembly(classType); var isAlreadyDefined = list.EventReceivers.Cast<SPEventReceiverDefinition>().Any(x => (x.Class == className) && (x.Type == type)); // If definition isn't already defined, add it to the list if (!isAlreadyDefined) { eventReceiverDefinition = list.EventReceivers.Add(); eventReceiverDefinition.Type = type; eventReceiverDefinition.Assembly = assembly.FullName; eventReceiverDefinition.Synchronization = syncType; eventReceiverDefinition.Class = className; eventReceiverDefinition.SequenceNumber = sequenceNumber; eventReceiverDefinition.Update(); list.Update(); } } return eventReceiverDefinition; }
/// <summary> /// Adds an event receiver to a the specified event receiver definition collection. /// </summary> /// <param name="eventReceivers">The event receivers.</param> /// <param name="eventReceiverType">Type of the event receiver.</param> /// <param name="assembly">The assembly.</param> /// <param name="className">Name of the class.</param> /// <param name="name">The name.</param> /// <returns></returns> public static SPEventReceiverDefinition Add(SPEventReceiverDefinitionCollection eventReceivers, SPEventReceiverType eventReceiverType, string assembly, string className, string name) { SPEventReceiverDefinition def = GetEventReceiver(eventReceivers, eventReceiverType, assembly, className); if (def == null) { eventReceivers.Add(eventReceiverType, assembly, className); def = GetEventReceiver(eventReceivers, eventReceiverType, assembly, className); if (def != null && !String.IsNullOrEmpty(name)) { def.Name = name; def.Update(); } return def; } return def; }
/// <summary> /// Adds the event handler to the content type. /// </summary> /// <param name="contentType">The content type.</param> /// <param name="name">The name of the event.</param> /// <param name="assembly">The assembly containing the event receiver.</param> /// <param name="className">Name of the event receiver class.</param> /// <param name="type">The event receiver type.</param> /// <param name="sequence">The sequence.</param> /// <param name="sync">The synchronization type.</param> public static void AddEventHandler( SPContentType contentType, string name, string assembly, string className, SPEventReceiverType type, int sequence, SPEventReceiverSynchronization sync) { // Guard if (contentType == null) { throw new ArgumentNullException("contentType"); } SPEventReceiverDefinition definition = GetEventHandler(contentType.EventReceivers, name, type); if (definition == null) { contentType.EventReceivers.Add(type, assembly, className); definition = GetEventHandler(contentType.EventReceivers, className, type); } definition.Name = name; definition.SequenceNumber = sequence; definition.Synchronization = sync; definition.Update(); }
/// <summary> /// Gets the event receiver. /// </summary> /// <param name="eventReceivers">The event receivers.</param> /// <param name="eventReceiverType">Type of the event receiver.</param> /// <param name="assembly">The assembly.</param> /// <param name="className">Name of the class.</param> /// <returns></returns> private static SPEventReceiverDefinition GetEventReceiver(SPEventReceiverDefinitionCollection eventReceivers, SPEventReceiverType eventReceiverType, string assembly, string className) { foreach (SPEventReceiverDefinition erd in eventReceivers) { if (erd.Assembly.ToLower() == assembly.ToLower() && erd.Class.ToLower() == className.ToLower() && erd.Type == eventReceiverType) { return erd; } } return null; }
/// <summary> /// Event Receiver Info (Content Type) /// </summary> /// <param name="contentType">The content type</param> /// <param name="type">The event receiver type</param> /// <param name="syncType">The synchronization type</param> public EventReceiverInfo(ContentTypeInfo contentType, SPEventReceiverType type, SPEventReceiverSynchronization syncType) : this(contentType, type, syncType, string.Empty, string.Empty) { }
private bool ValidateRule(Rule r, SPListItem item, SPEventReceiverType eventType) { if (r.RunOnAdded && eventType == SPEventReceiverType.ItemAdded) return true; if (r.RunOnAnyUpdate == true && eventType == SPEventReceiverType.ItemUpdated && item.Level != SPFileLevel.Checkout) return true; if (r.RunOnFirstUpdate == true && eventType == SPEventReceiverType.ItemUpdated && item.Level != SPFileLevel.Checkout) { if (item[SPBuiltInFieldId.owshiddenversion].ToString() == "2") { return true; } } return false; }
/// <summary> /// Event Receiver Info (List) /// </summary> /// <param name="list">The list</param> /// <param name="type">The event receiver type</param> /// <param name="syncType">The synchronization type</param> public EventReceiverInfo(ListInfo list, SPEventReceiverType type, SPEventReceiverSynchronization syncType) : this(list, type, syncType, string.Empty, string.Empty) { }
static void RemoveEventReceiver(SPContentType contentType, SPEventReceiverType type, Type target) { foreach (SPEventReceiverDefinition definition in contentType.EventReceivers) { if (definition.Class == target.FullName && definition.Assembly == target.Assembly.FullName && definition.Type == type) { definition.Delete(); contentType.Update(true, false); break; } } }