Build a Feature based Solution to Deploy Site columns in SharePoint
I have got a bunch of site columns created in development environment and want to port them to Production environment with out manually creating em again. Let see step by step: creating feature based solution for SharePoint 2010 site columns using Visual Studio 2010.
1. Lets start by creating a new Visual Studio 2010 Project.
2. Choose “Empty SharePoint Project” as project type, under SharePoint 2010, give it a name, Here I’ve given “Crescent.Portal.SiteColumns.Feature”, and don’t forget to set the framework to .Net Framework 3.5 and click “Ok”.
3. Select the Project type, which ever applicable to you. Here I’ve selected farm solutions for the maximum control.
4. Now the project will look like the below one.
5. Lets add a new to our project by right clicking and then selecting “Add >> New Item” menu
6. Choose “Empty Element”, name it and click Add button. This will add new element.xml to our project.
7. We’ll add little more meaning to the feature by just renaming it to “SiteColumnFeature”
8. To create SharePoint site column feature, we need to add the field type schema to the Elements.xml file. How do we do that? well, There are many ways, We can use Visual Studio 2010 Server explorer to get the schema XML. But I’m a big fan of SharePoint Manager!(Get it from SharePoint Manager)
Open the SharePoint Manager, Navigate to the Source site where fields are already created Navigate till “Fields” node, select the desired field, click on Schema XML tab, copy the XML node <Field type> paste it inside element.xml file in the Visual Studio project. Highlighted is the SharePoint site column definition XML.
Repeat this for all fields. The Element.xml file was looking like this, once I copy-pasted all of my desired fields. Typical SharePoint site column definition:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="https://schemas.microsoft.com/sharepoint/">
<Field Type="Choice" DisplayName="Bpt-Departments" Required="FALSE" EnforceUniqueValues="FALSE" Indexed="FALSE" Format="RadioButtons" FillInChoice="FALSE" Group="Bpt Columns" ID="{ad2d13ee-e48e-4697-8823-8155d5fa3cfc}" SourceID="{03548e22-e4b5-4ae0-b325-12c584754e72}" StaticName="Bpt-Departments" Name="Bpt-Departments">
<Default>IT</Default>
<CHOICES>
<CHOICE>IT</CHOICE>
<CHOICE>Sales</CHOICE>
<CHOICE>Marketing</CHOICE>
<CHOICE>Production</CHOICE>
<CHOICE>Legal</CHOICE>
<CHOICE>Corporate</CHOICE>
</CHOICES>
</Field>
<Field Type="Number" DisplayName="Bpt-InvoiceNo" Required="TRUE" EnforceUniqueValues="FALSE" Indexed="FALSE" Min="1" Max="10000" Group="Bpt Columns" ID="{b81c7da6-1317-46fa-a32b-9f446c30b6e9}" SourceID="{03548e22-e4b5-4ae0-b325-12c584754e72}" StaticName="Bpt-InvoiceNo" Name="Bpt-InvoiceNo">
</Field>
</Elements>
10. We have our Field schema ready now. Next step: Add the element to the feature. Right click the Feature name (In our case: SiteColumnFeature) node in visual studio project, choose “View Designer” . This will launch Feature Designer, one of the brand new feature in SharePoint 2010.
11. Associate the element with the feature by moving it to the right.
12. With Feature Designer, We can set all additional properties to our feature like Activate on Default, Image, etc.
13. We are done creating site column feature for SharePoint 2010. Lets Build, package and deploy the project. Deploy site column feature by right clicking the project and select “Deploy”.
14. Verify the deployment by going to Site action >> Site settings >> Manage Site Features
15. verify the solution “Crescent Site column Feature” is deployed and activated
16. See them under Site columns!
See the new columns deployed!