2012-08-09 50 views
0

我有一個帶有itemadded事件接收器的文檔庫。文檔庫中的項目是文檔集。ItemAdded如何檢測添加的項目是文檔集本身還是文檔到文檔庫

當我在文檔庫中創建一個項目時,添加的項目被執行。然而,這個邏輯,我只想爲文檔集本身執行它,而不是當我將文件添加到文檔集時。創建文檔時設置的,但行

下面的代碼工作正常:

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

回答

0

我想通了。

這個想法是檢測內容類型,因爲有時列表可能會有不同的內容類型,並且您希望代碼僅對一種內容類型執行。

容易:

string contentTypeName = properties.ListItem.ContentType.Name; 
      if (contentTypeName == "contentypename") 
      { 
相關問題