Creating SharePoint Custom List and Content Types based on the class POCO using VS 2010
Do you want to customize a
list with various column fields and various options? It is so simple by create
a custom list and Content types based on the class POCO (Plain Old CLR Objects).
Think about this scenario, Customer need lists for storing Project data
and every list having various columns and various options. Also customer
asking a simple GUI to create customizes lists. The GUI web part showed below.
In these types
of requirements, you need to create lists based on class POCO. The following
steps are explaining solution.
Step 1: Create a "Project" class in VS 2010
In this class,
List fields are added as Properties of Project Class. Another important
thing we need to consider while defining a properties which is, these
properties are going to be converted as list fields
in SharePoint server. So there are some associate attributes with
list fields. Ex- Field Value Required, Each field has different display name,
etc.
How to associate these attributes with
properties? There is an absolute way in .Net which is Reflections and
Attributes
Here, I’m going to consider two
associate attributes for a property, one is Boolean value of Required Field and
other one is String value of Display Name. Then define a new attribute class
named as SPFieldAttribute.cs
Project.cs
Step 2: Using Visual WebPart to bind the “Project”.cs
Create
a visual WebPart to design GUI. Here we need to bind the class properties in field column.
To bind
properties in check-box-list as fields.
Step 3: Convert Property type into SPFieldType
Before Creating a list and content type you need to convert the property type into SPFieldType. Here I use the conversion only for Text, Number and DateTime Fields.
String
- SPFieldType.Text
Decimal
- SPFieldType.Number
Integer
- SPFieldType.Integer
Boolean
- SPFieldType.Boolean
DateTime
- SPFieldType.Boolean
Uri
-SPFieldType.URL
Id
- SPFieldType.Guid
Xml
-SPFieldType.File
Now you can create the list and content type mentioned above.
creating custom list in SharePoint Server with selected properties. Before checking this you need to go down and check how to create content type by using VS 2010.
public void createList (string contentName, string
listName, Type entityType,
List<string> selectedProperties, bool Navigation)
{
// choose your site
using (SPSite site
= new SPSite(url))
{
using (SPWeb
web = site.OpenWeb())
{
SPListCollection list = web.Lists;
if (!ListExists(web, listName))
{
list.Add(listName, "Project List", SPListTemplateType.GenericList);
SPList newlist = web.Lists[listName];
newlist .ContentTypesEnabled
= true;
newlist.ContentTypes.Add(web.ContentTypes[contentName]);
newlist.Update();
newlist.ContentTypes.Delete(newlist.ContentTypes["Item"].Id);
SPView view = newlist.Views["All
Items"];
SPField titleField = newlist.Fields.GetField("Title");
SPViewFieldCollection collection = view.ViewFields;
foreach (string property in selectedProperties)
{
if (newlist.Fields.ContainsField(property))
{
SPField Field = newlist.Fields.GetField(property);
collection.Add(Field);
}
else
{
PropertyInfo[] properties =
entityType.GetProperties();
foreach (PropertyInfo
prop in properties)
{
object[] attributes = prop.GetCustomAttributes(true);
if (((SPFieldAttribute)attributes[0]).DisplayName
== property)
{
SPFieldType type = ConvertToSPType(prop.PropertyType);
newlist.Fields.Add(((SPFieldAttribute)attributes[0]).DisplayName, type
((SPFieldAttribute)attributes[0]).Required);
SPField Field =
newlist.Fields.GetField(((SPFieldAttribute)attributes[0]).DisplayName);
view.ViewFields.Add(Field);
SPFieldType type = ConvertToSPType(prop.PropertyType);
newlist.Fields.Add(((SPFieldAttribute)attributes[0]).DisplayName, type
((SPFieldAttribute)attributes[0]).Required);
SPField Field =
newlist.Fields.GetField(((SPFieldAttribute)attributes[0]).DisplayName);
view.ViewFields.Add(Field);
}
}
}
}
//Enable Navigation
if (Navigation)
{
newlist.OnQuickLaunch = true;
}
newlist.Update();
view.Update();
}
}
}
}
}
So Creating Custom list and Content type based
on the class POCO using VS 2010 is simple and having more customized way.
Have a fun..:)