Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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");
        }
share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

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; 
} 
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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