public SPEventReceiverDefinitionInstance Add(object id, object contextList)
        {
            SPEventReceiverDefinition result;

            if (id == Undefined.Value && contextList == Undefined.Value)
            {
                result = m_eventReceiverDefinitionCollection.Add();
            }
            else if (id != Undefined.Value && contextList == Undefined.Value)
            {
                var guid = GuidInstance.ConvertFromJsObjectToGuid(id);
                result = m_eventReceiverDefinitionCollection.Add(guid);
            }
            else
            {
                var guid = GuidInstance.ConvertFromJsObjectToGuid(id);

                var listContextList = contextList as SPListInstance;
                if (listContextList == null)
                {
                    throw new JavaScriptException(this.Engine, "Error", "Context list must be specified and an instance of SPList.");
                }

                result = m_eventReceiverDefinitionCollection.Add(guid, listContextList.List);
            }

            return(result == null
                ? null
                : new SPEventReceiverDefinitionInstance(this.Engine.Object.InstancePrototype, result));
        }
Пример #2
0
        /// <summary>
        /// Ensures the taxonomy event receivers.
        /// </summary>
        /// <param name="eventReceivers">The event receivers definition collection.</param>
        /// <exception cref="System.ArgumentNullException">All null parameters.</exception>
        public void EnsureTaxonomyEventReceivers(SPEventReceiverDefinitionCollection eventReceivers)
        {
            if (eventReceivers == null)
            {
                throw new ArgumentNullException("eventReceivers");
            }

            // Check if the ItemAdding exists in the collection.
            bool hasItemAdding = this.eventReceiverHelper.EventReceiverDefinitionExist(eventReceivers, SPEventReceiverType.ItemAdding, AssemblyFullName, ClassFullName);

            if (!hasItemAdding)
            {
                // Add the ItemAdding event receiver.
                eventReceivers.Add(SPEventReceiverType.ItemAdding, AssemblyFullName, ClassFullName);
            }

            // Check if the ItemUpdating exists in the collection.
            bool hasItemUpdating = this.eventReceiverHelper.EventReceiverDefinitionExist(eventReceivers, SPEventReceiverType.ItemUpdating, AssemblyFullName, ClassFullName);

            if (!hasItemUpdating)
            {
                // Add the ItemUpdating event receiver.
                eventReceivers.Add(SPEventReceiverType.ItemUpdating, AssemblyFullName, ClassFullName);
            }
        }
Пример #3
0
        protected SPEventReceiverDefinition CreateNewEventReceiverDefinition(
            object modelHost,
            SPEventReceiverDefinitionCollection eventReceivers,
            out bool isNew)
        {
            var result = eventReceivers.Add();

            isNew = true;

            if (modelHost is WebModelHost)
            {
                result.HostType = SPEventHostType.Web;
            }
            else if (modelHost is ListModelHost)
            {
                result.HostType = SPEventHostType.List;
            }
            else if (modelHost is SiteModelHost)
            {
                result.HostType = SPEventHostType.Site;
            }
            else if (modelHost is SPContentType)
            {
                result.HostType = SPEventHostType.ContentType;
            }
            else
            {
                throw new SPMeta2UnsupportedModelHostException("model host should be ListModelHost/WebModelHost/SiteModelHost");
            }

            return(result);
        }
Пример #4
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();
            }
        }
        /// <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);
        }
        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;
        }
        protected SPEventReceiverDefinition CreateNewEventReceiverDefinition(
            object modelHost,
            SPEventReceiverDefinitionCollection eventReceivers,
            out bool isNew)
        {
            var result = eventReceivers.Add();

            isNew = true;

            if (modelHost is WebModelHost)
                result.HostType = SPEventHostType.Web;
            else if (modelHost is ListModelHost)
                result.HostType = SPEventHostType.List;
            else if (modelHost is SiteModelHost)
                result.HostType = SPEventHostType.Site;
            else if (modelHost is SPContentType)
                result.HostType = SPEventHostType.ContentType;
            else
            {
                throw new SPMeta2UnsupportedModelHostException("model host should be ListModelHost/WebModelHost/SiteModelHost");
            }

            return result;
        }
Пример #8
0
        /// <summary>
        /// Ensures the taxonomy event receivers.
        /// </summary>
        /// <param name="eventReceivers">The event receivers definition collection.</param>
        /// <exception cref="System.ArgumentNullException">All null parameters.</exception>
        public void EnsureTaxonomyEventReceivers(SPEventReceiverDefinitionCollection eventReceivers)
        {
            if (eventReceivers == null)
            {
                throw new ArgumentNullException("eventReceivers");
            }

            // Check if the ItemAdding exists in the collection.
            bool hasItemAdding = this.eventReceiverHelper.EventReceiverDefinitionExist(eventReceivers, SPEventReceiverType.ItemAdding, AssemblyFullName, ClassFullName);
            if (!hasItemAdding)
            {
                // Add the ItemAdding event receiver.
                eventReceivers.Add(SPEventReceiverType.ItemAdding, AssemblyFullName, ClassFullName);
            }

            // Check if the ItemUpdating exists in the collection.
            bool hasItemUpdating = this.eventReceiverHelper.EventReceiverDefinitionExist(eventReceivers, SPEventReceiverType.ItemUpdating, AssemblyFullName, ClassFullName);
            if (!hasItemUpdating)
            {
                // Add the ItemUpdating event receiver.
                eventReceivers.Add(SPEventReceiverType.ItemUpdating, AssemblyFullName, ClassFullName);
            }
        }
 /// <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;
 }
Пример #10
0
        public static void ProcessEventHookAction(Form parent, ISecurableObject obj, string typeName, Assembly selectedAssembly, Type selectedType, bool useNextSequenceNumber)
        {
            Type selectedObjectType = obj.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)obj;

                parentName  = web.Name;
                definitions = web.EventReceivers;
            }
            else if (typeof(SPList).IsAssignableFrom(selectedObjectType))
            {
                SPList list = (SPList)obj;

                parentName  = list.Title;
                definitions = list.EventReceivers;
            }
            else
            {
                throw new NotSupportedException("The selected object type is not supported.");
            }

            if (definitions != null)
            {
                bool cancel = false;

                List <SPEventReceiverDefinition> pendingDelete = new List <SPEventReceiverDefinition>();

                foreach (SPEventReceiverDefinition definition in definitions)
                {
                    if ((definition.Type == eventType) && string.Equals(definition.Assembly, selectedAssembly.FullName) && string.Equals(definition.Class, selectedType.FullName))
                    {
                        if (MessageBox.Show(parent, string.Format("The selected event receiver has already been hooked to the [{0}] objects [{1}] event. Do you wish to overwrite the existing event hook?", parentName, eventType), "Event Already Hooked", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                        {
                            pendingDelete.Add(definition);
                        }
                        else
                        {
                            cancel = true;
                            break;
                        }
                    }

                    if (definition.SequenceNumber > maxSequence)
                    {
                        maxSequence = definition.SequenceNumber;
                    }
                }

                foreach (SPEventReceiverDefinition definition in pendingDelete)
                {
                    definition.Delete();
                }

                if (cancel)
                {
                    return;
                }

                definitions.Add(eventType, selectedAssembly.FullName, selectedType.FullName);

                if (useNextSequenceNumber)
                {
                    foreach (SPEventReceiverDefinition definition in definitions)
                    {
                        if ((definition.Type == eventType) && string.Equals(definition.Assembly, selectedAssembly.FullName) && string.Equals(definition.Class, selectedType.FullName))
                        {
                            definition.SequenceNumber = (maxSequence + 1);
                            definition.Update();
                            break;
                        }
                    }
                }
            }
        }