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)
{
do
{
try
{
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);
testList.Fields.Add(newcolumn);
break;
case "2":
SPFieldMultiLineText newcolumnMul = (SPFieldMultiLineText)testList.Fields.CreateNewField(SPFieldType.Note.ToString(), colName);
testList.Fields.Add(newcolumnMul);
break;
}
// newcolumn.Required = false;
//newcolumn.Description = "comma seperated value";
//testList.Fields.Add(newcolumn);
testList.Update();
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;
defView.ViewFields.Add(colName);
defView.Update();
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...");
Console.ReadLine();
}
}
}
No comments:
Post a Comment