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