Alex
Alex

Reputation: 14618

Create list upon feature installation in Sharepoint Server 2010

I have a Sharepoint feature that includes a settings list template, which is read by other features. The list must have a particular name for the whole thing to work, so ideally I would rather have a list created instead of a list template.

I tried playing with the EventReceiver class, by overriding FeatureInstalled method. It receives a parameter of SPFeatureReceiverProperties type. I've looked through documentation, and saw that one property, UserCodeSite, refers to the SPSite where the feature is installed, if it is a Site scoped solution, which it is in my case. That way I wanted to write a piece of code that would create a specific list from the list template included in the feature.

SPWeb_object.Lists.Add(listName, "", SPListTemplate_object)

However the property is null, as well as the Feature property. So I can't get the SPSite object, and I can't get the SPWeb object.

Any other ideas?

Upvotes: 0

Views: 1698

Answers (2)

Jason Weber
Jason Weber

Reputation: 1522

List creation is a very common requirement. It's often best to create a list instance upon activation rather than installation, as suggested by another poster. You can access the desired activation scope (the SPWeb or SPSite in which the feature is activated) like so:

SPWeb web = (SPWeb)properties.Feature.Parent;

or

SPSite site = (SPSite)properties.Feature.Parent;

Hope this helps.

Upvotes: 1

Vojtech Nadvornik
Vojtech Nadvornik

Reputation: 351

You can create a new list instance ListInstance construct. See this: http://msdn.microsoft.com/en-us/library/ms476062.aspx The list will be created upon activating that particular feature. For automatic activation of a feature you can use feature stapling. See this http://msdn.microsoft.com/en-us/library/ff648422.aspx

Upvotes: 2

Related Questions