學習:Dynamically Creating Sharepoint Content Type

來源:互聯網
上載者:User

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.