Nibble

Archive for the 'C#' Category


Creating members programmatically 40

Maybe you want to import a number of members or create a custom registration form. You can use the umbraco api to create, update and delete members

I’ll first create a ‘demo’ member type and add 2 properties.

image

Address (alias address) and City (alias city) both of the type textstring.

Owkey now I am ready to write some c# code that will create a new member.

Before I can access the umbraco member api I need to reference cms.dll and businesslogic.dll (these can be found in the bin folder of umbraco).

Now I’ll add a new usercontrol to my project where a member can register. With a couple of textboxes and a button.

image

So the code I need to execute on the click event of the button is:

first add this at the top

using umbraco.cms.businesslogic.member;
using umbraco.cms.businesslogic.propertytype;

And on the click event:

if (Member.GetMemberFromEmail(txtEmail.Text) == null)
{
                MemberType demoMemberType = new MemberType(1040); //id of membertype ‘demo’
                Member newMember = Member.MakeNew(txtName.Text, demoMemberType, new umbraco.BusinessLogic.User(0));
 
                newMember.Email = txtEmail.Text;
                newMember.Password = txtPassword.Text;
                newMember.LoginName = txtUsername.Text;
 
                newMember.getProperty(“address”).Value = txtAddress.Text; //set value of property with alias ‘address’
                newMember.getProperty(“city”).Value = txtCity.Text; //set value of property with alias ‘city’
 
                newMember.Save();
}
else
{
                //member exists
}

 

First I check if there is no other member with the email address, if the email address is not used yet I can make a new member.

Demo project: download

.net usercontrols and the umbraco dictionary 10

With umbraco it is possible to create multilingual site. I’m not going to get into details on how to set this up (not in this post), but when you want to localize your templates and macro’s you need to create dictionary items. Your .net usercontrols can also use the umbraco dictionary.

You manually create a new dictionary item and then set the control’s text to the dictionary item (after referencing the needed assemblies).

MyControl.Text = umbraco.library.GetDictionaryItem(“mydictionaryitem”);
MySecondControl.Text = umbraco.library.GetDictionaryItem(“anotherdictionaryitem”);

 

This is owkey when you have a small usercontrol and don’t have to put in loads of dictionary items.

But image what a lousy task it would  be if you had 30,40, more controls on your usercontrol (creating all the dictionary items manually).

So to save me time I wrote some code that loops all controls in a usercontrol and sets the text of the control.

I created a base usercontrol class which inherits from the usercontrol class. And I make my usercontrols inherit from my base class.

This Is my BaseUserControl class:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using umbraco.cms.businesslogic;
 
namespace TG.Umb.Library
{
    public class BaseUserControl: UserControl
    {
        private string _dictionaryId;
 
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Request[“addtodictionary”] != null)
            {
                //create dictionary items

                createDictionaryItems(this.Controls);
                Response.Write(“Dictionary Items Created”);
            }
 
            if (!IsPostBack)
            {
                //set copy of controls
                setCopy(this.Controls);
            }
        }
 
        #region UmbracoDictionaryFunctions
 
        //loop all controls and set text
        public void setCopy(ControlCollection Controls)
        {
 
            foreach (Control ctrl in Controls)
            {
 
 
                if (ctrl is System.Web.UI.WebControls.Panel)
                {
                    setCopy(ctrl.Controls);
                }
 
                if (ctrl is System.Web.UI.WebControls.Literal)
                {
                    if (umbraco.library.GetDictionaryItem(dictionaryId + ((Literal)ctrl).ID) != string.Empty)
                    {

                        ((Literal)ctrl).Text = umbraco.library.GetDictionaryItem(dictionaryId + ((Literal)ctrl).ID);
                    }
                }
 
                if (ctrl is System.Web.UI.WebControls.HyperLink)
                {
                    if (umbraco.library.GetDictionaryItem(dictionaryId + ((HyperLink)ctrl).ID) != string.Empty)
                    {
                        ((HyperLink)ctrl).Text = umbraco.library.GetDictionaryItem(dictionaryId + ((HyperLink)ctrl).ID);
                    }
                }
 
                if (ctrl is System.Web.UI.WebControls.Button)
                {
                    if (umbraco.library.GetDictionaryItem(dictionaryId + ((Button)ctrl).ID) != string.Empty)
                    {
                        ((Button)ctrl).Text = umbraco.library.GetDictionaryItem(dictionaryId + ((Button)ctrl).ID);
                    }

                }
 
                if (ctrl is System.Web.UI.WebControls.CheckBox)
                {
                    if (umbraco.library.GetDictionaryItem(dictionaryId + ((CheckBox)ctrl).ID) != string.Empty)
                    {
                        ((CheckBox)ctrl).Text = umbraco.library.GetDictionaryItem(dictionaryId + ((CheckBox)ctrl).ID);
                    }
                }
            }
        }
 
        //loop all controls and create dictionary Item
        public void createDictionaryItems(ControlCollection Controls)
        {
            foreach (Control ctrl in Controls)
            {

                if (ctrl is System.Web.UI.WebControls.Panel)
                {
                    createDictionaryItems(ctrl.Controls);
                }
 

                if (ctrl is System.Web.UI.WebControls.Literal)
                {
                    createDictionaryItem(dictionaryId + ((Literal)ctrl).ID, ((Literal)ctrl).Text);
                }
 
                if (ctrl is System.Web.UI.WebControls.HyperLink)
                {
                    createDictionaryItem(dictionaryId + ((HyperLink)ctrl).ID, ((HyperLink)ctrl).Text);
                }
 
                if (ctrl is System.Web.UI.WebControls.Button)
                {
                    createDictionaryItem(dictionaryId + ((Button)ctrl).ID, ((Button)ctrl).Text);
                }
 
                if (ctrl is System.Web.UI.WebControls.CheckBox)
                {
                    createDictionaryItem(dictionaryId + ((CheckBox)ctrl).ID, ((CheckBox)ctrl).Text);
                }
            }
        }
 

        //create dictionaryItem
        private void createDictionaryItem(string name, string defaulttext)
        {
            try
            {
 
                Dictionary.DictionaryItem.addKey(name, defaulttext);

                Response.Write(“added “ + name + “<br/>”);
            }
            catch
            {
                //allready exists
            }
        }
 

        //identifier for umbraco dictionary
        //string dictionaryId = “frmLoginLogout”;
        public string dictionaryId
        {
            get
            {
 
                return _dictionaryId;


            }
            set
            {
                _dictionaryId = value;
            }
        }
 
        #endregion
    }
}
 

 

So all I need to do in my usercontrols is:

public partial class frmEditProfile : BaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.dictionaryId = this.GetType().Name;
            base.Page_Load(sender, e);
 
        }
    }

 

Inherit from the custom class instead of UserControl, set the dictionaryId and cal the page_load of the base class.

Now the next step is to trigger the usercontrol to loop its controls and add dictionary items.

You can do this by adding ?addtodictionary=true to the querystring on the page where you have your usercontrol.

So if you have a page http://www.examplesite.com/page.aspx you request http://www.examplesite.com/page.aspx?addtodictionary=true

And *boom* the dictionary items get created automaticly. This is an example of the dictionary items created:

dictionary

So the name will start with the usercontrol name followed by the control id;

Now you can start setting the correct copy for the dictionary items…

And as you will notice the text will be fetched from the dictionary.

The loops in the class will only work on  literal, hyperling, button and checkbox, you can add other types of controls in the loops.

BaseUserControl class: download

Why Umbraco Rocks - Adding custom usercontrols 17

Umbraco rocks! Why ?

Reason one:

It’s extremely easy to add custom .net usercontrols to your umbraco site. So you can still take full advantage of the .net framework when working with umbraco. I made a quick screencast showing how to add a .net usercontrol to your umbraco site.

 

image

Demo Project: download

« Previous Page