SaaS CMS has officially launched! Learn more now.

List all by content Type


Hi all !
I'm new to Episerver.


I created a ContentData TestContent class which looks like this :

public class TestContent : ContentData, IContent
        //IContent implementation
        public string Name { get; set; }
        public ContentReference ContentLink { get; set; }
        public ContentReference ParentLink  { get; set; }
        public Guid ContentGuid  { get; set; }
        public int ContentTypeID  { get; set; }
        public bool IsDeleted { get; set; }

        //Test Entity implementation
        public virtual string TestName { get; set; }
        public virtual int TestNumber { get; set; }
        public virtual string Text { get; set; }

Then I saved a sample through Controller as following:

public ActionResult Index()
    var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
    var testContent = contentRepository.GetDefault<TestContent.TestContent> 
            testContent.Name = "Test2";
            testContent.TestName = "Testing name";
            testContent.TestNumber = 25;
            testContent.Text = "Testing text2";
    contentRepository.Save(testContent, Security.AccessLevel.Administer);
    return View();

Then I checked in database that it did saved as i wanted and it is a success.

My question: I want to be able to get a list of all the things I saved earlier of this content type which is TestContent from a controller.

Thanks for all help

Edited, Mar 23, 2020 14:34

Here you get all usages and then gets them for a type

var contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
var contentModelUsage = ServiceLocator.Current.GetInstance<IContentModelUsage>();

var contentType = contentTypeRepository.Load<TestContent>();
var usages = contentModelUsage.ListContentOfContentType(contentType);

var yourSavedStuff = usages.Select(x => _contentLoader.Get<TestContent>(x.ContentLink));
Edited, Mar 23, 2020 15:09
Wade - Mar 23, 2020 15:22
Thanks alot !!
Now I learned about IContentModelUsage :D
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.