Monday, June 20, 2011

Programatically adding column to sharepoint list, code to add column to list

Create new console application in VS and add following code in program.cs file. also add refrence to Microsoft.Sharepoint.dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace AddColumntoList
    class Program
        static void Main(string[] args)
                    Console.WriteLine("--Add Column to list--");
                    Console.WriteLine("Enter Site URL : ");
                    String str_siteurl = Console.ReadLine();
                    using (SPSite siteColl = new SPSite(str_siteurl))
                        Console.WriteLine("Site Found");
                        siteColl.AllowUnsafeUpdates = true;
                        using (SPWeb rootweb = siteColl.OpenWeb())
                            rootweb.AllowUnsafeUpdates = true;
                            //Console.WriteLine("Site Found");
                            Console.WriteLine("Enter List Name: ");
                            string listName = Console.ReadLine();
                            SPList testList = rootweb.Lists[listName];
                            Console.WriteLine("list Found");
                            Console.WriteLine("Enter Column Name: ");
                            String colName = Console.ReadLine();
                            Console.WriteLine("Select Type Of Column:-");
                            Console.WriteLine("1. Single line Text");
                            Console.WriteLine("2. MultiLine Text");
                            Console.WriteLine("Enter Choice number:- ");

                            string typeChoice = Console.ReadLine();
                            switch (typeChoice)
                                case "1":
                                    SPFieldText newcolumn = (SPFieldText)testList.Fields.CreateNewField(SPFieldType.Text.ToString(), colName);
                                case "2":
                                    SPFieldMultiLineText newcolumnMul = (SPFieldMultiLineText)testList.Fields.CreateNewField(SPFieldType.Note.ToString(), colName);

                            //    newcolumn.Required = false;
                            //newcolumn.Description = "comma seperated value";
                            Console.WriteLine("Column Created");

                            Console.WriteLine("Add Colum to Default View y/n??? :-");
                            string defChoice = Console.ReadLine();
                            if (defChoice == "y" || defChoice == "Y")
                                SPView defView = testList.DefaultView;
                                Console.WriteLine("Column added to default view");
                            rootweb.AllowUnsafeUpdates = false;
                        siteColl.AllowUnsafeUpdates = false;
                catch (Exception ex)
                    Console.WriteLine("ERROR : " + ex.Message);
                    Console.WriteLine("Press Enter to Continue and ESC to exit...");
            } while (Console.ReadKey(true).Key != ConsoleKey.Escape);
            Console.WriteLine("Press Enter to Exit...");

No comments:

Post a Comment

SharePoint document metadata not updating

I faced a weird issue today, Metadata for document which has lookup column was not updating even after saving the item. There was no erro...