Sometimes it is necessary to create a Sharepoint Content Type by code, specially when batch creating content types. The following code snippet shows how a sharepoint content type can be created in C#. I have also included some not-so-simple field types like choice and lookup.
using (SPSite p_site = new SPSite(urlToSite))
{
using (SPWeb p_web = p_site.OpenWeb())
{
SPContentType CustomContentType = new
SPContentType(p_web.AvailableContentTypes["ParentContentType"],
p_web.ContentTypes, "MyContentType");
// A string Field
p_web.Fields.Add("NumberColumn", SPFieldType.Number);
SPFieldLink fLink1 = new SPFieldLink(p_web.Fields["NumberColumn"]);
CustomContentType.FieldLinks.Add(fLink1);
// A required number field
p_web.Fields.Add("StringColumn", SPFieldType.Text, true);
SPFieldLink fLink2 = new SPFieldLink(p_web.Fields["StringColumn"]);
CustomContentType.FieldLinks.Add(fLink2);
//A Choice Field
p_web.Fields.Add("ChoiceColumn", SPFieldType.Choice, false);
SPFieldChoice choicefield = (SPFieldChoice)p_web.Fields["ChoiceColumn"];
// Add a group to the filed
choicefield.Group = "MyGroup";
// Add choices
choicefield.Choices.Add(string.Empty);
choicefield.Choices.Add("Yes");
choicefield.Choices.Add("No");
// Set the default choice
choicefield.DefaultValue = string.Empty;
choicefield.Update();
SPFieldLink fLink3 = new SPFieldLink(choicefield);
CustomContentType.FieldLinks.Add(fLink3);
p_web.Fields.AddLookup("LookupColumn", new Guid("guidofremotelist"), false);
// Edited thanks for Lee Richardson's comment below
SPFieldLookup lookupfield = (SPFieldLookup)p_web.Fields["LookupColumn"];
// Set the remote lookup list
lookupfield.LookupList = new Guid("guidofremotelist");
// Set the remote field
lookupfield.LookupField = "remotefieldname";
lookupfield.Update();
SPFieldLink fLink4 = new SPFieldLink(lookupfield);
CustomContentType.FieldLinks.Add(fLink4);
p_web.ContentTypes.Add(CustomContentType);
CustomContentType.Update();
}
}
Comments
When I try to update my
Permalink Submitted by Steve (not verified) on Tue, 08/07/2008 - 7:12am.
Steve, Add the following
Permalink Submitted by Joe (not verified) on Sun, 28/12/2008 - 8:12pm.
Hi Joe, Thanks for pointing
Permalink Submitted by Niral on Fri, 02/01/2009 - 10:59am.
With respect to the Lookup
Permalink Submitted by Scott (not verified) on Fri, 18/07/2008 - 12:39pm.
hi, I am trying to add a
Permalink Submitted by Chris S (not verified) on Thu, 21/08/2008 - 3:41pm.
Hi Chris, You are trying to
Permalink Submitted by Niral on Sun, 24/08/2008 - 10:36pm.
You are trying to get data
Permalink Submitted by mpcoc (not verified) on Sat, 14/02/2009 - 5:35am.
With respect to the Lookup
Permalink Submitted by mpcoc (not verified) on Sat, 14/02/2009 - 5:35am.
I got the "use the AddLookup
Permalink Submitted by Lee Richardson (not verified) on Mon, 23/03/2009 - 2:49pm.
site.Fields.AddLookup("LookupColumn", new Guid("guidofremotelist"), false); SPFieldLookup newField = (SPFieldLookup)site.Fields["LookupColumn"]; ...Hope someone finds that useful.Hi Lee, It is a very good
Permalink Submitted by Vadim Dzyuban (not verified) on Sat, 04/04/2009 - 3:24am.
Pages