zoukankan      html  css  js  c++  java
  • 学习:Dynamically Creating Sharepoint Content Type (转)

     

    In a scenario where you need to create Content Type in bunch, there comes the role of Dynamic Content types.

     

     

    The following code snippet helps you in creating content type dynamically.

     

     

    Code:

     

    using System;

    using Microsoft.SharePoint;

    namespace DynamicContentTypeConsole

    {

        class Program

        {

            static void Main(string[] args)

            {

                string siteURL = "URL of Site";

                using (SPSite objSPSite = new SPSite(siteURL))

                {

     

                    using (SPWeb objSPWeb = objSPSite.OpenWeb())

                    {

                        SPContentType objSPContentType = new SPContentType(

                           objSPWeb.AvailableContentTypes["ParentContentType"], objSPWeb.ContentTypes, "Dynamic Content Type");

     

                        // A Required Field

                        // Create Site Column

                        objSPWeb.Fields.Add("Dynamic Number Column", SPFieldType.Number, true);

     

                        SPFieldLink objSPFieldLink1 = new SPFieldLink(objSPWeb.Fields["Dynamic Number Column"]);

                        objSPContentType.FieldLinks.Add(objSPFieldLink1);

     

                        // A required String Field

                        // Create Site Column

                        objSPWeb.Fields.Add("Dynamic String Column", SPFieldType.Text, true);

     

                        SPFieldLink objSPFieldLink2 = new SPFieldLink(objSPWeb.Fields["Dynamic String Column"]);

     

                        objSPContentType.FieldLinks.Add(objSPFieldLink2);

     

                        //A non-required Choice Field

                        // Create Site Column

                        objSPWeb.Fields.Add("Dynamic Choice Column", SPFieldType.Choice, false);

     

                        SPFieldChoice choicefield = (SPFieldChoice)objSPWeb.Fields["Dynamic Choice Column"];

     

                        // Add a group to the filed

                        choicefield.Group = "Dynamic Group";

     

                        // 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 objSPFieldLink3 = new SPFieldLink(choicefield);

                        objSPContentType.FieldLinks.Add(objSPFieldLink3);

     

                        objSPWeb.Fields.AddLookup("Dynamic Lookup Column", new Guid("GUIDofRemoteList"), false);

     

                        SPFieldLookup lookupfield = (SPFieldLookup)objSPWeb.Fields["Dynamic Lookup Column"];

     

                        // Set the remote field

                        lookupfield.LookupField = "RemoteFieldName";

                        lookupfield.Update();

                        SPFieldLink objSPFieldLink4 = new SPFieldLink(lookupfield);

                        objSPContentType.FieldLinks.Add(objSPFieldLink4);

     

                        objSPWeb.ContentTypes.Add(objSPContentType);

                        objSPContentType.Update();

                    }

                }

            }

        }

    }

     

     

     

    The code snippet is the Console Application, and is ready to use.

     

    Come from: http://www.directsharepoint.com/2011/04/dynamically-creating-sharepoint-content.html

  • 相关阅读:
    python 基础(三) list 的用法
    python 基础(二) string 的用法
    python 基础(四)购物车(list类型练习)
    python 基础(一)猜年龄游戏
    centOS 7 环境搭建之安装 python 3
    centOS 7 环境搭建之安装 JDK 8
    源代码管理相关命令(Git常用命令、Nuget常用命令、CMD常用命令)
    .Net Core 3.0 关于Windows Form和WPF的全面支持
    .Net Core 常用开发工具(IDE和运行时、Visual Studio插件、Visual Studio Code插件)
    .Net Core 精选公众号集合(保持更新)
  • 原文地址:https://www.cnblogs.com/LeimOO/p/2193175.html
Copyright © 2011-2022 走看看