0

我有一个带有 itemadd 事件接收器的文档库。文档库中的项目是文档集。

当我在文档库中创建一个项目时,添加的项目被执行。但是,这种逻辑,我只想为文档集本身执行它,而不是在我将文件添加到文档集时执行它。

创建文档集时,以下代码可以正常工作,但是该行:

string recurrent = item[Meetings.Common.Constants.FIELDS_AGENDAPOINTSRECURRENT_NAME].ToString();

文件上传到文档集时执行事件接收器时抛出对象引用异常

public override void ItemAdded(SPItemEventProperties properties)
        {
            Logger.LogDebug("AgendaPointsProposedEvents", "ItemAdded(SPItemEventProperties properties)", "BEGIN");
            base.ItemAdded(properties);
            try
            {
                base.EventFiringEnabled = false;

                SPListItem item = properties.ListItem;
                SPItemEventDataCollection afterProperties = properties.AfterProperties;
                string listName = item.ParentList.RootFolder.Name;

                string recurrent = item[Meetings.Common.Constants.FIELDS_AGENDAPOINTSRECURRENT_NAME].ToString();
                bool recurrentBln = bool.Parse(recurrent);
                if (recurrentBln)
                {
                    #region NLNSSV-169
                    // If the user selects recurrent the agenda proposed item cant have an associated meeting.
                    // Also the security is reset to its initial state.
                    properties.ListItem[Meetings.Common.Constants.FIELDS_MEETING_NAME] = null;
                    properties.List.ResetRoleInheritance();
                    properties.ListItem.Update();
                    #endregion
                }
                else
                {
                    #region NLNSSV-184
                    // NLNSSV-184
                    // Proposed meeting points should be visible to other departments as soon as they are added to a meeting. 
                    // An event handler should change the authentication on the item level when a meeting point is added to (or removed from!) the meeting.
                    // This also needs a change in the views and screens..
                    SPFieldLookup sp = item.GetLookupField(MeetingsCommon.Constants.FIELDS_MEETING_NAME);
                    string meetingname = afterProperties.GetValueAsString("Meeting");
                    if (!string.IsNullOrEmpty(meetingname))
                    {
                        RoleDefRead = SecurityHelper.GetRoleDefinition(properties.Web, Constants.PERMISSIONLEVEL_READ);
                        DepartmentCORUsersGroup = properties.Web.SiteGroups[MeetingsCommon.Constants.SECURITY_PRINCIPAL_DEPARTMENTCORUSERS_NAME];
                        DepartmentFINUsersGroup = properties.Web.SiteGroups[MeetingsCommon.Constants.SECURITY_PRINCIPAL_DEPARTMENTFINUSERS_NAME];
                        DepartmentMSCUsersGroup = properties.Web.SiteGroups[MeetingsCommon.Constants.SECURITY_PRINCIPAL_DEPARTMENTMSCUSERS_NAME];
                        DepartmentOPIUsersGroup = properties.Web.SiteGroups[MeetingsCommon.Constants.SECURITY_PRINCIPAL_DEPARTMENTOPIUSERS_NAME];
                        DepartmentRDSUsersGroup = properties.Web.SiteGroups[MeetingsCommon.Constants.SECURITY_PRINCIPAL_DEPARTMENTRDSUSERS_NAME];
                        DepartmentSALUsersGroup = properties.Web.SiteGroups[MeetingsCommon.Constants.SECURITY_PRINCIPAL_DEPARTMENTSALUSERS_NAME];

                        bool isProposed = item.TaxonomyFieldValueIsGivenValue(properties.AfterProperties, MeetingsCommon.Constants.FIELDS_AGENDAPOINTSTATUS_NAME, MeetingsCommon.Constants.TERMVALUE_AGENDAPOINTSTATUS_PROPOSED, 1033);
                        if (isProposed)
                        {

                            #region Change permissions in COR items
                            if (listName.Equals(MeetingsCommon.Constants.LISTS_AGENDAPOINTSPROPOSEDCOR_NAME))
                            {
                                item.BreakRoleInheritance(true);
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentFINUsersGroup, false); // Read
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentMSCUsersGroup, false); // Read
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentOPIUsersGroup, false); // Read
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentRDSUsersGroup, false); // Read
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentSALUsersGroup, false); // Read   
                            }
                            #endregion

                            #region Change permissions in FIN items
                            if (listName.Equals(MeetingsCommon.Constants.LISTS_AGENDAPOINTSPROPOSEDFIN_NAME))
                            {
                                item.BreakRoleInheritance(true);
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentCORUsersGroup, false); // Read
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentMSCUsersGroup, false); // Read
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentOPIUsersGroup, false); // Read
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentRDSUsersGroup, false); // Read
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentSALUsersGroup, false); // Read   
                            }
                            #endregion

                            #region Change permissions in MSC items
                            if (listName.Equals(MeetingsCommon.Constants.LISTS_AGENDAPOINTSPROPOSEDMSC_NAME))
                            {
                                item.BreakRoleInheritance(true);
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentCORUsersGroup, false); // Read
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentFINUsersGroup, false); // Read
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentOPIUsersGroup, false); // Read
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentRDSUsersGroup, false); // Read
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentSALUsersGroup, false); // Read   
                            }
                            #endregion

                            #region Change permissions in OPI items
                            if (listName.Equals(MeetingsCommon.Constants.LISTS_AGENDAPOINTSPROPOSEDOPI_NAME))
                            {
                                item.BreakRoleInheritance(true);
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentCORUsersGroup, false); // Read
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentFINUsersGroup, false); // Read
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentMSCUsersGroup, false); // Read
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentRDSUsersGroup, false); // Read
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentSALUsersGroup, false); // Read   
                            }
                            #endregion

                            #region Change permissions in RDS items
                            if (listName.Equals(MeetingsCommon.Constants.LISTS_AGENDAPOINTSPROPOSEDRDS_NAME))
                            {
                                item.BreakRoleInheritance(true);
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentCORUsersGroup, false); // Read
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentFINUsersGroup, false); // Read
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentMSCUsersGroup, false); // Read
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentOPIUsersGroup, false); // Read
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentSALUsersGroup, false); // Read   
                            }
                            #endregion

                            #region Change permissions in SAL items
                            if (listName.Equals(MeetingsCommon.Constants.LISTS_AGENDAPOINTSPROPOSEDSAL_NAME))
                            {
                                item.BreakRoleInheritance(true);
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentCORUsersGroup, false); // Read
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentFINUsersGroup, false); // Read
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentMSCUsersGroup, false); // Read
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentOPIUsersGroup, false); // Read
                                SecurityHelper.AddToRoleAssignment(RoleDefRead, properties.Web, item, DepartmentRDSUsersGroup, false); // Read   
                            }
                            #endregion
                        }
                    }
                    else
                    {
                        item.ResetRoleInheritance();
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                Logger.LogError("AgendaPointsProposedEvents", "ItemAdded(SPItemEventProperties properties)", ex);
                throw;
            }
            finally
            {
                base.EventFiringEnabled = true;
            }
            Logger.LogDebug("AgendaPointsProposedEvents", "ItemAdded(SPItemEventProperties properties)", "END");
        }
4

1 回答 1

0

我想到了。

这个想法是检测内容类型,因为有时列表可能具有不同的内容类型,并且您希望代码仅针对一种内容类型执行。

简单的:

 string contentTypeName = properties.ListItem.ContentType.Name;
            if (contentTypeName == "contentypename")
            {
于 2012-08-10T13:06:38.923 回答