0

I have a document library with an itemadded event receiver. Items in the document library are document sets.

when I create an item in the document library, the item added is executed. However that logic, I want to execute it only for the document set itself and not when I add a file INTO the document set.

the following code works fine when creating the document set, but the line:

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

throw object reference exception when the event receiver is executed when a file is uploaded to the document set

  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");
        }

1 Answer 1

1

You can check if the item is a document set first. Here's a method that can help curtosy of Mary Rivers

 public static bool IsDocumentSet(this SPListItem item) 
 { 
    bool documentSetItem = false; 
    DocumentSet documentSet = null; 

    if (null != item && item.IsFolder()) 
    { 
      documentSet = DocumentSet.GetDocumentSet(item.Folder);      
      if (null != documentSet) 
      documentSetItem = true; 
    } 
    return documentSetItem; 
} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.