0

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?
1
  • im not sure what you are asking. what is wrong with having to adding site columns to a list Jul 11 '12 at 16:28
0

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();
            }
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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