2

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

  • is the "SharePoint Server Publishing Infrastructure" is the site collection level activated? – A.Saéed Feb 9 '18 at 17:50
  • @AhmedSaéed Yes it's activated. In a initial phase i had the code to check that... – MckPT Feb 9 '18 at 23:11
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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