Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a feature that will add 2 columns to site columns. However I need to add them to the list. Because its an existing development and the didnt start from scratch using feature versioning, I prefer to do the second part by code.

These are my fields

<Field ID="{65076B51-5D7C-4140-A5E9-F7D794351489}" Name="BudgetType" DisplayName="$Resources:SPNLSponsoring,Field_BudgetInternalType_Name;" Description="$Resources:SPNLSponsoring,Field_BudgetInternalType_Description;" Group="$Resources:SPNLSponsoring,Field_NationaleLoterijSponsoringColumns_Group;" Type="TaxonomyFieldType" ShowField="Term1033" StaticName="BudgetInternalType" Required="FALSE" Hidden="FALSE" />
  <Field ID="{5E4C6D2A-DA94-4547-80BD-D79C3DD15CB0}" Name="BudgetInternalTypeTaxHTField0" DisplayName="BudgetInternalTypeTaxHTField0;" Description="EventInternalTypeTaxHTField0;" Group="$Resources:SPNLSponsoring,Field_NationaleLoterijSponsoringColumns_Group;" Type="Note" StaticName="EventInternalTypeTaxHTField0" Hidden="TRUE" CanToggleHidden="TRUE" Required="FALSE" />
  1. How can I add them programatically to the list?
  2. How can I modify one view to add one of these columns?
share|improve this question
    
im not sure what you are asking. what is wrong with having to adding site columns to a list –  Sebastien Stettler Jul 11 '12 at 16:28

2 Answers 2

up vote 0 down vote accepted

the first part is easy, just use the AvailableFields collection fro the SPWeb object like this:

/// <summary>
        /// Adds existing site columns to an existing list and then modifies one view to add one of the new site columns
        /// </summary>
        /// <param name="web"></param>
        private void AddFieldsAndModifyView(SPWeb web)
        {
            SPList list = web.Lists["Budgets"];
            if (!list.Fields.ContainsField("BudgetInternalType"))
            {
                list.Fields.Add(web.AvailableFields["BudgetInternalType"]);
                list.Update();
            }
share|improve this answer

here is a snippet followed by some documention for second part

SPList list = web[listname]
SPView view = list.Views[viewname]
view.ViewFields.Add(col1)
view.ViewFields.Add(col2)
view.Update

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spviewfieldcollection_members

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.