示例#1
0
        /// <summary>
        /// An item has been added.
        /// </summary>
        public override void ItemAdded(SPItemEventProperties properties)
        {
            this.EventFiringEnabled = false; // FINALLY CLAUSE AT END WILL ENSURE IT IS IN PROPER STATE
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(properties.SiteId))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            string uniqueUrl = string.Empty;

                            try
                            {
                                SPList casesList = web.Lists[properties.ListId];
                                SPListItem item  = casesList.GetItemById(properties.ListItemId);

                                #region Get LockCase Value
                                bool lockCase;
                                SPListItem caseStatusItem = item.GetFieldLookupAsSPListItem("CaseStatusLookup");
                                bool.TryParse(caseStatusItem[eCaseConstants.FieldGuids.ECASE_STATUSES_LOCK_SITE].ToString(), out lockCase);
                                #endregion

                                #region Get Users
                                SPUser assignedTo, supervisor;
                                GetAssignedToAndSupervisor(item, out assignedTo, out supervisor);
                                #endregion

                                #region Get Other Properties
                                string title        = item[eCaseConstants.FieldGuids.OOTB_TITLE].ToString();
                                string description  = (string)item[eCaseConstants.FieldGuids.ECASES_LIST_DESCRIPTION] ?? string.Empty;
                                string uniqueCaseId = item[eCaseConstants.FieldGuids.ECASES_LIST_UNIQUECASEID].ToString();
                                #endregion

                                // Get a unique URL that doesn't currently exist
                                uniqueUrl = web.GetValidNewWebUrl(uniqueCaseId);

                                SPWeb caseWeb;
                                string caseWebUrl = string.Empty;
                                try
                                {
                                    using (caseWeb = CreateCaseWeb(web, uniqueUrl, title, description))
                                    {
                                        #region Configure Proxy Group Lists Permissions
                                        SPGroup ownersGroup          = eCaseGroups.GetOwners(caseWeb);
                                        SPGroup participantsGroup    = eCaseGroups.GetParticipants(caseWeb);
                                        SPGroup externalSharingGroup = eCaseGroups.GetExternalUsers(caseWeb);

                                        SPRoleDefinition fullControl;
                                        caseWeb.TryGetRoleDefinition("Full Control", out fullControl);
                                        SPRoleDefinition reader;
                                        caseWeb.TryGetRoleDefinition("Read", out reader);
                                        SPRoleDefinition contributor;
                                        caseWeb.TryGetRoleDefinition("Contribute", out contributor);

                                        SPList ownersList = caseWeb.GetListByInternalName("Bureau");
                                        ownersList.BreakRoleInheritance(false);
                                        ownersList.TryGrantPermission(ownersGroup, fullControl);
                                        ownersList.TryGrantPermission(participantsGroup, reader);

                                        SPList participantsList = caseWeb.GetListByInternalName("Investigator");
                                        participantsList.BreakRoleInheritance(false);
                                        participantsList.TryGrantPermission(ownersGroup, fullControl);
                                        participantsList.TryGrantPermission(participantsGroup, reader);
                                        #endregion

                                        #region Configure Group Memberships via Proxy
                                        this.EventFiringEnabled = true; // TURN ON EVENT FIRING SO THAT PROXY GROUP EVENT RECEIVER FIRES
                                        UpdateProxyGroups(caseWeb, assignedTo, supervisor, null, null);
                                        if (assignedTo.LoginName != properties.UserLoginName && (supervisor == null || supervisor.LoginName != properties.UserLoginName))
                                        {
                                            SPList ownersProxyList = eCaseGroups.OwnersList(caseWeb);
                                            eCaseGroups.AddGroupProxyItem(ownersProxyList, web.EnsureUserProperly(properties.UserLoginName));
                                        }
                                        this.EventFiringEnabled = false; // FINALLY CLAUSE AT END WILL ENSURE IT IS IN PROPER STATE
                                        #endregion

                                        #region Configure Sharing With External Party
                                        SPList shareWithExternalUsersList = caseWeb.GetListByInternalName("ShareWithExternalUsers");
                                        shareWithExternalUsersList.BreakRoleInheritance(false);
                                        shareWithExternalUsersList.TryGrantPermission(ownersGroup, fullControl);
                                        shareWithExternalUsersList.TryGrantPermission(participantsGroup, contributor);
                                        shareWithExternalUsersList.TryGrantPermission(externalSharingGroup, reader);
                                        #endregion

                                        ConfigureItemPermissions(item, caseWeb, assignedTo, supervisor);

                                        #region Configure Group Permissions on Root Web
                                        //bool allowunsafeupdate = web.AllowUnsafeUpdates;
                                        //web.AllowUnsafeUpdates = true;
                                        //SPRoleDefinition topreader;
                                        //web.TryGetRoleDefinition("Read", out topreader);
                                        //web.TryGrantPermission(ownersGroup, topreader);
                                        //web.TryGrantPermission(participantsGroup, topreader);
                                        //web.TryGrantPermission(reviewersGroup, topreader);
                                        //web.Update();
                                        //web.AllowUnsafeUpdates = allowunsafeupdate;
                                        #endregion

                                        #region Configure eCase Statuses List Permissions
                                        //SPList statusList = web.GetListByInternalName("eCaseStatuses");
                                        //if (!statusList.HasUniqueRoleAssignments)
                                        //{
                                        //    statusList.BreakRoleInheritance(true);
                                        //    statusList.Update();
                                        //}
                                        //statusList.TryGrantPermission(ownersGroup, topreader);
                                        //statusList.TryGrantPermission(participantsGroup, topreader);
                                        //statusList.TryGrantPermission(reviewersGroup, topreader);
                                        //statusList.Update();
                                        #endregion

                                        UpdateDefaultAspx(item, caseWeb);

                                        // Record item guid in caseweb's property bag so methods can find the parent item easily
                                        caseWeb.AddProperty(eCaseConstants.PropertyBagKeys.ECASE_CASE_LIST_ITEM_GUID, item.UniqueId.ToString());
                                        caseWeb.Update();

                                        try
                                        {
                                            _connectionString = web.Properties[eCaseConstants.PropertyBagKeys.ECASE_DB_CONNECTION_STRING];
                                            using (DbAdapter dbAdapter = new DbAdapter())
                                            {
                                                dbAdapter.Connect(_connectionString);

                                                try
                                                {
                                                    #region Add Case Site Info to eCaseManagement Database
                                                    Guid siteGuid             = caseWeb.Site.ID;
                                                    Guid caseWebGuid          = caseWeb.ID;
                                                    Guid activitiesTasksGuid  = caseWeb.GetListByInternalName(eCaseConstants.ListInternalNames.ACTIVITIES_AND_TASKS).ID;
                                                    Guid caseRelatedDatesGuid = caseWeb.GetListByInternalName(eCaseConstants.ListInternalNames.CASE_RELATED_DATES).ID;

                                                    CreateCaseWebSProc sProc = new CreateCaseWebSProc(siteGuid, item.UniqueId, caseWebGuid, activitiesTasksGuid, caseRelatedDatesGuid);
                                                    dbAdapter.ExecuteNonQueryStoredProcedure(sProc);
                                                    #endregion
                                                }
                                                catch (Exception x)
                                                { Logger.Instance.Error(string.Format("Failed while recording new CaseWeb at {0} in database", caseWeb.Url), x, DiagnosticsCategories.eCaseSite); }

                                                if (lockCase)
                                                {
                                                    LockCaseSite(item, caseWeb, dbAdapter);
                                                }
                                            }
                                        }
                                        catch (Exception x)
                                        { Logger.Instance.Error(string.Format("Failed while accessing database at {0} with connection string {1}", caseWeb.Url, _connectionString), x, DiagnosticsCategories.eCaseSite); }

                                        caseWebUrl = string.Format("{0}, {1}", caseWeb.Url, "View Case");
                                    }
                                }
                                catch (Exception x)
                                {
                                    caseWebUrl = string.Format("{0}/_layouts/1033/error.htm, {1}", web.Url, "ERROR!");
                                    Logger.Instance.Error(string.Format("Failed to create Case Web for {0}", uniqueCaseId), x, DiagnosticsCategories.eCaseSite);
                                    if (web.Webs[uniqueUrl].Exists)
                                    {
                                        RemoveCaseWeb(properties, uniqueUrl);
                                    }

                                    throw x;
                                }
                                finally
                                {
                                    item[eCaseConstants.FieldGuids.ECASES_LIST_CASEURL] = caseWebUrl;
                                    item.SystemUpdate(); // Update the change in the DB using the system account
                                }
                            }
                            catch (Exception x)
                            {
                                Logger.Instance.Error(string.Format("Failed to create Case Web for {0}", uniqueUrl), x, DiagnosticsCategories.eCaseSite);
                                throw x;
                            }
                        }
                    }
                });
            }
            catch (Exception x)
            { throw x; }
            finally
            { this.EventFiringEnabled = true; }
        }