Rock.Model.FollowingService.GetFollowedItems C# (CSharp) Method

GetFollowedItems() public method

Gets the entity query For example: If the EntityTypeId is GroupMember, this will return a GroupMember query of group members that the person is following
public GetFollowedItems ( int entityTypeId, int personId ) : IQueryable
entityTypeId int The entity type identifier.
personId int The person identifier.
return IQueryable
        public IQueryable<IEntity> GetFollowedItems( int entityTypeId, int personId )
        {
            EntityTypeCache itemEntityType = EntityTypeCache.Read( entityTypeId );
            var rockContext = this.Context as RockContext;
            var followedItemsQry = this.Queryable().Where( a => a.PersonAlias.PersonId == personId && a.EntityTypeId == entityTypeId );

            if ( itemEntityType.AssemblyName != null )
            {
                Type entityType = itemEntityType.GetEntityType();
                if ( entityType != null )
                {
                    Type[] modelType = { entityType };
                    Type genericServiceType = typeof( Rock.Data.Service<> );
                    Type modelServiceType = genericServiceType.MakeGenericType( modelType );
                    Rock.Data.IService serviceInstance = Activator.CreateInstance( modelServiceType, new object[] { rockContext } ) as IService;

                    MethodInfo qryMethod = serviceInstance.GetType().GetMethod( "Queryable", new Type[] { } );
                    var entityQry = qryMethod.Invoke( serviceInstance, new object[] { } ) as IQueryable<IEntity>;

                    entityQry = followedItemsQry.Join(
                        entityQry,
                        f => f.EntityId,
                        e => e.Id,
                        ( f, e ) => e );

                    return entityQry;
                }
            }

            return null;
        }

Usage Example

        protected void LoadContent()
        {
            var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields( this.RockPage, this.CurrentPerson );

            var entityType = EntityTypeCache.Read(GetAttributeValue("EntityType").AsGuid());

            if ( entityType != null )
            {

                RockContext rockContext = new RockContext();

                int personId = this.CurrentPersonId.Value;

                var followingService = new FollowingService( rockContext );
                IQueryable<IEntity> qryFollowedItems = followingService.GetFollowedItems( entityType.Id, personId );

                int quantity = GetAttributeValue( "MaxResults" ).AsInteger();
                var items = qryFollowedItems.Take(quantity + 1).ToList();

                bool hasMore = (quantity < items.Count);

                mergeFields.Add( "FollowingItems", items.Take( quantity ) );
                mergeFields.Add( "HasMore", hasMore );
                mergeFields.Add( "EntityType", entityType.FriendlyName );
                mergeFields.Add( "LinkUrl", GetAttributeValue( "LinkUrl" ) );
                mergeFields.Add( "Quantity", quantity );

                string template = GetAttributeValue( "LavaTemplate" );
                lContent.Text = template.ResolveMergeFields( mergeFields );

                // show debug info
                if ( GetAttributeValue( "EnableDebug" ).AsBoolean() && IsUserAuthorized( Authorization.EDIT ) )
                {
                    lDebug.Visible = true;
                    lDebug.Text = mergeFields.lavaDebugInfo();
                }
            }
            else
            {
                lContent.Text = string.Format( "<div class='alert alert-warning'>Please configure an entity in the block settings." );

            }
        }
All Usage Examples Of Rock.Model.FollowingService::GetFollowedItems
FollowingService