Skip to main content

Cloud

Creating a site column in SharePoint programmatically using the object model

Creating a Site Column in SharePoint programmatically using an object model

In my last blog I talked about how to create a lookup site column programmatically, but I figured it might be useful to have an example of how to create a regular site column. Here are two methods: one for Choice columns and another one good for other types:

public static SPFieldChoice CreateChoiceSiteColumn(SPWeb web, string fieldName, StringCollection choices)

{

string name = web.Fields.Add(fieldName, SPFieldType.Choice, true, false, choices);

SPFieldChoice field = (SPFieldChoice)web.Fields.GetField(name);

field.Group = groupName;

field.Update();

web.Update();

return field;

}

public static SPField CreateSiteColumn(SPWeb web, string fieldName, SPFieldType type)

{

string name = web.Fields.Add(fieldName, type, true);

SPField field = web.Fields.GetField(name);

field.Group = groupName;

field.Update();

web.Update();

return field;

}

As always, if you see any mistakes or inaccuracies in this blog, please let me know.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

PointBridge Blogs

More from this Author

Follow Us
TwitterLinkedinFacebookYoutubeInstagram