Creating site column without using features
-
16-10-2019 - |
문제
I am trying to create a site column in VS2010 without using features. Is this possible? If yes, please let me know where I can start looking.
I don't want to use feature because feature comes with its own maintainability headache which I want to avoid as much as possible. I want to be able to individually install/update/delete each site column that I create in any given environment.
해결책
If you are just trying to avoid using declarative xml to create the column, you can write C# code to create the column. But to deploy to your site through a wsp, it will still need to be packaged in a feature.
You could write a console application that runs on the server and executes the code to create the column. However, this seems like more of a maintainability headache than creating a feature.
Either way, the code would look something like this:
using(SPSite site = new SPSite("url-of-your-site"))
{
using(SPWeb web = site.RootWeb)
{
web.Fields.Add("ColumnName", SPFieldType.Text, true);
}
}
You could also create a Powershell script to add the column. Something like:
$site = Get-SPSite "your-site-url"
$web = $site.RootWeb
$column = '<Field Name=...' +
'ID=...' +
etc. +
'></Field>'
$web.Fields.AddFieldAsXml($column)
But, really features aren't that bad once you accept that feature activation is the way to programmatically add elements to a SharePoint site.
다른 팁
You can create columns in three ways:
- By using the SharePoint user interface
- By using the SharePoint object model
- By deploying a Feature that installs the column based on an XML definition file