Create a Custom Content Type in code : Content types are one of the new Sharepoint Services 3.0, and they allow you to create a tree structure to manage the various types of content that can be used in SharePoint sites. So every different kind of list, from the custom document library, has at its base its content type. To create these objects, there is a user interface, accessible under Site Settings> Site content types. It may be useful, however, to create code to meet our needs to customize sharepoint. Let's see how:
Code:
SPWeb web = new SPSSite ("http://localhost"). OpenWeb ();
SPContentType tipoBase web.AvailableContentTypes = ["Item"];
SPContentType myCT = new SPContentType (tipoBase, web.ContentTypes, contentTypeName);
web.ContentTypes.Add (myCT);
myCT.FieldLinks.Add (new SPFieldLink (web.AvailableFields ["Author"]));
myCT.Update ();
In this example, we created a content type that inherits from the base type "Item", and we added the column "Author." This type of column is called "Site Column" column of the site are also part of the new features in the latest version of Sharepoint, and have the task of defining the actual content of the various content types. So how we can add to our content type, a column already exists within the site, so we can create for ourselves our own columns and put them in our content type.
Code:
SPWeb web = new SPSSite ("http://localhost"). OpenWeb ();
SPContentType tipoBase web.AvailableContentTypes = ["Item"];
SPContentType myCT = new SPContentType (tipoBase, web.ContentTypes, contentTypeName);
web.ContentTypes.Add (myCT);
/ / Add a column pre-existing
myCT.FieldLinks.Add (new SPFieldLink (web.AvailableFields ["Author"]));
/ / Create a custom column and add it to content type
web.Fields.AddLookup string fieldName = ("MyCustomField" web.Lists ["MyLookupList"]. ID, web.ID, true);
SPFieldLookup web.Fields lookup = ["MyCustomField"] as SPFieldLookup;
myCT.FieldLinks.Add (new SPFieldLink (web.AvailableFields [lookup.Title]));
myCT.Update ();
Once created our own content type, we can assign one, or more, the listings on our site. That type of content, it was a very good move.
I hope this TIPS are Useful for you...
Bookmarks