0

I'm working on a new data type for an Umbraco site and have been building a custom control to accommodate this. I need to query the active data source for the document types. It's going to return several and I want to populate a DropDownBox with those items. I've got the query and drop down ready to go, but I'm a having trouble with the connection to the DB to execute the query. Since it's in a custom control the datasource is not built into the control itself but in Umbraco. Any help on this would be greatly appreciated!!

2
  • Sounds obvious but did you do this tinyurl.com/3u9e87b . I think you need more information about setting up datasource in general, in such case you can refer any article with this. Puttin as comment as your question was not so clear to me. Commented Apr 20, 2011 at 12:30
  • Of course, I even checked the Umbraco forums for support as well before posting here. I just love the development community over here and figured if someone else with Umbraco experience stumbled across the question it would be an easy answer for them. The data source is built into Umbraco's installation, so I'm guessing there's a method I can use somewhere to pass the query and get the results, just haven't been able to find where. Commented Apr 20, 2011 at 12:43

2 Answers 2

1

If anyone is interested, I was able to solve this using the following. Include a method for ISqlHelper:

public static ISqlHelper SqlHelper
    {
        get
        {
            return Application.SqlHelper;
        }
    }

Using the ISqlHelper, I was able to run a query and populate the drop-down:

IRecordsReader docTypes = SqlHelper.ExecuteReader("SELECT nodeId, alias FROM cmsContentType WHERE alias IN ('Product','Article') ORDER BY alias");

        while (docTypes.Read())
        {
            string dtName = docTypes.GetString("alias");
            string dtId = docTypes.GetInt("nodeId").ToString();

            _lstDocType.Items.Add(new ListItem(dtName, dtId));
        }

Hope that helps someone else too!

Sign up to request clarification or add additional context in comments.

Comments

0

You can also use the Umbraco Business Logic to get all the document types with:

var DocTypes = um.cms.businesslogic.web.DocumentType.GetAllAsList();
var DocTypeDropDown = new DropDownList();
foreach (var DocType in DocTypes)
{
    var DocTypeListItem = new ListItem
    {
        Text = DocType.Text,
        Value = DocType.Id.ToString()
    };
    DocTypeListItem.Attributes.Add("rel", DocType.Alias);
    DocTypeDropDown.Items.Add(DocTypeListItem);
}

You can obviously arrange the DropDownList however you like, I just wanted to draw attention to the properties: Text, Alias, and Id as likely items you will want to use of the DocType.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.