Monday, March 21, 2011

Add Fields to list in share point through coding.

In share point lists we have the fields. We can add the fields to list though administration. Here is the code to add the fields to list through coding.
        /// Add a field to the list
        private static void AddFieldToList(SPList list)
        {
            string fieldName = string.Empty;
            Console.WriteLine("enter the name of the field to add");
            fieldName = Console.ReadLine();
            if (!string.IsNullOrEmpty(fieldName))
            {
                if (list.Fields[fieldName] == null)
                {
                    list.Fields.Add(fieldName, SPFieldType.Text, true);
                    list.Update();
                    Console.WriteLine("New field added to " + list.Title);
                }
                else
                {
                    Console.WriteLine("field already exists in"+list.Title);
                }
            }
            else
            {
                Console.WriteLine("field name should not be null or empty");
            }           
        }
This is pretty straight way, may help you.

Share this