Ejemplo n.º 1
0
    /// <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();
    }
        /// <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();
                }
            }
        }
Ejemplo n.º 3
0
 /// <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;
 }
Ejemplo n.º 4
0
 /// <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;
 }
Ejemplo n.º 5
0
        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;
            }
        }
Ejemplo n.º 6
0
 /// <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;
 }
Ejemplo n.º 7
0
 /// <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;
 }
Ejemplo n.º 8
0
 /// <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;
 }
Ejemplo n.º 9
0
    /// <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;
        }
Ejemplo n.º 12
0
        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;
            }
        }
Ejemplo n.º 13
0
        public static void EnsureReceiver(SPList list, Type receiverClass, int sequence = 10000,
                                          SPEventReceiverSynchronization sync           = SPEventReceiverSynchronization.Default,
                                          string name = null,
                                          params SPEventReceiverType[] receiverTypes)
        {
            if (receiverClass == null)
            {
                throw new ArgumentNullException(nameof(receiverClass));
            }

            if (name == null)
            {
                name = $"Event receiver of type {receiverClass.FullName}";
            }

            foreach (var receiverType in receiverTypes)
            {
                var receiverClassName = receiverClass.FullName;
                var receiverAssembly  = receiverClass.Assembly.FullName;
                var receiver          =
                    list.EventReceivers.OfType <SPEventReceiverDefinition>().FirstOrDefault(
                        x => x.Class == receiverClassName && x.Type == receiverType);

                if (receiver == null)
                {
                    var rec = list.EventReceivers.Add();
                    rec.Name            = name;
                    rec.Assembly        = receiverAssembly;
                    rec.Class           = receiverClassName;
                    rec.Synchronization = sync;
                    rec.Type            = receiverType;
                    rec.SequenceNumber  = sequence;
                    rec.Update();
                }
            }
        }
Ejemplo n.º 14
0
        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;
        }
Ejemplo n.º 15
0
 /// <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;
 }
Ejemplo n.º 16
0
 /// <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)
 {
 }
Ejemplo n.º 17
0
        private SPEventReceiverDefinition AddEventReceiverDefinition(SPContentType contentType, SPEventReceiverType type, string assemblyName, string className, SPEventReceiverSynchronization syncType)
        {
            SPEventReceiverDefinition eventReceiverDefinition = null;

            var classType = Type.GetType(string.Format(CultureInfo.InvariantCulture, "{0}, {1}", className, assemblyName));
            if (classType != null)
            {
                var assembly = Assembly.GetAssembly(classType);
                eventReceiverDefinition = this.AddEventReceiverDefinition(contentType, type, assembly, className, syncType);
            }

            return eventReceiverDefinition;
        }
Ejemplo n.º 18
0
        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 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();
        }
        /// <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);
        }
Ejemplo n.º 21
0
 /// <summary>
 /// Обеспечить наличие на списке <paramref name="list"/> EventReceiver'а <paramref name="name"/>.
 /// Этот метод позволяет программно повесить обработчик события на список.
 /// Если на момент вызова данного метода, такой обработчик на списке уже висел, то он будет повешен заново.
 /// </summary>
 /// <param name="name"></param>
 /// <param name="list"></param>
 /// <param name="typeClassReceiver"></param>
 /// <param name="typeReceiver"></param>
 /// <param name="synchronizationValue"></param>
 /// <param name="sequenceNumber"></param>
 public static void ProvisionListEventReceiver(string name, SPList list, Type typeClassReceiver, SPEventReceiverType typeReceiver, SPEventReceiverSynchronization synchronizationValue, int sequenceNumber)
 {
     DeleteListEventReceiver(list, typeClassReceiver, typeReceiver);
     AddListEventReceiver(name, list, typeClassReceiver, typeReceiver, synchronizationValue, sequenceNumber);
 }
Ejemplo n.º 22
0
        /// <summary>
        /// Добавить списку <paramref name="list"/> EventReceiver с именем <paramref name="name"/>.
        /// </summary>
        /// <param name="name">User friendly name for EventReceiver</param>
        /// <param name="list">SPList, к которому добавляется EventReceiver </param>
        /// <param name="typeClassReceiver">Тип (класс), в котором реализован метод-обработчик соответствующего EventReceiver'а</param>
        /// <param name="typeReceiver">Тип EventReceiver'а. Используйте только типы EventReceiver'ов, которые используются для СПИСКОВ (SPList)</param>
        /// <param name="synchronizationValue" >Синхронное или Асинхронное</param>
        /// <param name="sequenceNumber">Число, представляющее место данного события в последовательности событий</param>
        /// <exception cref="ArgumentNullException">Возникает если следующие параметры равны null: <paramref name="name"/>, <paramref name="list"/>, <paramref name="typeClassReceiver"/>.
        /// </exception>
        /// <exception cref="ArgumentException">Возникает если у списка уже есть Event ресивер типа <paramref name="typeReceiver"/> реализованный с помощью класса <paramref name="typeClassReceiver"/>
        /// </exception>
        public static void AddListEventReceiver(string name, SPList list, Type typeClassReceiver, SPEventReceiverType typeReceiver, SPEventReceiverSynchronization synchronizationValue, int sequenceNumber)
        {
            #region Проверка входящих параметров на null

            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            if (list == null)
            {
                throw new ArgumentNullException("list");
            }

            if (typeClassReceiver == null)
            {
                throw new ArgumentNullException("typeClassReceiver");
            }

            #endregion

            var eventReceiverAssembly = typeClassReceiver.Assembly.FullName;
            var eventReceiverClass    = typeClassReceiver.FullName;

            #region Проверяем есть ли уже в списке такой Event Reciever.

            for (var i = 0; i < list.EventReceivers.Count; i++)
            {
                var eventReceiverDefinition = list.EventReceivers[i];
                if (String.Equals(eventReceiverDefinition.Name, name))
                {
                    throw new ArgumentException("Event Receiver с таким именем уже существует.", "name");
                }

                if (eventReceiverDefinition.Assembly == eventReceiverAssembly && eventReceiverDefinition.Class == eventReceiverClass && eventReceiverDefinition.Type == typeReceiver)
                {
                    throw new ArgumentException(
                              String.Format("Такой Event Receiver уже существует. eventReceiverClass = {0} eventReceiverAssembly = {1}, typeReceiver = {2}", eventReceiverClass, eventReceiverAssembly, typeReceiver));
                }
            }

            #endregion

            // Создаём новый EventReceiver
            SPEventReceiverDefinition newEventReceiverDefinition = list.EventReceivers.Add();
            newEventReceiverDefinition.Type     = typeReceiver;
            newEventReceiverDefinition.Assembly = typeClassReceiver.Assembly.FullName;
            newEventReceiverDefinition.Class    = typeClassReceiver.FullName;
            // Задаём правильное имя EventReceiver'у
            newEventReceiverDefinition.Name = name;
            // Задаём тип синхронизации
            newEventReceiverDefinition.Synchronization = synchronizationValue;

            newEventReceiverDefinition.SequenceNumber = sequenceNumber;

            newEventReceiverDefinition.Update();
        }
Ejemplo n.º 23
0
        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();
        }
 public SPGENEventHandlerSynchronizationAttribute(SPEventReceiverSynchronization synchronization)
 {
     this.Synchronization = synchronization;
 }
Ejemplo n.º 26
0
 /// <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;
 }
Ejemplo n.º 27
0
 /// <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)
 {
 }
Ejemplo n.º 28
0
        public void RegisterItemEventHandler(ref SPList list, Type receiverClassType, SPEventReceiverType receiverType, int sequence, SPEventReceiverSynchronization sync)
        {
            SPEventReceiverDefinition def = GetEventReceiver(ref list, receiverClassType, receiverType);

            if (def == null)
            {
                def = list.EventReceivers.Add();

                def.Assembly = receiverClassType.Assembly.FullName;                    //"ERDefinition, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=704f58d28567dc00";
                def.Class    = receiverClassType.Name;                                 // "ERDefinition.ItemEvents";
                def.Name     = receiverClassType.Name + "_" + receiverType.ToString(); //ItemAdded Event";
                def.Type     = receiverType;
            }
            def.SequenceNumber  = sequence;
            def.Synchronization = sync;
            def.Update();
        }
Ejemplo n.º 29
0
        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);
        }
Ejemplo n.º 30
0
        public static void EnsureEventReceiver(this SPList list, System.Type ReceiverClass, int sequence, SPEventReceiverSynchronization synchronous, params SPEventReceiverType[] ReceiverTypes)
        {
            if (list == null)
            {
                return;
            }
            try
            {
                list.ParentWeb.AllowUnsafeUpdates = true;
                string assembly = ReceiverClass.Assembly.FullName;
                foreach (var item in ReceiverTypes)
                {
                    if (!list.EventReceivers.Cast <SPEventReceiverDefinition>().Any(P => P.Class == ReceiverClass.FullName &&
                                                                                    P.Assembly == assembly &&
                                                                                    P.Type == item))
                    {
                        var def = list.EventReceivers.Add();
                        def.Assembly        = assembly;
                        def.Class           = ReceiverClass.FullName;
                        def.Synchronization = synchronous;
                        def.SequenceNumber  = sequence;

                        def.Type = ReceiverTypes[0];
                        foreach (var type in ReceiverTypes)
                        {
                            def.Type |= type;
                        }
                        def.Update();
                    }
                }
                list.Update();
            }
            catch (Exception)
            {
            }
            finally
            {
                list.ParentWeb.AllowUnsafeUpdates = false;
            }
        }
Ejemplo n.º 31
0
 /// <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;
 }
Ejemplo n.º 32
0
        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();
            }
        }
Ejemplo n.º 33
0
 public static void Register(SPContentType ct, SPEventReceiverType eventType, SPEventReceiverSynchronization synchronization, int sequenceNumber, string className)
 {
     Register(ct, eventType, synchronization, sequenceNumber, className, true);
 }
        /// <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();
        }
Ejemplo n.º 35
0
 /// <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)
 {
 }
Ejemplo n.º 36
0
        private SPEventReceiverDefinition AddEventReceiverDefinition(SPContentType contentType, SPEventReceiverType type, string assemblyName, string className, SPEventReceiverSynchronization syncType)
        {
            SPEventReceiverDefinition eventReceiverDefinition = null;

            var classType = Type.GetType(string.Format(CultureInfo.InvariantCulture, "{0}, {1}", className, assemblyName));

            if (classType != null)
            {
                var assembly = Assembly.GetAssembly(classType);
                eventReceiverDefinition = this.AddEventReceiverDefinition(contentType, type, assembly, className, syncType);
            }

            return(eventReceiverDefinition);
        }
Ejemplo n.º 37
0
 /// <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)
 {
 }