PANVEGA’s Blog

DotNet Development, SharePoint Customizing, Silverlight, MS Infrastructure and other tips and tricks

How to create a new List Item Instance with a Feature

Posted by PANVEGA on January 18, 2009

List Instances

A list instance is a list created by using a list template. When creating a list this way, you can override some of the list properties set in the template, and you can include data rows that will also be created as list items in the list. In this example we gonna create a Custom List Instance called BannerList.

Allowed Scopes

The scopes to which a feature can be deployed, are dictated by the types of elements included in it. A feature with list instance elements can only be deployed to Web Site scopes, since it creates a list in a specific web site.

First create a feature.xml, which I expleined in a previous post. In the next stepcreate a ListInstace.xmlxml file for your List feature.

<?xml version=”1.0″ encoding=”utf-8″ ?>
<Feature xmlns=”http://schemas.microsoft.com/sharepoint/&#8221; Id=”A4E3B5AB-857C-42d1-961D-4B2A6A2D43A7″
Title=”ListInstance”
Description=”The feature create a new Custom List Instance”
Hidden=”FALSE”   Scope=”Web”  Version=”1.0.0.0″>
<ElementManifests>
<ElementManifest Location=”ListInstance.xml” />
</ElementManifests>
</Feature>

Notes about this feature manifest:

  • The title of the feature is My List Instance Feature.
  • It will be deployed as a Web Site feature, since it’s Scope value is Web.
  • It references a single element manifest file: ListInstance.xml.

ListInstance.xml

<?xml version=”1.0″ encoding=”utf-8″ ?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”&gt;
<ListInstance
Id=”2242AE7D-D7B8-4dd5-A03B-F1B3AC8FBEA8″
FeatureId=”00bfea71-de22-43b2-a848-c05709900100
TemplateType=”100″
Title=”BannerList”
Url=”Lists/BannerList”  OnQuickLaunch=”true”
Description=”Contains all Items for the banner function”/>
</Elements>

Note: The FeatureId Attribute value you can get when e.g. creating a new Custom List Instance manually. You find the right ID in the URL:

http://localhost/_layouts/new.aspx?FeatureId={00bfea71-de22-43b2-a848-c05709900100}&ListTemplate=100

Instance of a Image Library (Template 109):

<ListInstance
Id=”84BA31F1-901E-4e3c-829D-20DACA142940″
FeatureId=”00bfea71-52d4-45b3-b544-b1c71b620109
TemplateType=”109
Title=”BannerImages”
Url=”Lists/BannerImages”
OnQuickLaunch=”True”
Description=”Contains all Images for the BannerList”/>

The element manifest file can have any name you wish (in this example it’s called ListInstance.xml), but it’s root element must be <Elements>. Inside this root element, you can place any number of feature element descriptions. In this example I will present the use of the <ListInstance> element which is used to deploy Lists to SharePoint web sites.

This examples creates a list named My List with the URL Lists/MyList. Here is a short explanation of the attributes of the <ListInstance> element:

  • FeatureId – (optional) The GUID of the feature where the list template was defined. If this attribute is not present, SharePoint will assume that the list template was defined in this feature.
  • Title – (required) Title of the list.
  • Description – (optional) Description for the list.
  • Id – (required) Unique integer identifier for this list instance. It must be unique across this feature.
  • TemplateType – (required) The integer identifier of the list template. This attribute must map to the Type attribute of the list template definition (in the ListTemplate element).
  • Url – (required) Site-relative URL where the list will created.

Other Attributes

There are some other less used attributes that one can use in the <ListInstance> element, which are listed below:

  • OnQuickLaunch – (optional) True if you want the list to be displayed in the QuickLaunch menu (local navigation).
  • QuickLaunchUrl – (optional) Specifies the URL of the view page to open for the list through QuickLaunch navigation.
  • RootWebOnly – (optional) True if the list is only created in the root web site of the site collection.

Here you find a nice List Instance Generatror

Creating lists using ListInstances is quite easy to do within a feature. However, deleting those lists again when deactivating that feature does not happen automatically (which is understandable, since you don’t want to lose all data in the list when accidentally deactivating the feature …). A feature receiver to do the job for you.  The receiver simple deletes all lists created by a ListInstance tag in the feature.

Including Data in the List Instance

When using this type of feature element, you can specify a set of items that will be included when the list is created. For that you must insert a <Data> element inside the <ListInstance> element, as shown below.

<?xml version=”1.0″ encoding=”utf-8″ ?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”&gt;
<ListInstance
FeatureId=”5DFD12AF-D0AA-4c63-8FB8-C49DB1191083″
Title=”My List”
Description=”List created with a feature.”
Id=”1036″
TemplateType=”100″
Url=”Lists/BannerList”>
<Data>
<Rows>
<Row>
<Field Name=”Title”>Item 1</Field>
<Field Name=”CustomField”>Custom Data</Field>
</Row>
<Row>
<Field Name=”Title”>Item 2</Field>
<Field Name=”CustomField”>New Custom Data</Field>
</Row>
</Rows>
</Data>
</ListInstance>
</Elements>

This example shows how to add two list items to the list instance. For each of the list items, two fields are updated: Title and CustomField.

More Information:

http://www.sharepointblogs.com/nicksevens/archive/2008/11/20/automatic-removal-of-lists-when-deactivating-listinstance-feature.aspx

http://msdn.microsoft.com/en-us/library/ms476062.aspx

http://geekswithblogs.net/djacobus/archive/2008/10/27/126265.aspx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: