Experimenting with the Sitefinity 4.0 API Step-by-Step

In my last Blog, I provided some code to get the response data from a form and show it in a list box. I know at least one person had the question, "Where do I put this code?". That's actually a very good question and made me take a step back and think about website developers just getting started with Sitefinity 4.0. Sitefinity is quite powerful and using only the Sitefinity Administrator you can do many things. However, if you need to do custom coding that requires using the Sitefinity API, I'll provide detailed steps to show you how to get started.

Using my previous Blog as a starting point, let's examine the steps to test that code. If you're going to venture into Sitefinity development, I recommend using MS Visual Studio as your development tool since it integrates seamlessly with the Sitefinity Project Manager.

My steps will assume you are using MS Visual Studio as your development tool. The first step is to open the Sitefinity Project Manager, select your website and click the "Edit in Visual Studio" link.

 

The solution explorer in Visual Studio should look something like this:

 

Now let's add an external page to our site, the definition of external page in this context is a page that is outside the control of the Sitefinity Administrator. We will create the new page in the root folder of our solution. You can do this by right clicking the project node in the solution explorer " SitefinityWebApp" in my solution, choose "Add" | "New item...".

 

From the "Add New Item" dialog, select Web Form and give it a new name (MyTestPage.aspx) and click "Add".

 

Now we can edit the .aspx file of our new form. In Design view, open the VS toolbox and drop a ListBox control on the form.

 

Now we are ready to add our FormsManager code. Copy and paste this code into the Page_Load method of our form. (Note: the hardcoded names in this code assume you have already added a Sitefinity Form and TextBox Widget as described in my previous Blog.)

protected void Page_Load(object sender, EventArgs e)
{
    FormsManager formsManager = FormsManager.GetManager();

    // Get the form named "sf_testform"
    var forms = formsManager.GetForms().Where(f => f.Name == "sf_testform");

    foreach (var form in forms)
    {
        var records = formsManager.GetFormEntries(form);

        // Loop through all the response records for this form
        foreach (var record in records)
        {
            FormEntry fe = (FormEntry)record;

            // Grab the newtextbox column value to display in a list box
            var TextBoxValue = fe.GetValue("newtextbox");

            ListBox1.Items.Add(new ListItem() { Text = TextBoxValue.ToString() });
        }
    }
}

To get access to all the pieces of this code you need to add the following "using" statements to your code:

using Telerik.Sitefinity.Modules.Blogs;
using Telerik.Sitefinity.Modules.News;
using Telerik.Sitefinity.Modules.Events;

Now we are ready to test our code. Since our code is in the Form_Load method, we just need to navigate to our form to execute this code. To do this, we'll need to add a link to our page. You can run the website straight from Visual Studio (or from the Sitefinity Project Manager), whichever you choose you'll need to get to the Sitefinity Administrator and click the "Pages" tab, then the "Create a page" link to add a new page.

 

Give your page a name (FirstPage) and click the "Create and go add content" button.

 

Next drag a Content Block widget to your form and click "Enter Content".

 

Click the Hyperlink Manager button and we'll add a link to our external page.

 

In the "Insert a link" dialog, put "~/MyTestPage.aspx" for the "Web address" and "My Test Page" for the "Text to display", then click the "Insert the link" button.

 

Now "Publish" the page, by click the Publish button.

 

Finally, set this page as the "Home Page" of our site. To accomplish this, click the "Action" link next to your page and choose "Set as Homepage".

 

Okay, once again we can run the site from Visual Studio and this time if you've followed all my steps you will see a screen like below, click the "My Test Page" link to see the results of the custom code:

 

Here is the result we've been working towards:

 

Now you have test environment setup to try out other things with the Sitefinity API with a quick way to verify if the code is working as you expect. Since you are running from Visual Studio you have the debugger available to help you track down issues. This example is using the FormsManager, but you can start working with the any of the other Sitefinity Managers (BlogsManager, EventsManager, NewsManager etc) and start exercising the power of the Sitefinity 4.0 API.

comments powered by Disqus