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

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="http://schemas.microsoft.com/sharepoint/">
  <ListInstance
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" >
<Data>
  <Rows>
    <Row>
      <Field Name="ID">1</Field>
      <Field Name="Title">Machine Head</Field>
    </Row>
    <Row>
      <Field Name="ID">2</Field>
      <Field Name="Title">Rubber Soul</Field>
    </Row>
  </Rows>
</Data>
  </ListInstance>
</Elements>

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
add comment

1 Answer

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();
fieldsStringCollection.Add("Title");

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

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
2  
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: msdn.microsoft.com/en-us/library/ms414322.aspx –  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
2  
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
add comment

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.