Tuesday, May 29, 2012

Manage SPListItem publishing programatically

Recently, I had to work on converting a regular SPList to have similar to a publishing feature. Like, have an item edited, review it and then publish the page for public.

I made the below modifications to the list settings to enable versioning of list items.
Go to the List Setting page and select "Versioning Settings" and set the options to the same as below.

 
This enables the list items in the list to be version controlled. The author can save a version of the list item, review it and publish it at any point of time or revert to its previously published version.

I wanted to do it programatically using a layouts page (application page) since we are using the application pages as the default display form of the particular content type.

For saving a new SPListItem, the code should look similar to this. This will save the list item content and assign the status to "Pending".

var item = SPContext.Current.ListItemitem["Title"] = SPHttpUtility.ConvertSimpleHtmlToText(this.txtTitle.Text, 255);
//this will set the status of the current item to "Pending"
item.Update();


To know how to get the CurrentListItem in an app page, please see my other post here

To publish the list item, the code would look like this.

if (SPContext.Current.ListItem.ModerationInformation != null)
{
    SPContext.Current.ListItem.ModerationInformation.Status = SPModerationStatusType.Approved;
    SPContext.Current.ListItem.ModerationInformation.Comment = String.Format("Published on {0} by {1}" 
                                                   DateTime.Now.ToString(), SPContext.Current.Web.CurrentUser.Email);
    SPContext.Current.ListItem.Update();
    
    //Added to refresh the page content and hide the publishing controls 
    Response.Redirect(SPContext.Current.ListItem.Url);
}
 
To revert a saved list item to it's previously published version, the code would look like below:

if (SPContext.Current.ListItem.ModerationInformation != null && SPContext.Current.ListItem.Level != SPFileLevel.Published)
{
    //Getting the last published version of the current item
    var lastPublishedVersionInfo = SPContext.Current.ListItem.Versions.Cast<SPListItemVersion>()
                                                                            .Where(v => v.Level == SPFileLevel.Published)
                                                                            .OrderByDescending(l => l.Created).Take(1);
    SPContext.Current.ListItem.Versions.RestoreByID(lastPublishedVersionInfo.First().VersionId);
 
    SPContext.Current.ListItem.ModerationInformation.Status = SPModerationStatusType.Approved;
    SPContext.Current.ListItem.ModerationInformation.Comment = String.Format("Reverted on {0} by {1}", 
                                                               DateTime.Now.ToString(), SPContext.Current.Web.CurrentUser.Email);
    SPContext.Current.ListItem.Update();
 
    Response.Redirect(BlogManager.GetBlogPostUrl(SPContext.Current.Web, SPContext.Current.List.ID, SPContext.Current.ListItem.ID));
}

Hope this helps some one or even myself somewhere in the future.

Thanks
Senthil S

No comments:

Post a Comment