I have created a solution scoped to farm and a feature within it scoped to web. I am testing it to try to understand what it is really doing. It is supposed to create new items on an existing list under the top level web based on the name of a list that exists only at the top level. I also have two subsites that do not have a list of the same name as top level web list. I set the feature to not activate upon deployment so that it must be activated manually
After deployment, it added items to the list when it should not have. Saw that the feature deployed to top level web and both subwebs as well and all three were showing as inactive. I activated this feature on each of the three webs one by one and saw it add the same items to the list each time. That part seemed to work but I'm not sure why. So in total, including deployment, it added the same two records four times.
- Why did it add the items upon deployment instead of waiting for activation?
- How can it add items to the same list when the feature is activated on a subsite? I don't understand how it finds the top level list
If the list existed on one of the subsites instead of rootweb, how would the top level site find it if feature is activated at rootweb?
public override void FeatureActivated(SPFeatureReceiverProperties properties) { SPSite site = new SPSite("http://spsa"); SPWeb web = site.OpenWeb(); SPList list = web.Lists["Lecture55List"]; SPItem item = list.Items.Add(); item["Title"] = "Third Item"; item["Test"] = "Third Test"; item.Update(); SPItem item2 = list.Items.Add(); item2["Title"] = "Fourth Item"; item2["Test"] = "Fourth Test"; item2.Update(); }