Automatic Approval of Publishing Pages

Recently I got the time to look further into the process of automatic approval of publishing pages in Office SharePoint Server.

In my previous attempt, I had some problems making the approval work properly, and it seemed a little unstable. The method I used looked something like this:

// Get the SPListItem from eventhandler properties and pass it on
SPListItem item = newsPages.Items[this.itemId];

// OK item is found, proceed with approval process
SPModerationInformation modInfo = item.ModerationInformation;
if (modInfo.Status.Equals(SPModerationStatusType.Pending) && !isTopstory) // automatically approve when some criteria is met.
{
    // Check Expiration Date
    DateTime publishingEnd = (DateTime)item["PublishingExpirationDate"];
    if (DateTime.Now < publishingEnd)
    {
    modInfo.Status = SPModerationStatusType.Approved;
    modInfo.Comment = "Newspage was automatically approved by the system";
    }
    else
    {
        modInfo.Status = SPModerationStatusType.Denied;
        modInfo.Comment = "Newspage has expired!";
    }
    item.Update();
}

Notice that this requires the approval rights in SharePoint. I use the COM+ approach to elevate rights.
This seemed to work ok, but only on newly created items. Whenever a publishing page was edited and submitted for approval, the process would not kick in properly.

Now, I had a look into the Microsoft.SharePoint.Publishing assembly, which seems to offer some other way of accessing the scheduling and publishing of an item.

SPList newsPages = site.Lists[some SPList id];
SPListItem item = newsPages.Items[some SPListItem id];

if (item != null)
{
    if (ScheduledItem.IsScheduledItem(item))
    {
        ScheduledItem thisScheduled = ScheduledItem.GetScheduledItem(item);
        thisScheduled.Schedule("Newspage was automatically approved by the system");
    }
}

A much cleaner approach, and it seems to work properly.

Now, with some backsight, I just wonder why the other approach is working at all, as the SPModerationinformation is taken from Microsoft.SharePoint and has nothing to do with the publishing features of Office SharePoint Server. I'll leave this to someone else to explain.

/Steinhof

Leave a Reply