IsPublishingWeb() - what does it REALLY check for?
-
Tuesday, January 06, 2009 9:37 AM
From the MSDN documentation on IsPublishingWeb():
"This method checks the SPWeb instance to determine if the 'Publishing' feature has been activated on it."
This statement is incorrect (or at the very least it needs expanding on):The below code, placed in feature receiver stapled to site creation via FeatureSiteTemplateAssociation, may error with a "feature already activated" error. (Sometimes the code will succeed, sometimes it will fail, depending on the timing of background events in SharePoint).
// only execute code if IsPublishingWeb returns false if (!PublishingWeb.IsPublishingWeb(web)) { // 'Publishing' Feature GUID const string PUB_WEBFEATUREID = "22A9EF51-737B-4ff2-9346-694633FE4416"; // This line of code may result in a "feature already activated" error!! web.Features.Add(new Guid(PUB_WEBFEATUREID)); web.Update(); }
Regardless of whether the provisioning order between code and onet.xml etc. is correct or incorrect, the above code should never error if the statement in the documentation were true.
You are therefore left with the question - what does IsPublishingWeb() actually check?? I can only assume it somehow checks whether the site is fully provisioned for publishing somehow, rather than just checking whether the publishing feature has been activated as the article states.
Whatever, this topic needs expansion, as it is clearly not just checking the Publishing feature activation.- Edited by Johnny Dogbert Wednesday, January 07, 2009 9:47 AM
All Replies
-
Wednesday, January 07, 2009 9:44 AM
Update.
Using Reflector, we see that IsPublishingWeb calls GetPublishingWeb behind the scene, which in turn calls a method called verifyPublishingWeb().
Unfortunately, Reflector goes quiet at that point.
public static bool IsPublishingWeb(SPWeb web) { CommonUtilities.ConfirmNotNull(web, "web"); return CacheManager.GetManager(web.Site).ObjectFactory.CreateObject(GetPublishingWeb(web)).IsArea; } public static PublishingWeb GetPublishingWeb(SPWeb web) { return new PublishingWeb(web); } private PublishingWeb(SPWeb web) { CommonUtilities.ConfirmNotNull(web, "web"); this.currentWeb = web; verifyPublishingWeb(web); } private static void verifyPublishingWeb(SPWeb web) { }
-
Thursday, January 08, 2009 10:10 AMUpdate:
What's really weird is that I have found that if I remove the call to IsPublishingWeb() entirely, then all my subsequent publishing web specific code operations (starting of course with GetPublishingWeb()) all work perfectly.
So I've just implemented my own check that the Publishing feature is activated, and got rid of the IsPublishingWeb call. Now everything works perfectly.
That still doesn't answer what's going on with IsPublishingWeb() though.
There are two main remarks in the documentation:
"Set to True if the SPWeb class supports being wrapped in a PublishingWeb instance. Otherwise, it is set to False."
Well, I know it does support being wrapped in a PublishingWeb object, as my subsequent GetPublishingWeb() call works just fine.
"This method checks the SPWeb instance to determine if the 'Publishing' feature has been activated on it."
I also know that the publishing feature has been activated because my custom code checks for it.
So why is it still returning false??
My advice is not to use this method until MS either document it properly or fix a possible bug, as it not doing what it says on the tin right now.- Marked As Answer by Johnny Dogbert Friday, April 03, 2009 9:17 AM
- Unmarked As Answer by Johnny Dogbert Friday, April 03, 2009 9:17 AM
-
Friday, January 09, 2009 11:20 PMI ran into some weird stuff with this method as well.
My guess is
A) it's checking to see if the PublishingWeb.PagesListId is not null.
B) Creating a publishing site is an asynchronous act. My code frequently got different results back on whether it was a publiching site or not depending on where the site creation process was when I happened to try and do something with it.
Either way, proceed with caution!

