SharePoint Development and Programming
Add a new site column with a custom field type
Question:
I have created a custom feature that adds a new column (field) to the site column library (field collection). I have done with the following code through the feature activated event:
SPFieldCollection fieldcol = mySite.Fields;
fieldcol.Add(
"newColumn", SPFieldType.Text, false);
SPField column = mySite.Fields["newColumn"];
column.Group =
"newGroup";
column.Update();
I have also seperatly created an empty SharePoint project (through visual studio extensions). With this project I have added a field control. This field control creates a new field type (which I have called 'newType').
In this same project I want to add a new site column (as above) with a field type of that just created.
Does anyone know how I can do this?
Lucy
Answer1:
your question is not clear. your Column type is Text. now what you are looking for.
if you want to create a FieldType like available field type "Single Line of Text, Multiple Line of Text, Numeric" etc then you will have to write a Custom SPFieldType. please clarify what you want to achieve.
Answer2:
If I understand correctly, you want to add a custom field (of type 'newType') to mySite.Fields collection.
I think this should work:
Code SnippetAnswer3:
Yes that is what I want to do, but where can I put this code?
Lucy
Answer4:
I can also easily do this through CAML using the <field> element. My problem is, that I do not know where I can put any of this code in my field control feature?
Lucy
Answer5:
Hi Lucy,
You will need to add a feature stapler that is stappled to your site definition. This will then activate the feature on any site that are created using your site definition.
Have a look at this blog post from Chris Johnson that details how to do this
http://blogs.msdn.com/cjohnson/archive/2006/11/01/feature-stapling-in-wss-v3.aspx
Cheers
For more useful SharePoint tips, check out my blog at http://statto1974.wordpress.com
Answer6:
I'm not using a site definition.
Lucy
Answer7:
Can I add the c# code to one of the BaseFieldControl methods? I have tried adding code to change the titel of the site in the CreateChildControls method but I saw no change.
Lucy
Answer8:
Could you give us a quick overview of what you are trying to achieve.
This will hopefully give us provide an accurate answer
Cheers
For more useful SharePoint tips, check out my blog at http://statto1974.wordpress.com
Answer9:
I open up visual studio and select new - project. Under the project 'SharePoint', I select the template 'Empty'. I then right click the project name in the solution explorer, and click 'Add - New Item'. In this dialog I select the category 'SharePoint' and then select the template 'Field Control'. I then build and deploy the solution. In the solution explorer, I open the folders Template and then XML. I then open the file fldtypes_FieldControl1.xml. In this file I change the TypeName, TypeDisplayName and TypeDescription elements to the appropriate name and description of my custom field type. (I have called my custom field type 'NewType') I then build and deploy this solution again.
In this same project I want to add a new column (field) to the site column library (field collection) of type - 'NewType'.
Is this clearer?
I have found the following MSDN article that looks like it may give me more of an understanding:
http://msdn2.microsoft.com/en-us/library/bb861799.aspx
Lucy
Answer10:
You could create a new "List Definition" project that references your new field type.
Once you have deployed your new field control it will be available to add as a column in any list or libraries you have deployed it to.
For more useful SharePoint tips, check out my blog at http://statto1974.wordpress.com
Answer11:
I have solved my problem!
I created an Empty SharePoint project in VS. I then added a field control. I deleted the fldtypes_*.xml file that is automaticaly created in the projects Templates\XML folder. I created a new fldtypes_*.xml file directly under the project that looks like this:
Code SnippetAnswer12:
after create custom field type? how to create sitecolumn for this custom field using feature ?
Answer13:
When the custom field type is created and the xml file is put in the correct folder (\12\TEMPLATE\XML), the custom field type can immediatly be used in the SharePoint site. When my feature is activated the xml file of the feature calls my C# class library and specifically the FeatureActivated event. In this event, a new field is created that is added to the sites field collection. The new field being created specifies the field type and display name of the new field, as shown:
//New field created with the specified field type
SPField newTypeField = mySite.Fields.CreateNewField("CustomFieldType", "CustomFieldTypeColumn");
//The field is added to the specified group
newTypeField.Group =
"CustomFieldTypeGroup";
//The field is added to the sites field collection (i.e - it will be added to the Site Column Gallery)
mySite.Fields.Add(newTypeField);
//The site is updated in order for the effects of the code above to take place immediatly
mySite.Update();
Answer14:
my question create custom site column as xml base not code base. instead using code below
//New field created with the specified field type
SPField newTypeField = mySite.Fields.CreateNewField("CustomFieldType", "CustomFieldTypeColumn");
//The field is added to the specified group
newTypeField.Group =
"CustomFieldTypeGroup";
//The field is added to the sites field collection (i.e - it will be added to the Site Column Gallery)
mySite.Fields.Add(newTypeField);
//The site is updated in order for the effects of the code above to take place immediatly
mySite.Update();
I want to use xml instead of code above? Can someone point me to the schema to use for custom field?
Related posts
Powered by BlogEngine.NET 1.2.0.0
Sponsor