Friday, May 6, 2011

Save a PublishingPage programmatically

We are trying to do edit and save page functionality for a publishing page programmatically. I was facing an issue where the page content is not getting updated when check-in the page programmatically. As we know the below code checks in and publishes any SharePoint item into the library.

SPContext.Current.ListItem.File.CheckIn("Page edited at: " + DateTime.Now,  
                                                SPCheckinType.MajorCheckIn);
SPContext.Current.ListItem.File.Publish("Published page at: " + DateTime.Now);

When I run the above code, the page gets checked in and gets published but the content is not getting updated in the page. To achieve that, I had to add the below code to save the contents of the page before check-in and publish the SharePoint item.

Microsoft.SharePoint.WebControls.SaveButton.SaveItem(SPContext.Current, 
                                         false"Saved at: " + DateTime.Now);

This one works fine for me. Hope this helps some one else too.

2 comments:

  1. Great post, many thanks.
    Currently I'm using:

    Microsoft.SharePoint.WebControls.SaveButton.SaveItem(SPContext.Current, false, "Saved at: " + DateTime.Now);

    so, how to get Check In Comments after saving list item?
    Thanks,
    Phu Dang

    ReplyDelete