Programmatically Create Site Column, Site Content Type in Sharepoint

Here is a Code snippet for creating a new Site column and adding it the site collection Site column gallery and int0 a newly created content type.

public void CreateSiteColum()
{

string columnGroup = "Custom Columns";

// Adding Custom Column Amount
string amountFieldName = site.Fields.Add("Amount", SPFieldType.Currency, false);

SPFieldCurrency amountField = (SPFieldCurrency)site.Fields.GetFieldByInternalName(amountFieldName);

amountField.Group = columnGroup;
amountField.DisplayFormat = SPNumberFormatTypes.TwoDecimals;
amountField.MinimumValue = 0;
amountField.Update();

See detailed Post @ Create site column in Sharepoint 2010

/* CREATE SITE CONTENT TYPES */

string contentTypeGroup = "Custom Content Types";

// Get a Document content type to be the parent of our new custom content type.
SPContentType documentCType = site.AvailableContentTypes[SPBuiltInContentTypeId.Document];

// Create the Custom Financial Document content type.
SPContentType financialDocumentCType = new SPContentType(documentCType, site.ContentTypes, "Financial Document");
site.ContentTypes.Add(financialDocumentCType);

// Note: A content type is not initialized until after it is added.
financialDocumentCType = site.ContentTypes[financialDocumentCType.Id];
financialDocumentCType.Group = contentTypeGroup;


// Add the Amount column. Child content types inherit the column.
SPFieldLink amountFieldRef = new SPFieldLink(amountField);
financialDocumentCType.FieldLinks.Add(amountFieldRef);

// Commit changes.
financialDocumentCType.Update();

}

site in the above code is SPSite (or SiteCollection) Object.

2 comments:

  1. hey check this new website www.countcode.com. It's a social network made for programmers, where you can download,share or upload source codes, where you can count your own code lines for free. You have access to the web forum and the web chatroom. we are happy to have you joined to our community!

    ReplyDelete
  2. hey check this new website www.countcode.com. It's a social network made for programmers, where you can download,share or upload source codes, where you can count your own code lines for free. You have access to the web forum and the web chatroom. we are happy to have you joined to our community!

    ReplyDelete

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer or anyone else. Should you have any questions or concerns please e-mail me at sharepointprogrammingblogger@gmail.com .

Copyright (c) 2010 @ myshaepointwork.blogspot.com. All rights are reserved.Do Not Copy.

@ Learning SharePoint.com