Code Behind:-
SPSite _site;
SPWeb _myWeb;
bool gTemplateExists = false;
string _strList = "ListName";
_site = new SPSite(@"SiteUrl");
_myWeb = _site.OpenWeb();
//check Whether list already exist or not.
if (IsListExists(_myWeb, _strList) == false)
{
CreateNewList(_strList);
}
public bool IsListExists(SPWeb pWeb, string pListName)
{
try
{
SPList list = pWeb.Lists[pListName];
if (list == null)
return false;
else
return true;
}
catch (Exception ex)
{
return false;
}
}
public void CreateNewList(string pListName)
{
try
{
_myWeb.AllowUnsafeUpdates = true;
SPListCollection lists = _myWeb.Lists;
// create new Generic list called ListName
lists.Add("ListName", "List Description", SPListTemplateType.GenericList);
SPList list = _myWeb.Lists["ListName"];
// create Text type new column called "Title"
SPFieldText fldTitle = (SPFieldText)list.Fields.CreateNewField(
SPFieldType.Text.ToString(), " Title ");
fldTitle.Required = false;
// create DateTime type new column called "DateTime"
SPFieldDateTime fldDateTime = (SPFieldDateTime)list.Fields.CreateNewField(
SPFieldType.DateTime.ToString(), "DateTime");
fldDateTime.DisplayFormat = SPDateTimeFieldFormatType.DateTime;
/* add the new fields to the list */
list.Fields.Add(fldTitle);
list.Fields.Add(fldDateTime);
/* finally update list */
list.Update();
/*add the new fields to view */
SPView view = list.DefaultView;
view.ViewFields.Add("Title");
view.ViewFields.Add("DateTime");
view.Update();
_myWeb.AllowUnsafeUpdates = false;
}
catch (Exception ex)
{
}
}
Thanks... Dude... It works for me... :-)
ReplyDelete