public static void createlist()
{
string site = "http://admin-pc:2345";
try
{
using (SPSite osite = new SPSite(site))
{
using (SPWeb oweb = osite.OpenWeb())
{
SPListTemplate template = oweb.ListTemplates["Custom List"];
if (oweb.Lists.TryGetList("Training list final123")==null)
{
oweb.AllowUnsafeUpdates = true;
oweb.Lists.Add("Training list final123", " ", template);
SPList olist = oweb.Lists.TryGetList("Training list final123");
olist.Fields.Add("field1", SPFieldType.Text, false);
olist.Fields.Add("field2", SPFieldType.Text, false);
olist.Fields.Add("field3", SPFieldType.Choice, false);
SPFieldChoice ochoice = (SPFieldChoice)olist.Fields["field3"];
ochoice.EditFormat=SPChoiceFormatType.Dropdown;
ochoice.Choices.Add("Male");
ochoice.Choices.Add("female");
ochoice.Update();
olist.OnQuickLaunch = true;
SPView newview = olist.DefaultView;
newview.ViewFields.Add("field1");
newview.ViewFields.Add("field2");
newview.ViewFields.Add("field3");
newview.Update();
olist.Update();
oweb.AllowUnsafeUpdates = false;
Console.WriteLine("List fields added");
}
else
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite osite1 = new SPSite(site))
{
using (SPWeb oweb1 = osite1.OpenWeb())
{
SPList olist = oweb.Lists.TryGetList("Training list final123");
olist.Fields.Delete("field1");
olist.Fields.Delete("field2");
olist.Fields.Delete("field3");
olist.Fields.Add("field1", SPFieldType.Text, false);
olist.Fields.Add("field2", SPFieldType.Text, false);
olist.Fields.Add("field3", SPFieldType.Choice, false);
SPFieldChoice ochoice = (SPFieldChoice)olist.Fields["field3"];
ochoice.EditFormat=SPChoiceFormatType.RadioButtons;
ochoice.Choices.Add("Male");
ochoice.Choices.Add("female");
ochoice.Update();
SPView newview = olist.DefaultView;
newview.ViewFields.Add("field1");
newview.ViewFields.Add("field2");
newview.ViewFields.Add("field3");
newview.Update();
olist.OnQuickLaunch = true;
oweb.AllowUnsafeUpdates = true;
olist.Update();
oweb.AllowUnsafeUpdates = false;
}
}
});
Console.WriteLine("List already Exist it has to be udgraded");
}
}
}
}
catch (Exception ex)
{
throw ex;
}
Console.ReadLine();
}
No comments:
Post a Comment