Create a site column in SharePoint 2013/2016 programmatically and using PowerShell

Steps to create Site column using Visual Studio 2013 in SharePoint 2013
SharePoint deveopment training course

This SharePoint tutorial explains, how to create a site column in SharePoint 2013 using visual studio, as well as we can see how to create site column using out of box approach, designer as well as in PowerShell.

The same way we can create a site column programmatically using the visual studio in SharePoint 2016.

Site Column in SharePoint

Site Columns in SharePoint are columns which are available on the site where you have created as well as to the subsites for that particular site. But if you have created any column in a particular list or library then the column will be available inside the list or library only where you have created.

Create SharePoint Site Column using Browser

Open your SharePoint 2013 Site, then click on the Gear icon and then click on Site Settings. This will open the Site Settings page. In the Site Settings page, click on Site columns link which is under Web Designer Galleries.

create site column in sharepoint 2013
create site column in sharepoint 2013

In the Site Columns page, this will display all the site columns available in the site based on various category. Here click on the Create button as shown in the fig below:

SharePoint create a site column using browser
SharePoint create a site column using browser

In the Create Site column give a name and select the type. Here I have chosen a Choice column type as shown in the fig below:

Create site column in SharePoint 2013/2016
Create site column in SharePoint 2013/2016

Either you can categories the site column by adding to an existing group or you can add to a new group by giving a group name as shown in the fig below:

sharepoint 2016 create site column
sharepoint 2016 create site column

Since it is a Choice type, I have added 3 options to it and select my first option to come as default as shown in the fig below.

sharepoint 2013 create site column
sharepoint 2013 create site column

Once you click on OK, the site column will get created and available for use throughout the site.

You will be able to see the column in the Site Columns page.

create site column programmatically sharepoint 2013
create site column programmatically sharepoint 2013

Create a SharePoint Site Column using PowerShell

Here we will discuss, how to create a site column using PowerShell in SharePoint 2013/2016.

Below is the PowerShell command to create a site column in SharePoint 2013/2016. You can run the below command in Windows PowerShell ISE.

Add-PSSnapin "Microsoft.SharePoint.PowerShell"
$site = get-SPSite http://win-pfcp2dgt8di/sites/EnjoySharePoint/
$web = $site.RootWeb
$columnDisplayName="My WebSites Column From PowerShell"
$columnInternalName="scMyWebSitesFromPowerShell"
$sitecolumnFieldType="Choice"
$web.Fields.Add($columnInternalName,$sitecolumnFieldType,$false)
$spChoiceField=$web.Fields.GetField($columnInternalName)
$spChoiceField.Group="MyCustomSiteColumns"
$spChoiceField.Choices.Add("EnjoySharePoint.com")
$spChoiceField.Choices.Add("OnlySharePoint2013.com")
$spChoiceField.Choices.Add("Fewlines4biju.com")
$spChoiceField.Title=$columnDisplayName
$spChoiceField.DefaultValue="EnjoySharePoint.com"
$spChoiceField.FillInChoice=$true
$spChoiceField.Update()
$web.Dispose()
$site.Dispose()

Once the command runs successfully, it will create the site column which we can be able to see in the Site column page. It looks like below:

create site column using powershell
create site column using powershell

Create a SharePoint Site Column using Visual Studio

Follow the below steps to create a site column using Visual studio 2013/2015/2017 in SharePoint 2013/2016.

Open Visual Studio 2013/2015/2017 then File -> New -> Project. In the New Project dialog box, you will be able to see the lots of project templates. From the left side you can go to Installed -> Templates -> Visual C# -> Office/SharePoint -> SharePoint Solutions. Then in the right side choose SharePoint 2013 – Empty Project.

Then give a name for the project location where you want to Save and the name of the Solution and then click on OK. Here make sure you have selected .NET Framework version 4.5 is selected. Because .NET Framework 3.5 is not supported in SharePoint 2013.

In the next screen in SharePoint, Customization wizard gives a local SharePoint URL for debugging and then choose the option as Deploy as a sandboxed solution. You can check out: Difference between Sandbox solution and farm solution.

This will create a solution for us. Then right-click on the Project -> Add -> New Item… which will open the Add New Item dialog box. From the list of templates, choose the Site Column template as shown in the fig below:

sharepoint 2013 create site column using powershell
sharepoint 2013 create site column programmatically

Once you will add this you can see the Elements.xml file which looks like below:

<?xml version="1.0″ encoding="utf-8″?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Field
ID="{ce590f27-49bb-4fc5-900e-a34687d81c3f}"
Name="MySiteColumnFromVisualStudio"
DisplayName="My Site Column From Visual Studio"
Type="Text"
Required="FALSE"
Group="Custom Site Columns">
</Field>
</Elements>

Here, by default, it adds a site column with Single line of text type. Here I have changed a few things like instead of a single line of text, I have changed to a Choice column (set Type=”Choice”). Then I have added 3 values to the Choice column. Then I have changed the Group name to my custom group name which looks like below:

<?xml version="1.0″ encoding="utf-8″?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Field
ID="{ce590f27-49bb-4fc5-900e-a34687d81c3f}"
Name="MySiteColumnFromVisualStudio"
DisplayName="My Site Column From Visual Studio"
Type="Choice"
Required="FALSE"
Group="MyCustomSiteColumnss">
<CHOICES >
<CHOICE>EnjoySharePoint.com</CHOICE>
<CHOICE>OnlySharePoint2013.com</CHOICE>
<CHOICE>Fewlines4Biju.com</CHOICE>
</CHOICES>
</Field>
</Elements>

The solution explorer looks like below:

sharepoint 2013 create site column programmatically
sharepoint 2016 create site column programmatically

I have not changed anything in the Features node. By default, it comes as Web scope.

Now we can right-click and Deploy the solution. Once the solution deployed successfully, you can go to the Site features page (Site Settings -> click on “Manage site features” which is under Site Actions section). You should be able to see our feature already activated, which means our site column should be created successfully.

sharepoint 2013 create site column visual studio
sharepoint 2013 create site column visual studio

Now go to Site Setting page, Then from Web Designer Galleries click on “Site columns“, there we should be able to see the site column. It looks like below:

create site column in sharepoint 2013 using visual studio
create site column in sharepoint 2013 using visual studio

You may like following SharePoint site column tutorials:

Hope this SharePoint tutorial explains, how to create a site column in SharePoint? We saw here how to create site column programmatically using visual studio.

Also, we discussed how to create a site column using PowerShell and using a browser.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →