Dependent fields in Form

Chetan Sharma asked on February 20, 2014 09:56

Hi,

I have this requirement.

We have an article section in our website with limited authors. These authors selection is in a Contributor form using a drop down (because this is linked to "related articles by author" functionality). These authors have their google plus profile also. This drop down data is filled via a custom table. So our custom table has two fields, author and g+ profile link.

I want to access this g+ profile link related to an article inside transformation. How can I achieve that?

An article can have multiple authors which can be selected via separate drop downs, all pointing to the same custom table. That is I want to access a Database table value inside a transformation, in this case g+ url for an author.

Second approach I thought would be if I we can have two dependent fields. In this case selecting an author in one field automatically loads its corresponding g+ url in second drop down in a Document type form.

Any better approach to achieve this is welcome!!

Thanks, Chetan

Correct Answer

answered on February 20, 2014 16:53

Great question!

I hope I understand your requirement properly, as I'm getting two different impressions of it. Are you wanting to display the Google+ profile link inside the user contribution form directly? Or do you want to display it on the completed document?

I like your first approach best, personally. I think it would be more powerful and cleaner to query the custom table directly rather than relying on multiple drop-downs for each author. For that approach (accessing the custom table through the transformation), you would need to create a custom transformation function: Adding Custom Functions to Transformations.

For example, you could create a transformation function called GetAuthorProfileLink and do something like this (I will assume some things that are easy to change, such as your custom table name and how you are referencing the user in that table):

public string GetAuthorProfileLink(int userID) {
// Creates a new Custom table item provider
CustomTableItemProvider customTableProvider = new CustomTableItemProvider(CMSContext.CurrentUser);
string customTableClassName = "customtable.authorprofiles";

// Checks if Custom table 'Sample table' exists
DataClassInfo customTable = DataClassInfoProvider.GetDataClass(customTableClassName);

if (customTable != null) {

    // Prepares the parameters
    string where = "UserID =" + userID.ToString();
    int topN = 1;
    string columns = "ProfileLink";

    // Gets the data set according to the parameters
    DataSet dataSet = customTableProvider.GetItems(customTableClassName, where, null, topN, columns);

    if (!DataHelper.DataSourceIsEmpty(dataSet)) {
        // Gets the custom table item ID
        string profileLink = ValidationHelper.GetString(dataSet.Tables[0].Rows[0][0], "No Profile Specified");
        return profileLink;
    }
    else return "No Profile Specified";
}
else return "Author Not Found";

}

Once you have done that, you can call your function within a transformation with the following syntax (I'm not sure how you're storing the authors of the articles, but I assume it is a custom field on the document type):

<%# GetAuthorProfileLink(Eval<int>("AuthorID")) %>

Would that approach work for your needs?

0 votesVote for this answer Unmark Correct answer

Recent Answers


Chetan Sharma answered on February 20, 2014 23:02

Yes, this approach will work for me as it is more intuitive and make life easy for the contributors of our website.

Till now have created a big website with complex functionality without writing a custom transformation. But looks like now we need to use it.

Can we do the same thing by writing a function inside transformation or do we need to take the route suggested by you?

Thanks for the detailed answer.

Best Regards, Chetan

0 votesVote for this answer Mark as a Correct answer

answered on February 21, 2014 17:12

I'm glad that will work for you, Chetan!

Regarding your followup question, if you mean to write the entire function inside <script> tags in the ASCX transformation, I don't believe this would work. For the code above to work, additional references must be specified (using System.Data, etc) which are not available in the default transformation namespace. Using the custom transformation function approach you will be able to specify these references.

If you mean that you are referring to some other class you have written from within the transformation, I haven't tested it, but it should work the same as above.

I hope this helps.

0 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on February 23, 2014 11:21

Thanks John for taking out time to answer this.

I may sound naive, primarily because I have never done .NET coding. I have coding experience with Java/Python. Not familiar with .NET ecosystem. How do I compile this new file and fit it into my Kentico project? Do I need to have visual studio to do this?

Any reference doc or explanation will suffice.

Thanks Again!! Chetan

0 votesVote for this answer Mark as a Correct answer

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