SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to create a view on a list that you have created via the ListInstance element at the same time as you activate the Feature? For example I have the following as my Elements.xml:

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="">
Title="Favourite Albums" FeatureId="00BFEA71-DE22-43B2-A848-C05709900100"
  TemplateType="100" Id="11111"
  Description="Simple generic custom list sample to store albums."
  Url="Lists/MyCustomList" OnQuickLaunch="true"  RootWebOnly="false" >
      <Field Name="ID">1</Field>
      <Field Name="Title">Machine Head</Field>
      <Field Name="ID">2</Field>
      <Field Name="Title">Rubber Soul</Field>

Ideally I would like to create a view using CAML at the same time using something like a View element. I know I could use code inside the FeatureActivating event.

As yet I can't find any way to do this and it looks like it may not be possible. Would I have to use a ListTemplate element and a Schema.xml file? Any ideas? I am using Windows SharePoint Services 3.

share|improve this question
up vote 2 down vote accepted

You can easily create a view programmatically, using method SPViewCollection.Add.

Sample code:

var list = web.GetListFromWebPartPageUrl("/Lists/MyCustomList/AllItems.aspx");
var fieldsStringCollection = new StringCollection();

var view = list.Views.Add(
    "Test view",
    "<Where><Contains><FieldRef Name=\"Title\" /><Value Type=\"Text\">{FilterParam}</Value></Contains></Where>",
    30, // RowLimit
    true, // is paged
    false); // is default

This code should be added to FeatureActivated method.

share|improve this answer
This code will do it, but can I do it in CAML inside the elements.xml? – PeterM Sep 23 '11 at 13:19
No, CAML can do it only with Schema.xml and list templates. The only option with CAML is to create particular view on a page (it will be actually detached from the list instance). Btw, that's why we decided to move to programmatical creation of lists and views in our project at work. You can explore the Feature CAML schemas yourself if you don't believe: – Andrey Markeev Sep 23 '11 at 13:26
That is what I first thought after trying it out. So the ListTemplate element and Schema.xml is the way to go for CAML, otherwise the code approach. Many thanks for your answer. – PeterM Sep 23 '11 at 13:37
List template is not the same and it has many disadvantages, just for example, Calendar list created from custom list template will have 'Connect to Outlook' ribbon button disabled, because someone from MS had hardcoded '106' template in js. So, I recommend you the code approach. At least, try. Really, it is much more flexible, typo-prone, and doesn't have any disadvantages for most cases. – Andrey Markeev Sep 23 '11 at 13:45
Ok, I am convinced, use code! Thanks – PeterM Sep 23 '11 at 14:34

Your Answer


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.