Search This Blog

2009-04-30

SPContentType

What is SPContentType/What are the method and properties of SPContentType/How can we use SPContentType
You create a content type at the site level. This site content type acts as a template that is independent of any specific list or library. That site content type is then available on any child site. For example, if you create a site content type at the root site of a site collection, that site content type becomes available on any site in that site collection, so that you can add it to any list in the site collection.
When you add a site content type to a list, Windows SharePoint Services 3.0 copies a local copy of the site content type onto the list itself. This local instance is called a list content type and applies only to the list onto which it was copied.
Because Windows SharePoint Services stores a copy of the site content type as a list content type on each list to which that site content type is added, you can make changes to a list content type without affecting the site content type itself. The changes to a list content type are limited to that list and do not affect the site content type, or any other content types that inherit from that same site content type.
The following figure shows this relationship. Two site content types, Memo and Spec, are defined for a site. When the Spec content type is added to the list of a child site list, Windows SharePoint Services copies an instance of the site content type locally onto the list.
The Memo site content type is also available to be added to lists on the child site. However, this content type has not been added to the list, so no copy of it resides on the list.

Base Content Hirarchy:-

Properties :-

FieldLinks
Gets an SPFieldLinkCollection that represents the collection of column, or field, references in the content type.
Fields
Gets an SPFieldCollection that represents collection of column references included in the content type.
Group
Gets and sets the content type group to which the content type is assigned.
Hidden
Gets an SPContentTypeId that represents the content type ID of the content type.
Id
Gets an SPContentTypeId that represents the content type ID of the content type.
Name
Gets or sets the content type name.

Note:-

There is a 1-to-1 correlation between the items in the SPFieldLinkCollection and SPFieldCollection objects. For each SPFieldLink you add to a content type, Windows SharePoint Services adds a corresponding SPField object that represents the merged view of that column as it's defined in the content type.
You cannot directly add or delete items from an SPFieldCollection object in an SPContentType object; trying to do so throws an error.

Methods:-
The Two most important methods are Delete and Update.

Small Code Snippet Example:-

//* Create a text field dynamically.
//* Create a contenttype dynamically.
//* Add field to a content type
//* Add content type to a list
//*Enable the content type on the list and make it vailable on New Menu Items.
namespace FieldAddDemo
{
class Program
{
static void Main(string[] args)
{
SPSite site = new SPSite("site Url");
SPWeb web = site.OpenWeb();

string fieldName = web.Fields.Add("CustomF1",
SPFieldType.Text, false);

web.Update();
SPField fieldCustom1 = web.Fields["CustomF1"];

fieldCustom1.Group = "Extended Columns";
fieldCustom1.ShowInNewForm = true;
fieldCustom1.Update();

SPContentType contentNew = new
SPContentType(web.ContentTypes["Item"], web.ContentTypes,
"NewDemoContent");

web.ContentTypes.Add(contentNew);
web.Update();
contentNew.FieldLinks.Add(new SPFieldLink(fieldCustom1));
contentNew.Update();




SPList dlist = web.Lists["list-Name"];
dlist.ContentTypesEnabled = true;
dlist.ContentTypes.Add(web.ContentTypes["NewDemoContent"]);
dlist.Update();
SPContentType[] contentCol = new SPContentType[1];
contentCol[0] = dlist.ContentTypes["NewDemoContent"];
SPFolder fld = dlist.RootFolder;
fld.UniqueContentTypeOrder = contentCol;
fld.Update();

}
}
}

Resource obtain from MSDN

No comments: