Rock.Model.SiteService.CanDelete C# (CSharp) Method

CanDelete() public method

Determines whether the specified site can be deleted. Performs some additional checks that are missing from the auto-generated SiteService.CanDelete(). TODO This should move into the SiteService CanDelete at some point once the generator tool is adjusted.
public CanDelete ( Site item, string &errorMessage, bool includeSecondLvl ) : bool
item Site The item.
errorMessage string The error message.
includeSecondLvl bool If set to true, verifies that there are no site layouts with any existing pages.
return bool
        public bool CanDelete( Site item, out string errorMessage, bool includeSecondLvl )
        {
            errorMessage = string.Empty;

            bool canDelete = CanDelete( item, out errorMessage );

            if ( canDelete && includeSecondLvl && new Service<Layout>( (RockContext)Context ).Queryable().Where( l => l.SiteId == item.Id ).Any( a => a.Pages.Count() > 0 ) )
            {
                errorMessage = string.Format( "This {0} has a {1} which is used by a {2}.", Site.FriendlyTypeName, Layout.FriendlyTypeName, Page.FriendlyTypeName );
                canDelete = false;
            }

            return canDelete;
        }

Usage Example

Ejemplo n.º 1
0
        /// <summary>
        /// Handles the Delete event of the gSites control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RowEventArgs" /> instance containing the event data.</param>
        protected void gSites_Delete( object sender, RowEventArgs e )
        {
            bool canDelete = false;

            var rockContext = new RockContext();
            SiteService siteService = new SiteService( rockContext );
            Site site = siteService.Get( e.RowKeyId );
            if ( site != null )
            {
                string errorMessage;
                canDelete = siteService.CanDelete( site, out errorMessage, includeSecondLvl: true );
                if ( !canDelete )
                {
                    mdGridWarning.Show( errorMessage, ModalAlertType.Alert );
                    return;
                }

                siteService.Delete( site );

                rockContext.SaveChanges();

                SiteCache.Flush( site.Id );
            }

            BindGrid();
        }
All Usage Examples Of Rock.Model.SiteService::CanDelete