Creating User Specific, Editable BizForms in kentico 8.2

Mehrdad ilchizadeh asked on September 26, 2015 09:25

hello,

how to use this in kentico cms 8.2 ?? Creating User Specific, Editable BizForms

var bizFormInfo = BizFormInfoProvider.GetBizFormInfo(viewBiz.FormName, viewBiz.SiteName);

if (bizFormInfo != null) { var dataClassInfo = DataClassInfoProvider.GetDataClass(bizFormInfo.FormClassID);

if (dataClassInfo != null)
{
    var connection = ConnectionHelper.GetConnection();
    var whereCondition = String.Format("UserId = '{0}'", CMSContext.CurrentUser.UserID);
    var dataSet = connection.ExecuteQuery(dataClassInfo.ClassName + ".selectall", null, whereCondition);

    if (!DataHelper.DataSourceIsEmpty(dataSet) && dataSet.Tables[0].Rows.Count == 1)
    {
        var formRecordId = ValidationHelper.GetInteger(dataSet.Tables[0].Rows[0][0], 0);

        viewBiz.FormMode = FormModeEnum.Update;
        viewBiz.ItemID = formRecordId;

        return;
    }

    viewBiz.FormMode = FormModeEnum.Insert;
}

}

Correct Answer

Maarten van den Hooven answered on September 26, 2015 20:49

Hello Mehrdad ilchizadeh,

I refactored the complete code of the "user specific BizForm" solution to Kentico 8.2, so what do you need to do:

  1. Clone the on-line form web part
  2. Change the code in the method SetupControl() to this:

    /// <summary>
    /// Initializes the control properties.
    /// </summary>
    protected void SetupControl()
    {
        if (StopProcessing)
        {
            // Do nothing
            viewBiz.StopProcessing = true;
        }
        else
        {
            // Set BizForm properties
            viewBiz.FormName = BizFormName;
            viewBiz.SiteName = SiteName;
            viewBiz.UseColonBehindLabel = UseColonBehindLabel;
            viewBiz.AlternativeFormFullName = AlternativeFormName;
            viewBiz.ValidationErrorMessage = ValidationErrorMessage;           
    
            // Set the live site context
            if (viewBiz != null)
            {
                viewBiz.ControlContext.ContextName = CMS.ExtendedControls.ControlContext.LIVE_SITE;
            }
    
            // Gets the form info object
            var bizFormInfo = BizFormInfoProvider.GetBizFormInfo(viewBiz.FormName, viewBiz.SiteName);
    
            if (bizFormInfo != null)
            {
                // Gets the class name of the 'ContactUs' form
                DataClassInfo formClass = DataClassInfoProvider.GetDataClassInfo(bizFormInfo.FormClassID);
                string className = formClass.ClassName;
    
                // Loads the form's data
                ObjectQuery<BizFormItem> dataSet = BizFormItemProvider.GetItems(className)
                    .WhereEquals("UserId", CurrentUser.UserID)
                    .Columns("UserId")
                    .TopN(1);
    
                if (!DataHelper.DataSourceIsEmpty(dataSet) && dataSet.Tables[0].Rows.Count == 1)
                {
                    var formRecordId = ValidationHelper.GetInteger(dataSet.Tables[0].Rows[0][0], 0);
                    viewBiz.ItemID = formRecordId;
                    return;
                }
            }
        }
    }
    
  3. Add an field to your form with the following configuration : Field name : UserID, Data Type : Text, Default value : {% CurrentUser.ID |(identity)GlobalAdministrator%} and check out option "Display field in the editing form"

  4. I tested this in my installation so it will work!!!

Good luck and if you need more help you know where to ask.

If this answer helped you, please vote for my answer :-)

1 votesVote for this answer Unmark Correct answer

Recent Answers


Mehrdad ilchizadeh answered on September 26, 2015 09:26

where i am use this code???where call?? and this have error in kentico 8.2...

please help me

0 votesVote for this answer Mark as a Correct answer

Mehrdad ilchizadeh answered on September 26, 2015 14:23

how to use this code in kentico cms 8.2???

var dataSet = connection.ExecuteQuery(dataClassInfo.ClassName + ".selectall", null, whereCondition);
0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on September 26, 2015 18:17

I'd look at the documentation and API changes from versions 7 -> 8 to find how the ExecuteQuery() method has changed.

1 votesVote for this answer Mark as a Correct answer

Maarten van den Hooven answered on September 28, 2015 16:07

Hello Mehrdad ilchizadeh,

So to load the dataset instead of the old code :

var dataSet = connection.ExecuteQuery(dataClassInfo.ClassName + ".selectall", null, whereCondition);

You now change it in Kentico 8.2 to :

 // Loads the form's data
ObjectQuery<BizFormItem> dataSet = BizFormItemProvider.GetItems(className)
    .WhereEquals("UserId", CurrentUser.UserID)
    .Columns("UserId")
    .TopN(1);

The new technology in Kentico 8.2 is also used in the document queries, to known more about this it is nice to read this article from Martin Hejtmanek : Kentico 8 Technology - Introduction to DataQuery.

Good luck with programming in Kentico!

If this answer helped you, please vote for my answer :-)

1 votesVote for this answer Mark as a Correct answer

Maarten van den Hooven answered on September 30, 2015 18:32

Hello Mehrdad ilchizadeh, Did the code worked for you? or do you still having problems?

1 votesVote for this answer Mark as a Correct answer

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