Today I was trying to figure out how to change the welcome page of a SharePoint 2007 site programmatically. I couldn’t really find useful links on Google, so I figured I’d go and play around a bit with the object model.
Apparently the PublishingWeb object has a property called DefaultPage, which takes an SPFile object as a value. I ended up with the following code to change the welcome page:
using(SPSite siteCollection = new SPSite("http://yourserver")) { SPWeb targetWeb = siteCollection.AllWebs["YourWebName"]; SPFile newWelcomePage = null; // Change the code here to set the SPFile object to your new welcome page if(PublishingWeb.IsPublishingWeb(targetWeb)) { PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(targetWeb); publishingWeb.DefaultPage = newWelcomePage; // this sets the new welcome page publishingWeb.Update(); } }
That should do the trick . I hope this post is useful for anyone who’s also trying to achieve the same thing!
Thx mate, you just saved me some trouble
Thanks..
Where does this code get posted and/or how to execute this code?
I’d suggest running this from a console application on a SharePoint server. Be sure to include references to Microsoft.SharePoint.dll and Microsoft.SharePoint.Publishing.dll, both to be found in [12]\ISAPI.
Is it possible to set this based on the logged in users role?
Thanks in Advance.