Dragan Panjkov
Dragan Panjkov

Reputation: 4332

How do I add custom column to existing WSS list template

I need to use feature stapler to add some text columns to Posts list inside OOTB blog site definition. I plan not to use site columns, but only to add those columns to list (I don't use site columns because I have multiple site collections and there will be only one Posts list per site collection, so site columns are not very reusable in this case). My question is: How do I achieve this?

Upvotes: 0

Views: 2337

Answers (3)

Dragan Panjkov
Dragan Panjkov

Reputation: 4332

I cannot see what benefit I have from creating custom action for posts list. Both posts are helpful, but I'll probably create custom feature for that.

Upvotes: 0

Ganesha
Ganesha

Reputation: 1531

The best solution is to create a hidden custom action for Posts List. I'm posting a simplified version here

Elements.xml:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction Id="XXXXXXXX"
   RegistrationType="List"
   RegistrationId="101"
   Rights="Open"
   Location="ViewToolbar"
   Sequence="110"
   Title="Hidden Settings Button"
   ControlAssembly="MyLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXXXXX"
       ControlClass="MyLib.MyClass"
   />

    <FeatureSiteTemplateAssociation Id="XXXXXXX" TemplateName="YOUR_BLOG_SITE_TEMPLATE_NAME" />

MyClass.cs:

  [DefaultProperty("Text")]
  [ToolboxData("<{0}:MyClass runat=server></{0}:MyClass>")]
  public class MyClass : WebControl
  {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            SPList list = SPContext.Current.List;
            if (list != null)
            {
                 list.Fields.Add(XXX, XXX, XXX);
                 list.Update();
            }    
        }    
   }

Upvotes: 2

Nat
Nat

Reputation: 14305

Perhaps you can create a feature that uses the object model from the feature receiver to add (and remove as appropriate) the columns to just the specific list when the feature is activated.

I would use the XML Schema approach for creating the columns in order to ensure the same GUID for each column. See

Upvotes: 2

Related Questions