/// <summary> /// An item is being updated. /// </summary> public override void ItemUpdating(SPItemEventProperties properties) { SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = new SPSite(properties.SiteId)) { using (SPWeb web = site.OpenWeb(properties.Web.ID)) { SPListItem item = web.GetListItem(properties.ListItem.GetServerRelativeUrl()); SPGroup group; string groupName; string groupSuffix = properties.List.RootFolder.Name; if (TryGetGroup(groupSuffix, web, out groupName, out group)) { // Get the old user as an SPUser SPUser oldUser = item.GetFieldAsSPUser(eCaseConstants.FieldGuids.GROUPPROXY_LIST_GROUPMEMBER); if (oldUser != null) { if (group.Owner != oldUser) { SPUser newUser = properties.AfterProperties.GetFieldAsSPUser("GroupMember", web); if (newUser != null) { group.RemoveUser(oldUser); group.AddUser(newUser); group.Update(); } else { properties.ErrorMessage = string.Format("User {0} cannot be found.", properties.AfterProperties["GroupMember"]); properties.Cancel = true; properties.Status = SPEventReceiverStatus.CancelWithError; } } else { properties.ErrorMessage = string.Format("User {0} is the group owner and cannot be removed.", item[eCaseConstants.FieldGuids.GROUPPROXY_LIST_GROUPMEMBER]); properties.Cancel = true; properties.Status = SPEventReceiverStatus.CancelWithError; } } else { properties.ErrorMessage = string.Format("User {0} cannot be found.", properties.BeforeProperties["GroupMember"]); properties.Cancel = true; properties.Status = SPEventReceiverStatus.CancelWithError; } } else { properties.ErrorMessage = string.Format("No group with a name of {0} can be found.", groupName); properties.Cancel = true; properties.Status = SPEventReceiverStatus.CancelWithError; } } } }); }
private void GetAssignedToAndSupervisor(SPListItem item, SPItemEventDataCollection afterProperties, SPWeb caseWeb, ref SPUser assignedToUser, ref SPUser prevAssignedToUser, ref SPUser supervisorUser, ref SPUser prevSupervisorUser) { // Check if we the AssignedTo or AssignedToSupervisor was modified if (!string.IsNullOrEmpty(afterProperties["AssignedTo"].ToString())) { assignedToUser = afterProperties.GetFieldAsSPUser("AssignedTo", caseWeb); prevAssignedToUser = item.GetFieldAsSPUser(eCaseConstants.FieldGuids.OOTB_ASSIGNEDTO); } //if (!string.IsNullOrEmpty(afterProperties["AssignedToSupervisor"].ToString())) //{ // supervisorUser = afterProperties.GetFieldAsSPUser("AssignedToSupervisor", caseWeb); // prevSupervisorUser = item.GetFieldAsSPUser(eCaseConstants.FieldGuids.ECASES_LIST_ASSIGNEDTOSUPERVISOR); //} }
private void GetAssignedToAndSupervisor(SPListItem item, out SPUser assignedTo, out SPUser supervisor) { supervisor = item.GetFieldAsSPUser(eCaseConstants.FieldGuids.ECASES_LIST_ASSIGNEDTOSUPERVISOR); assignedTo = item.GetFieldAsSPUser(eCaseConstants.FieldGuids.OOTB_ASSIGNEDTO); }
public override void ItemUpdated(SPItemEventProperties properties) { try { SPListItem item = properties.ListItem; SPUser assignedTo = item.GetFieldAsSPUser(eCaseConstants.FieldGuids.OOTB_ASSIGNEDTO); SPUser assignedToSupervisor = item.GetFieldAsSPUser(eCaseConstants.FieldGuids.ECASES_LIST_ASSIGNEDTOSUPERVISOR); string caseTitle = item[eCaseConstants.FieldGuids.OOTB_TITLE] as string; string caseWebUrl = item[eCaseConstants.FieldGuids.ECASES_LIST_CASEURL].ToString(); caseWebUrl = caseWebUrl.Split(',')[0]; SPSite site; SPWeb caseWeb; bool titleUpdated = false; using (site = new SPSite(caseWebUrl)) { using (caseWeb = site.OpenWeb()) { if (caseWeb.DoesUserHavePermissions(SPBasePermissions.ManageWeb)) { UpdateCaseWebTitle(caseWeb, caseTitle); titleUpdated = true; } } } if (titleUpdated == false) { SPSecurity.RunWithElevatedPrivileges(delegate() { using (site = new SPSite(caseWebUrl)) { using (caseWeb = site.OpenWeb()) { if (titleUpdated == false) { UpdateCaseWebTitle(caseWeb, caseTitle); } } } }); } if (item.DoesUserHavePermissions(SPBasePermissions.ManagePermissions)) { item.TryGrantPermission(assignedTo, SPRoleType.Administrator); item.TryGrantPermission(assignedToSupervisor, SPRoleType.Administrator); } else // We need to elevate to do this { SPSecurity.RunWithElevatedPrivileges(delegate() { using (site = new SPSite(properties.SiteId)) { using (SPWeb rootWeb = site.OpenWeb()) { SPList list = rootWeb.GetListByInternalName(properties.List.RootFolder.Name); SPListItem i = list.GetItemById(item.ID); i.TryGrantPermission(assignedToSupervisor, SPRoleType.Administrator); i.TryGrantPermission(assignedTo, SPRoleType.Administrator); } } }); } } catch (Exception x) { Logger.Instance.Error("Error in Cases list ItemUpdated", x, DiagnosticsCategories.eCaseExtensions); } }
public override void ItemUpdating(SPItemEventProperties properties) { try { // This represents the item as it WAS, not as it WILL BE SPListItem item = properties.ListItem; SPUser prevAssignedToUser = null; SPUser prevSupervisorUser = null; #region Determine Locking Behavior bool newLockCase, oldLockCase, lockCase, unlockCase; SPListItem oldCaseStatusItem = properties.ListItem.GetFieldLookupAsSPListItem("CaseStatusLookup"); SPListItem newCaseStatusItem = properties.AfterProperties.GetFieldLookupAsSPListItem("CaseStatusLookup", properties.List); if (bool.TryParse(newCaseStatusItem[eCaseConstants.FieldGuids.ECASE_STATUSES_LOCK_SITE].ToString(), out newLockCase) && bool.TryParse(oldCaseStatusItem[eCaseConstants.FieldGuids.ECASE_STATUSES_LOCK_SITE].ToString(), out oldLockCase)) { if (string.Compare(newCaseStatusItem[eCaseConstants.FieldGuids.OOTB_TITLE].ToString(), oldCaseStatusItem[eCaseConstants.FieldGuids.OOTB_TITLE].ToString()) != 0) { if (oldLockCase && !newLockCase) // If the status was locked, and now not, lets unlock { unlockCase = true; lockCase = false; } else if (oldLockCase && newLockCase) // If the status was locked, and still locked, do nothing { lockCase = unlockCase = false; } else if (!oldLockCase && newLockCase) // If the status was not locked, and now is locked, need to lock { unlockCase = false; lockCase = true; } else // !oldLockCase && !lockCase -- do nothing { lockCase = unlockCase = false; } } else // the status didn't change { lockCase = unlockCase = false; } } else // Something's wrong if we cannot parse one { throw new InvalidOperationException("Cannot parse Case Status Values"); } #endregion string caseWebUrl = item[eCaseConstants.FieldGuids.ECASES_LIST_CASEURL].ToString().Split(',')[0]; SPSite site; SPWeb caseWeb; SPSecurity.RunWithElevatedPrivileges(delegate() { using (site = new SPSite(caseWebUrl)) { using (caseWeb = site.OpenWeb()) { SPUser assignedToUser = null; SPUser supervisorUser = null; GetAssignedToAndSupervisor(item, properties.AfterProperties, caseWeb, ref assignedToUser, ref prevAssignedToUser, ref supervisorUser, ref prevSupervisorUser); if (assignedToUser != null || supervisorUser != null) { UpdateProxyGroups(caseWeb, assignedToUser, supervisorUser, prevAssignedToUser, prevSupervisorUser); } } } }); if (prevAssignedToUser == null) { prevAssignedToUser = item.GetFieldAsSPUser(eCaseConstants.FieldGuids.OOTB_ASSIGNEDTO); } if (prevSupervisorUser == null) { prevSupervisorUser = item.GetFieldAsSPUser(eCaseConstants.FieldGuids.ECASES_LIST_ASSIGNEDTOSUPERVISOR); } if (item.DoesUserHavePermissions(SPBasePermissions.ManagePermissions)) { item.TryRemovePermissions(prevAssignedToUser); item.TryRemovePermissions(prevSupervisorUser); } else // We need to elevate to do this { SPSecurity.RunWithElevatedPrivileges(delegate() { using (site = new SPSite(properties.SiteId)) { using (SPWeb rootWeb = site.OpenWeb()) { SPList list = rootWeb.GetListByInternalName(properties.List.RootFolder.Name); SPListItem i = list.GetItemById(item.ID); i.TryRemovePermissions(prevAssignedToUser); i.TryRemovePermissions(prevSupervisorUser); } } }); } if (lockCase || unlockCase) { SPSecurity.RunWithElevatedPrivileges(delegate() { using (site = new SPSite(caseWebUrl)) { using (caseWeb = site.OpenWeb()) { using (DbAdapter dbAdapter = new DbAdapter()) { SPList list = site.RootWeb.GetListByInternalName(properties.List.RootFolder.Name); SPListItem i = list.GetItemById(item.ID); _connectionString = site.RootWeb.Properties[eCaseConstants.PropertyBagKeys.ECASE_DB_CONNECTION_STRING]; dbAdapter.Connect(_connectionString); if (lockCase) { LockCaseSite(i, caseWeb, dbAdapter); } else { UnlockCaseSite(i, caseWeb, dbAdapter); } } } } }); } } catch (Exception x) { Logger.Instance.Error("Error in Cases list ItemUpdating", x, DiagnosticsCategories.eCaseSite); } }