1

I am a beginner in programming and Sitecore. And I would like to create a Controller using C# to handle some business logic. But I cannot find any backend source files of Sitecore. Could someone tell me where I can find those source file and edit it? Because I saw there are plenty of people who can write custom backend code for their Site.

Can upload my root folder if needed.

1
  • Hey, welcome to Sitecore & Programming. There is a lot to unpack with learning programming and learning Sitecore. I would start by taking the free Sitecore Essentials course here: learning.sitecore.com/pathway/sitecore-essentials - and learning how custom solutions are created and deployed to a Sitecore instance.
    – Richard Seal
    Commented Jun 8, 2022 at 13:02

2 Answers 2

1

If you want to create a controller rendering then there is an option to clone an existing component in Sitecore SXA. Follow this article to know how to clone a component in SXA.

https://sbhatiablogs.wordpress.com/2020/10/01/how-to-customized-components-in-sxa-by-cloning/

And to know more about creating a controller rendering in SXA, you need to follow this article shared by Sitecore.

https://doc.sitecore.com/xp/en/developers/sxa/17/sitecore-experience-accelerator/walkthrough--building-a-simple-rendering.html

Also if you want to check the code of an existing OOTB Sitecore controller then you need to go to a particular component and you will find the controller class and assembly in the controller field like this.

enter image description here

In this example the controller is Sitecore.XA.Feature.Composites.Controllers.AccordionController and you can find this in the Sitecore.XA.Feature.Composites DLL. So you need to use some third-party tools to extract this DLL and you will be able to find the code.

Then if you want to extend this code, you can copy the same logic in your controller class and extend it as per your needs.

Then in the above image in the Controller field, you need to pass your class and assembly name and then this will work with your logic.

Hope this is enough information for you to start.

2
  • Thanks for your much information. May I ask you 1 more question that is, is it possible to create an API in the backend? The API will do something like redirect the user based on some conditions. We will call this API by using the Javascript Ajax.
    – Newbie555
    Commented Jun 8, 2022 at 13:57
  • @Newbie555 As per my knowledge, when a controller rendering will render a view (cshtml), then on that you can write some JS/Jquery function to create an Ajax request. But I am not sure that it will allow you to redirect a user to another page. I've not done like that. Commented Jun 8, 2022 at 14:05
0

You need to create a controller action in MVC first. You can follow the below steps: -

  • You should configure a .NET MVC project for Sitecore, create a new controller in that .NET MVC solution.

  • Name the controller as TestData and create a new action method List inside it as shown below:-

    public class TestDataController : Controller
    {
        public ActionResult List()
        {
           // Write your usiness logic here..
           return View();
        }
    }
    
  • Create a view for this action method under Views/TestData/ path in the same .NET solution.

    @{
        if(Model!=null)
        {
           // Write you html here
        }
        else
        {
          // Write you html here
        }
     }
    

Now Create a Controller rendering in Sitecore :-

  • Login to Sitecore Content editor. Go to Layouts->Renderings.
  • Right click on Renderings, click on Insert->Controller Rendering. Enter the data values as shown below.

enter image description here

Controller: TestData, Controller Action: List

  • Save and publish the item.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.