I think I've successfully created a list based on a custom content type but when I click on the "Add new item" link for the list, I see nothing except:
- a basic ribbon tab labeled "Edit" with icons for SAVE, CANCEL, PAST, CUT/COPY, ATTACH FILE and SPELLING
- two buttons labeled SAVE and CANCEL
The content type contains roughly a dozen items and I was hoping to have the "Add new item" link produce a "data entry form" with labels for the fields followed by textboxes for the value of the field. I would like the behavior of "add new document" prompt for "metadata" after the upload of a file.
I find that if I create the list using a template type of SPListTemplateType.DocumentLibrary (instead of SPListTemplateType.GenericList), then I "sort of" get the sort of effect I am looking for. That is, I get a "document information panel" with labels and textboxes as described which is good - but this only comes after a prompt for what file to upload (which is bad because I don't want to upload files into this list).
Here are key snippets of my code currently being tested from a Windows "scratch pad" application running locally on my SP2010 development machine:
private void CreateListBasedOnCT_Click(object sender, EventArgs e)
{
using (var site = new SPSite(SiteUrl))
{
var web = site.RootWeb;
var list = CreateList(txtListName.Text);
list.ContentTypesEnabled = true;
var agreement = CreateContentType(txtCTname.Text); // get reference to the custom content type of the SITE
list.ContentTypes.Add(agreement); // add to content type collection of the LIST
// remove the item content type; go to the LIST content type collection for removal
SPContentType itemtype = list.ContentTypes["Item"];
list.ContentTypes.Delete(itemtype.Id);
list.Update();
listBox1.Items.Add("END: The list has been created based on the content type");
}
}
private SPList CreateList(string newListname)
{
using (var site = new SPSite(SiteUrl))
{
var web = site.RootWeb;
DeleteList(newListname);
var listID = web.Lists.Add(newListname, string.Empty, SPListTemplateType.GenericList);
var list = web.Lists[listID];
list.OnQuickLaunch = true;
list.ContentTypesEnabled = true;
list.Update();
listBox1.Items.Add(newListname + " list created");
return list;
}
}
private SPContentType CreateContentType(string newContentType)
{
using (var site = new SPSite(SiteUrl))
{
var web = site.RootWeb;
DeleteContentType(newContentType);
SPField field = web.Fields.TryGetFieldByStaticName("ApprovingOffical");
if (field == null)
{
var fieldNameInternal = web.Fields.Add("ApprovingOffical", SPFieldType.Text, true);
var siteColumn = web.Fields.GetFieldByInternalName(fieldNameInternal);
siteColumn.Group = "CustomED";
siteColumn.Update();
}
var document = web.ContentTypes["Flexiplace Work Agreement"]; // instantiate the parent
var teleworkCT = new SPContentType(document, web.ContentTypes, newContentType); // instantiate new CT
teleworkCT.ReadOnly = false;
teleworkCT.Group = "Custom ED";
web.ContentTypes.Add(teleworkCT); // need to add CT to the collection before updating it with columns
teleworkCT.Update();
AddExistingSiteColumn(web, "ApprovingOffical", teleworkCT);
AddExistingSiteColumn(web, "Date Modified", teleworkCT);
AddExistingSiteColumn(web, "Job Title", teleworkCT);
AddExistingSiteColumn(web, "Location", teleworkCT);
AddExistingSiteColumn(web, "Government ID Number", teleworkCT);
AddExistingSiteColumn(web, "Business Phone", teleworkCT);
AddExistingSiteColumn(web, "E-Mail", teleworkCT);
teleworkCT.Update();
listBox1.Items.Add(newContentType + " content type created");
return teleworkCT;
}
}