Learning: dynamically creating SharePoint content type

Source: Internet
Author: User

 

In a scenario where you need to create content typeInBunch, there comes the role of dynamic content types.

 

 

The following code snippet helps youInCreating content type dynamically.

 

 

Code:

 

UsingSystem;

UsingMicrosoft. SharePoint;

NamespaceDynamiccontenttypeconsole

{

Class Program

{

Static VoidMain (String[] ARGs)

{

StringSiteurl ="URL of site";

Using(SpsiteObjspsite =New Spsite(Siteurl ))

{

 

Using(SpwebObjspweb = objspsite. openweb ())

{

SpcontenttypeObjspcontenttype =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);

 

SpfieldlinkObjspfieldlink1 =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);

 

SpfieldlinkObjspfieldlink2 =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);

 

SpfieldchoiceChoicefield = (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 ();

 

SpfieldlinkObjspfieldlink3 =New Spfieldlink(Choicefield );

Objspcontenttype. fieldlinks. Add (objspfieldlink3 );

 

Objspweb. Fields. addlookup ("Dynamic lookup column",New Guid("Guidofremotelist"),False);

 

SpfieldlookupLookupfield = (Spfieldlookup) Objspweb. Fields ["Dynamic lookup column"];

 

// Set the remote field

Lookupfield. lookupfield ="Remotefieldname";

Lookupfield. Update ();

SpfieldlinkObjspfieldlink4 =New Spfieldlink(Lookupfield );

Objspcontenttype. fieldlinks. Add (objspfieldlink4 );

 

Objspweb. contenttypes. Add (objspcontenttype );

Objspcontenttype. Update ();

}

}

}

}

}

 

 

 

The code snippetIsThe console application, andIsReady to use.

 

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

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.