Use an object model to create a content type

Source: Internet
Author: User

1. Define a function to create the content type. The Code is as follows:

private SPContentType CreateContentType(SPWeb web, string typeName,string baseTypeName,
string description){ SPContentType contentType = null; try { SPContentType baseType; if (string.IsNullOrEmpty(baseTypeName)) { baseType = web.AvailableContentTypes[SPContentTypeId.Empty]; } else { baseType = web.AvailableContentTypes[baseTypeName]; } contentType = new SPContentType(baseType, web.ContentTypes, typeName); contentType.Description = description; web.ContentTypes.Add(contentType); } catch { } return contentType;}

2. Define two overloaded functions. Create a website field in the function, and then add fieldlink to the content type.

 private SPField AddField(SPContentType contentType, string fieldDisplayName, 
SPFieldType fieldType, bool bRequired){ SPField field = null; try { using (SPWeb web = contentType.ParentWeb) { web.Fields.Add(fieldDisplayName, fieldType, bRequired); field = web.Fields[fieldDisplayName]; contentType.FieldLinks.Add(new SPFieldLink(field)); } } catch { } return field;}private SPField AddField(SPContentType contentType, string fieldDisplayName, SPFieldType fieldType, bool bRequired, bool bCompactName, StringCollection choices){ SPField field = null; try { using (SPWeb web = contentType.ParentWeb) { web.Fields.Add(fieldDisplayName, fieldType, bRequired, bCompactName, choices); field = web.Fields[fieldDisplayName]; // add a field link to the content type contentType.FieldLinks.Add(new SPFieldLink(field)); } } catch { } return field;}

3. The following function adds the content type to a specified Website:

 private void AddContentType(SPWeb web){   if (web != null)   {       web.AllowUnsafeUpdates = true;       SPContentType contentType = CreateContentType(web, "OnlineEvent11", "Event", "OnlineEvent11");       if (contentType != null)       {           StringCollection statusColl = new StringCollection();           statusColl.Add("Approved");           statusColl.Add("Rejected");           AddField(contentType, "Event Status", SPFieldType.Choice,true, false, statusColl);           AddField(contentType, "Number Of Attendees",SPFieldType.Number, false);           StringCollection eventType = new StringCollection();           eventType.Add("Business/Career");           eventType.Add("Classes & Lectures");           eventType.Add("Dinners/Galas");           eventType.Add("Fundraiser");           eventType.Add("Misc");           eventType.Add("Seminars");
eventType.Add("Support Groups"); AddField(contentType, "Type Of Event", SPFieldType.Choice,true, false, eventType);
StringCollection audienceType = new StringCollection(); audienceType.Add("Adults"); audienceType.Add("Everyones' Invited"); audienceType.Add("Families"); audienceType.Add("Kids"); audienceType.Add("Men"); audienceType.Add("Seniors"); audienceType.Add("Singles"); audienceType.Add("Teens"); audienceType.Add("Women"); AddField(contentType, "Audience Type", SPFieldType.MultiChoice, true, false, audienceType); contentType.Update(true); } }}

4 :.The following code is used in feature:Executed when activated

public override void FeatureActivated(SPFeatureReceiverProperties properties){   if (properties != null)   {      SPWeb web = properties.Feature.Parent as SPWeb;      if (web != null)      {          AddContentType(web);      }   } }

V:Since spfeaturereceiver is an abstract class, the following method must be overwritten.

public override void FeatureDeactivating(SPFeatureReceiverProperties properties){ }public override void FeatureInstalled(SPFeatureReceiverProperties properties){ }public override void FeatureUninstalling(SPFeatureReceiverProperties properties){ }

6. After the deployment is successful, for example:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.