0

I'm trying to activate "SharePoint Publishing Server Infrastructure" and "SharePoint Server Publishing" by C# code. Here's my code:

SPSite siteCollection = new SPSite("http://MySitecollectionUrl");
SPWeb rootweb = siteCollection.RootWeb;
Guid guid1 = new Guid("f6924d36-2fa8-4f0b-b16d-06b7250180fa");
Guid guid2 = new Guid("94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb");
siteCollection.Features.Add(guid1, true);
rootweb.Features.Add(guid2, true);

After running the code, I met this error:

Error Microsoft.SharePoint.SPFeatureDependencyNotActivatedException: Dependency feature 'PublishingSite' (id: f6924d36-2fa8-4f0b-b16d-06b7250180fa) for feature 'PublishingWeb' (id: 94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb) is not activated at this scope

But when I check my Sharepoint site, the "Sharepoint Publishing Server Infrastructure" feature was activated by my code.

So, what am I doing wrong here?

UPDATE: I've tried activating the "SharePoint Publishing Server Infrastructure" by code, and then activated the "SharePoint Server Publishing" manually, and everything works OK. This issue really makes me confused. I really don't know why, are there any diffrences between activating feature by code and manually ?

1

Try this,

SPSite siteCollection = new SPSite("http://MySitecollectionUrl");
SPWeb rootweb = siteCollection.RootWeb;
siteCollection.AllowUnsafeUpdates = true;
rootweb.AllowUnsafeUpdates = true;
Guid guid1 = new Guid("f6924d36-2fa8-4f0b-b16d-06b7250180fa");
Guid guid2 = new Guid("94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb");
siteCollection.Features.Add(guid1, true);
siteCollection.Update();
rootweb.Features.Add(guid2, true);
rootweb.Update();
siteCollection.AllowUnsafeUpdates = false;
rootweb.AllowUnsafeUpdates = false;
0

I've tried the following code:

SPSite siteCollection = new SPSite("http://MySitecollectionUrl"); 
Guid guid1 = new Guid("f6924d36-2fa8-4f0b-b16d-06b7250180fa"); 
Guid guid2 = new Guid("94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb");
siteCollection.Features.Add(guid1, true);

//Get my SiteCollection again
SPSite siteCollection2 = new SPSite("http://MySitecollectionUrl");
SPWeb rootweb = siteCollection2.RootWeb;
rootweb.Features.Add(guid2, true); 

And it works!!
Still, I don't really understand why I had to get my site collection again ... It would be great if someone can tell me this.
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.