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

I'm trying to do a bulk page rename (NB. the file name e.g. default.aspx, not the displayed title)

foreach (PublishingPage page in pweb.GetPublishingPages())
{
    Console.WriteLine(page.Name);
    page.Name = GetNewNamingConventionForPage(page.Name); //error, it's read only
}

What gives? I can rename it manually in the pages library.

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

You need to set the "FileLeafRef" field of the underlying SPListItem for the publishing page. You need to first check out the file (or create it and then Update it for new sites), update the .ListItem["FileLeafRef"] property and then check it back in and publish it.

This code is to create a new publishing page and then change the file name. You have to run the .Update on the PublishingPage before setting the FileLeafRef otherwise you get an "object does not exist" error.

if (PublishingWeb.IsPublishingWeb(curWeb))
{
    PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(curWeb);
    PublishingPage pubPage = pubWeb.AddPublishingPage();

    pubPage.Title = "My New Page Title";
    pubPage.Update();

    pubPage.ListItem["FileLeafRef"] = "default";  //.aspx is added automatically
    pubPage.ListItem.SystemUpdate();  //SystemUpdate doesn't update the modified fields
    pubPage.CheckIn("");
    pubPage.ListItem.File.Publish("");
} 

Update: BaseName changed to FileLeafRef, BaseName is readonly and we can't update this field.

share|improve this answer
    
i've renamed them all by hand now, but i'll trust you that this works ;) –  Anonymous Apr 30 '10 at 13:16
add comment

Its beacuse PublishingPage.Name is a read-only property.

You could try:

page.ListItem("Name")

I have not tested this so do not know if this will do exactly what you need, but you definitely cannot set using the Name property.

share|improve this answer
    
this sounds right.. also note the difference between the name (filename) and the title.. –  Anonymous Mar 26 '10 at 21:43
    
doesn't work either. –  Anonymous Apr 26 '10 at 12:04
add comment
var url = oldUrl.Replace(oldFileName, newFileName);
publPage.ListItem.File.MoveTo(url);
share|improve this answer
1  
It would be great if you could add a little more explanation to your answer than just the code, otherwise it gets automatically flagged. Thanks. –  SPDoctor Feb 10 '12 at 14:42
add comment

Your Answer

 
discard

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