Question

I have this strange situation, i have a site definition, that has a feature that after provisioning the site has code that runs, and one of the tasks is to activate the Publishing Feature in the newly crated web site.

After several tests, the final code it's like this:

        Guid publishingFeature = new Guid("94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb");
        if (props.Feature.Parent is SPWeb)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate ()
            {
                using (SPWeb web = props.Feature.Parent as SPWeb){
                    web.AllowUnsafeUpdates = true;
                    SPFeatureCollection wFeatureCol = web.Features;
                    wFeatureCol.Add(publishingFeature, true);
                    web.AllowUnsafeUpdates = false;
                }
            });

        }

So, i just run with elevated privileges and force the feature activation. If i don't force the feature, it throws an error, and doesn't activate anything.

But in the end when i look to the features of my site, i have the feature activated has expected, but it looks like that isn't "correctly" activated. I say this, because this website uses some features that need the publishing feature activated and i can't use them, for instance i can't see my master page applied to this sub site.

If i don't activate the feature in the code, and after provisioning the site if i activate the feature in the interface, or even if i leave the code alone and deactivate and activate the feature. Everything works as expected.

So it's different doing it by code, or in the interface? Or I'm missing something?

Thanks

Was it helpful?

Solution

Finally I found the problem/solution.

Just setting the "__PublishingFeatureActivated" value to true for the metadata of the web site, solve my problem. It doesn't make much sense to me, but in the end it just works.

Thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top