Pages

Subscribe:

Friday, October 28, 2011

Programmatically create a new content type in sharepoint 2010

Here is the code snippet for creating a content type in a document library using object model.

Public void CreateContentType(string _contentTypename, sting _fullFilePath,SPweb currentWeb)
{

SPFile myFile = currentWeb.GetFile(fullFilePath);

if (currentWeb.AvailableContentTypes[contentTypename] == null)
{
var myContentType = new SPContentType(currentWeb.AvailableContentTypes[new SPContentTypeId("0x0101")], currentWeb.ContentTypes, “contenttypename”)
{
DocumentTemplate = myFile.ServerRelativeUrl,
Description = “My custom Content type”
}

currentWeb.ContentTypes.Add(myContentType);
}

SPDocumentLibrary myDocLib = (SPDocumentLibrary) currentWeb.Lists["doclibname"];

myDocLib.ContentTypesEnabled = true;
myDocLib.ContentTypes.Add(currentWeb.AvailableContentTypes["contenttypename"]);

myDocLib.Update()

}

No comments:

Post a Comment