SPFolder and Content Approval

Question:How do I approve a folder in a document library that requires content approval using the SharePoint object model?

Answer:It may seem like a trivial question. After working with the SharePoint object model to create and approve files, it would make sense that any item or folder object would also contain methods needed to check in, publish, and approve them. Well after checking the members of the SPFolder and SPListItem classes on MSDN, I found that they don’t contain properties or methods to intuitively check in, publish, and approve them.

At first, I figured I could use the approve method from SPFile class since the folder object has a property to access the underlying item which has a a property to access the underlying file. Well, it turns out that file property of the item property of the folder object returns a null reference. So, I was back to square one.

As it turns out, the solution to this problem lies in working with the underlying item object. The underlying item object has a property that allows you to access the moderation information object (SPModerationInformation) which contains information about the approval status of the folder. You can then check the status and change as it needed by manipulating the moderation information object directly. Below is a method that I wrote to solve my problem (which was just to approve the folder):

private void ApproveFolder(SPFolder spFolder, SPDocumentLibrary spDocumentLibrary)
{
  if (spFolder.Exists && spDocumentLibrary.EnableModeration)
  {
    SPModerationInformation spModerationInformation = spFolder.Item.ModerationInformation;
    switch (spModerationInformation.Status)
    {
      case SPModerationStatusType.Pending:
        spModerationInformation.Status = SPModerationStatusType.Approved;
        spModerationInformation.Comment = "Got approval?";
        spFolder.Item.Update();
        break;
      default:
        break;
    }
    spModerationInformation = null;
  }
}

Obviously there is a lot more that you can do with the moderation information object. Hopefully, you will find my experience useful.

3 thoughts on “SPFolder and Content Approval”

  1. Hi;
    I wonder, where we should put this code in the sharepoint ?

    I am newbie of the sharepoint.

    Thanks in advance.
    Kind Regards.
    Özgür

Leave a Reply