Steps to create Site column using Visual Studio 2013 in SharePoint 2013

Steps to create Site column using Visual Studio 2013 in SharePoint 2013

In this post we will discuss how we can create a site column in SharePoint 2013 using visual studio, as well as we can see how can we create site column using out of box approach, designer as well as in PowerShell.

Site Columns 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.

Also, you can read:
Working with Nintex Forms for Office 365

SharePoint 2013 Create a custom master page using Visual Studio 2013

SharePoint 2013 Online Remove Quick Edit from SharePoint List

Create 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.

 Steps to create Site column using Visual Studio 2013 in SharePoint 2013
Steps to create Site column using Visual Studio 2013 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 Create button as shown in the fig below:

 Steps to create Site column using Visual Studio 2013 in SharePoint 2016
Steps to create Site column using Visual Studio 2013 in SharePoint 2016

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:

 Steps to create Site column using Visual Studio 2015 in SharePoint 2016
Steps to create Site column using Visual Studio 2015 in SharePoint 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:

 Steps to create Site column using Visual Studio 2013 in SharePoint 2013
Steps to create Site column using Visual Studio 2013 in SharePoint 2013

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.

 Steps to create Site column using Visual Studio 2013 in SharePoint 2013
Steps to create Site column using Visual Studio 2013 in SharePoint 2013

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.

 Steps to create Site column using Visual Studio 2013 in SharePoint 2013
Steps to create Site column using Visual Studio 2013 in SharePoint 2013

Create a Site Column using PowerShell:

Here we will discuss how we can create a site column using PowerShell.

Below is the PowerShell command to create a site column. 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:

 Steps to create Site column using Visual Studio 2013 in SharePoint 2013
Steps to create Site column using Visual Studio 2013 in SharePoint 2013

Create Site Column using Visual Studio 2013:
Follow below steps to create a site column using Visual studio 2013.

Open Visual Studio 2013 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 give 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 the 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 Site Column template as shown in the fig below:

 Steps to create Site column using Visual Studio 2013 in SharePoint 2013
Steps to create Site column using Visual Studio 2013 in SharePoint 2013

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 add a site column with Single line of text type. Here I have change a few things, like instead of 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:

 Steps to create Site column using Visual Studio 2013 in SharePoint 2013
Steps to create Site column using Visual Studio 2013 in SharePoint 2013

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.

 Steps to create Site column using Visual Studio 2013 in SharePoint 2013


Steps to create Site column using Visual Studio 2013 in SharePoint 2013Now 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:

 Steps to create Site column using Visual Studio 2013 in SharePoint 2013
Steps to create Site column using Visual Studio 2013 in SharePoint 2013

Hope this post will be helpful to you.


You May Also like the Following SharePoint Online Tutorials:

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 →