zoukankan      html  css  js  c++  java
  • 使用对象模型来创建内容类型

    一:首先定义一个函数来创建内容类型,代码如下:

    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; }

    二:定义两个重载的函数,在函数中先创建网站的field,然后再往内容类型中添加FieldLink

     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; }

    三:下面函数完成向指定网站中添加内容类型的功能

     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); } } }

    四:.如下代码在feature被激活的时候执行

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

    五:由于SPFeatureReceiver是抽象类,所以下面方法也要被重写

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

    六:部署成功后,如下图:

    CreateContenttypeByAPI1

    作者:生鱼片
             
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    jackson 枚举 enum json 解析类型 返回数字 或者自定义文字 How To Serialize Enums as JSON Objects with Jackson
    Antd Pro V5 中ProTable 自定义查询参数和返回值
    ES6/Antd 代码阅读记录
    es 在数据量很大的情况下(数十亿级别)如何提高查询效率啊?
    Antd Hooks
    使用.Net Core开发WPF App系列教程(其它 、保存控件内容为图片)
    使用.Net Core开发WPF App系列教程( 三、与.Net Framework的区别)
    使用.Net Core开发WPF App系列教程( 四、WPF中的XAML)
    使用.Net Core开发WPF App系列教程( 二、在Visual Studio 2019中创建.Net Core WPF工程)
    使用.Net Core开发WPF App系列教程( 一、.Net Core和WPF介绍)
  • 原文地址:https://www.cnblogs.com/carysun/p/CreateContenttypeByAPI.html
Copyright © 2011-2022 走看看