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. http://camaragenbandarq.blogspot.com/2017/05/membahas-macam-macam-judi-di-indonesia.html
    http://agenbandarqcamarqq.blogspot.com/2017/05/mengetahui-4-tempat-judi-termewah-di.html
    http://agendomino99camarqq.blogspot.com/2017/06/trik-curang-main-bandarq.html
    http://agendominocamar.blogspot.com/2017/06/mengetahui-warnet-di-jepang-yang.html
    http://agendominocamarqq.blogspot.com/2017/06/cara-mendaftar-di-shioqq-situs-agen.html
    http://www.jakartatercinta.com/2017/05/mengenal-situs-shioqq-agen-bandarq.html
    http://www.jakartatercinta.com/2017/05/shioqqnet-agen-bandarq-yang-memberi.html

    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