Hi
I will start with creating a new console application (make sure you are working on your server) with Visual Studio 2010.
Add references for the following assemblies:
Microsoft.Office.Server.dll
Microsoft.SharePoint.dll
Both of them can be found inside the 14\ISAPI folder of your server.
Also add reference for:
System.Web
Make sure to also add the necessary using statements.
When all of this is sorted we can start coding!
First of all we need connection with the SharePoint Server.
{
SPWeb web = site.OpenWeb();
site.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = true;
SPListCollection coll = web.Lists;
}
We use SPSite to connect to our SharePoint Site, to access the Lists we also need a SPWeb object.
So we can get all the available lists using the SPWeb.Lists command.
Before I continue with any code, I would like to mention that to add a new List we need:
SPWeb object
A Title
A Description
A SPListTemplateType object
The necessary SPFields, which are the “columns”
I will continue pasting the previous code and adding the new code.
{
SPWeb web = site.OpenWeb();
site.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = true;
SPListCollection coll = web.Lists;
Guid gd = coll.Add("ListTitle", "ListDescription", SPListTemplateType.CustomGrid);
}
With the new line of code i have added we are adding a new List with the specified title, description and ListTemplateType.
After adding the List this function will return the Guid of the new List. This Guid we need to add the “columns” called SPFields.
To add a column to the new list we use the following command
coll[Guid].Fields.Add("ColumnTitle", SPFieldType.Text, true);
This function expects 3 parameters: a title for the column, the type for the column (text, boolean, ..), and a boolean whether it is required or not to enter content in this Field.
So we can use this to add some columns to our new list
{
SPWeb web = site.OpenWeb();
site.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = true;
SPListCollection coll = web.Lists;
Guid gd = coll.Add("ListTitle", "ListDescription", SPListTemplateType.CustomGrid);
coll[gd].Fields.Add("FirstName", SPFieldType.Text, true);
coll[gd].Fields.Add("LastName", SPFieldType.Text, true);
coll[gd].Fields.Add("Married", SPFieldType.Boolean, true);
coll[gd].Update();
}
By using the Update method we execute the changes.
The next stap is adding a custom View, before we do this we delete the existing View.
{
SPWeb web = site.OpenWeb();
site.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = true;
SPListCollection coll = web.Lists;
Guid gd = coll.Add("ListTitle", "ListDescription", SPListTemplateType.CustomGrid);
coll[gd].Fields.Add("FirstName", SPFieldType.Text, true);
coll[gd].Fields.Add("LastName", SPFieldType.Text, true);
coll[gd].Fields.Add("Married", SPFieldType.Boolean, true);
coll[gd].Update();
string defaultquery = coll[gd].Views[0].Query;
SPViewCollection viewcoll = coll[gd].Views;
Guid anothergd = coll[gd].Views[0].ID;
viewcoll.Delete(anothergd);
}
Before we delete the existing view we wanna keep the Query, to use it in our new View.
After this View is deleted we can add our custom View.
To add a View we need “ViewFields” which refer to the ListFields.
{
SPWeb web = site.OpenWeb();
site.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = true;
SPListCollection coll = web.Lists;
Guid gd = coll.Add("ListTitle", "ListDescription", SPListTemplateType.CustomGrid);
coll[gd].Fields.Add("FirstName", SPFieldType.Text, true);
coll[gd].Fields.Add("LastName", SPFieldType.Text, true);
coll[gd].Fields.Add("Married", SPFieldType.Boolean, true);
coll[gd].Update();
string defaultquery = coll[gd].Views[0].Query;
SPViewCollection viewcoll = coll[gd].Views;
Guid anothergd = coll[gd].Views[0].ID;
viewcoll.Delete(anothergd);
System.Collections.Specialized.StringCollection viewfields = new System.Collections.Specialized.StringCollection();
viewfields.Add("Title");
viewfields.Add("FirstName");
viewfields.Add("LastName");
viewfields.Add("Married");
coll[gd].Views.Add("View name", viewfields, defaultquery, 100, true, true);
coll[gd].Update();
}