0

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();
    }
    

1 Answer 1

1

You have hardcoded the SPSite object url. It does not matter even wich site collection you activate this feature.

Use the in parameter spfeaturereceiverproperties like below to get your web (or site)

properties.Feature.Parent

If your feature is web scoped, Parent is a SPWeb object.

2
  • won't properties.feature.Parent return a subweb if it is activated on a subweb? The list is under the rootweb, so it would find the list if activated at the rootweb, but how can it find it if activated on subweb? Yet it seems to find it.
    – Bobh
    Commented Mar 9, 2019 at 11:58
  • I think this answers why all three activations can add to the same list, and if properties.feature.parent were used, it would not work in the case of subsites because that would point to the subsite itself. So I'll mark this as answered.
    – Bobh
    Commented Mar 9, 2019 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.