Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

How to do the following but with code? my page name is actnow.aspx

click the "Site Actions" menu bar, select "Site Settings" and then click on "Modify All Site Settings". Under the "Look and Feel" column is a link called "Welcome Page".

share|improve this question
    
PublishingPage publishingPage = pubWeb.AddPublishingPage("sharepointtest/justanothersite/Pages/new-default.aspx";, pageLayout); publishingPage.Update(); pubWeb.DefaultPage = publishingPage.ListItem.File; pubWeb.Update(); const string checkInComment = "Initial Check In"; publishingPage.CheckIn(checkInComment); SPFile pageFile = publishingPage.ListItem.File; pageFile.Publish(checkInComment); pageFile.Approve(checkInComment); pageFile.MoveTo(file.ListItemAllFields.ParentList.RootFolder.Url + "/default.aspx", true); pageFile.Update(); } –  Roxanne May 9 '11 at 19:48
1  
Is this the answer to your question or an edit? This code doesn't belong in a comment... –  Kit Menke May 9 '11 at 20:14

4 Answers 4

up vote 2 down vote accepted

What you want is PublishingWeb.DefaultPage - it has to point to an SPFile represented by a list item in your Pages library.

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.publishing.publishingweb.defaultpage.aspx

Combine this with Lori's suggestion for doing it in PowerShell.

share|improve this answer
    
Thanks. this helps –  Roxanne May 11 '11 at 16:21

There is a powershell script for this here: http://social.msdn.microsoft.com/Forums/en-US/sharepoint2010setup/thread/ecc0b2e4-8661-420a-bdf5-d7af13567be2. If you are wanting to do this for multiple webs that have the same file name in their pages libraries, I'm sure you could modify this script to enumerate through those sites and perform the same action.

share|improve this answer

Assuming "web" is an SPWeb for the site you're trying to change:

web.RootFolder.WelcomePage = "URL to desired page";

See http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfolder.welcomepage.aspx

share|improve this answer
    
I did both web.RootFolder.WelcomePage = "Pages/MyCustom.aspx"; and PublishingWeb.DefaultPage in my code but after provisioning a spweb following comes up. web_A/Pages/default.aspx Status: Checked in and viewable by authorized users. –  Roxanne May 13 '11 at 11:58
    
I deleted both oot default pages. SPFile file = web.GetFile("./pages/default.aspx"); file.Delete(); it is still showing up as default page. However, if i refresh and it's gone. buggie?? –  Roxanne May 13 '11 at 12:15
    
Probably. Do you mean in SharePoint Designer, the browser, or the SPWeb object? In all of those there may be issues with the cache. –  Andy Burns May 13 '11 at 13:04
    
spweb object. hmm. so whats the fix Andy? –  Roxanne May 13 '11 at 14:12
    
I'd suggest closing the SPWeb object and loading a new one. It does seem like a bug. –  Andy Burns May 17 '11 at 12:56

I would recommend using RootFolder.WelcomePage. However, there seems to be a strange issue where you have to actually get the Rootfolder object to do stuff, unless it won't be updated.

Alas, this doesn't work:

web.RootFolder.WelcomePage = "myHomepage.aspx";
web.RootFolder.Update();

But this will:

var folder = web.RootFolder;
folder.WelcomePage = "myHomepage.aspx";
folder.Update();
share|improve this answer
    
This behavior is due to the fact the SPWeb.RootFolder internally calls new SPFolder(currentWeb,"") and returns that. Thus in the first block you get a new SPfolder twice, and call update on a non-changed version. –  Dribbel Sep 20 '11 at 13:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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