Custom Context classes

John Smith asked on August 9, 2017 16:12

Hello,

I am having trouble finding documentation for building your own custom Context classes. Can anyone direct me to this sort of information?

Thank you,

Recent Answers


Trevor Fayas answered on August 9, 2017 16:21

Can you clarify what you mean by "Context class"?

If you mean a 'page' type of object that you can put on the tree and it has a Page Template, etc, then it's just the Custom Page Types,

if it's an actual database class for custom modules, you want to look at Custom Module Classes.

0 votesVote for this answer Mark as a Correct answer

John Smith answered on August 9, 2017 16:34

Hey Trevor,

Sorry about that. I meant Context classes such as SiteContext

Thank you,

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on August 9, 2017 17:35

Ahh, now i track you.

Well a Context class is just a normal Class that has Context specific information. Can you provide a little more details about what type of "Context" you are looking for?

For example, the MembershipContext is really just providing the UserInfo and other objects for the current user (which does a look up on their session / credentials i suppose).

You can extend Info and InfoProviders, but Context and Helpers are just classes Kentico provides to aid you, so you should just be able to create your own classes and use them.

0 votesVote for this answer Mark as a Correct answer

Aaron Siebersma answered on August 16, 2018 19:32 (last edited on August 16, 2018 19:35)

The first step is to modify the signature of the context class. It needs to be a non-static class, inheriting from AbstractContext<T>. It should also have the [RegisterAllProperties] attribute.

[RegisterAllProperties]  
public class StudentContext : AbstractContext<StudentContext> {

The second step is to register the context. It looks like this is done by calling Module.RegisterContext from the OnInit method of a Module. In my case, I used a module called PGPLoaderModule:

public class PGPLoaderModule : Module {
    protected override void OnInit() {
        base.OnInit();

        Module.RegisterContext<StudentContext>("StudentContext");
1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.