Sunday, September 11, 2011

How content approval works in Sharepoint

As you know there is possibility to enable Content Approval option for lists and document libraries in Sharepoint. In this article I will show how it works internally. There will be also examples logged from list items event receiver, so you can use this article as a reference when you will need to know what events are get fired during the work with documents or pages.

Content approval can be enabled in List Settings > Versioning Settings > Content Approval section:

image

Lets first check what happens when it is turned off (option No on the picture above). For our examples we will enable versioning so it will use both minor and major versions (it can be done on the same Versioning Settings page):

image

For tests I created simple item event receiver which logs all events in the file on file system (of course I granted permissions to the folder where log file is stored to the account under which event receiver works):

   1: public class EventReceiver1 : SPItemEventReceiver
   2: {
   3:     public static object lockObj = new object();
   4:  
   5:     private void log(SPItemEventProperties p)
   6:     {
   7:         lock (lockObj)
   8:         {
   9:             File.AppendAllText("c:/temp/log.txt", string.Format("{0}:\t{1}\t{2}\t{3}\t{4}{5}",
  10:                 DateTime.Now, new StackTrace().GetFrame(1).GetMethod().Name,
  11:                 p.ListItem.File.CheckOutType, p.ListItem.File.MajorVersion,
  12:                 p.ListItem.File.MinorVersion, Environment.NewLine));
  13:         }
  14:     }
  15:  
  16:     public override void ItemAdding(SPItemEventProperties p) { log(p); }
  17:  
  18:     public override void ItemUpdating(SPItemEventProperties p) { log(p); }
  19:  
  20:     public override void ItemCheckingIn(SPItemEventProperties p) { log(p); }
  21:  
  22:     public override void ItemCheckingOut(SPItemEventProperties p) { log(p); }
  23:  
  24:     public override void ItemAdded(SPItemEventProperties p) { log(p); }
  25:  
  26:     public override void ItemUpdated(SPItemEventProperties p) { log(p); }
  27:  
  28:     public override void ItemCheckedIn(SPItemEventProperties p) { log(p); }
  29:  
  30:     public override void ItemCheckedOut(SPItemEventProperties p) { log(p); }
  31: }

It logs information about event type (new StackTrace().GetFrame(1).GetMethod().Name), also about check out type and minor and major versions.

For testing we will upload document in OTB Document doclib, set its metadata then edit metadata and publish major version. Lets see the log without content approval:

  9/10/2011 1:54:56 PM:    ItemAdded    Online    0    1
// was uploaded  
 

9/10/2011 1:56:23 PM:    ItemUpdating    Online    0    1
9/10/2011 1:56:23 PM:    ItemUpdated    Online    0    1
9/10/2011 1:56:23 PM:    ItemUpdating    Online    0    1
9/10/2011 1:56:23 PM:    ItemCheckingIn    Online    0    1
9/10/2011 1:56:23 PM:    ItemUpdated    None    0    1
9/10/2011 1:56:23 PM:    ItemCheckedIn    None    0    1

// metadata was set  
 

10.9.2011 13:58:31:    ItemCheckingOut    None    0    1
10.9.2011 13:58:31:    ItemCheckedOut    Online    0    2

// was checked out  
 

9/10/2011 1:58:39 PM:    ItemUpdating    Online    0    2
9/10/2011 1:58:39 PM:    ItemUpdated    Online    0    2

// metadata was changed  
 

9/10/2011 1:59:43 PM:    ItemUpdating    Online    0    2
9/10/2011 1:59:43 PM:    ItemCheckingIn    Online    0    2
9/10/2011 1:59:43 PM:    ItemCheckedIn    None    1    0
9/10/2011 1:59:43 PM:    ItemUpdated    None    1    0

// was published with major version  

In the table above “metadata was set” step occurred when user clicks Save button on the dialog which occurs after upload:

image

Then it was checked out (check out status changed from None to Online), title in metadata was changed (as you can see in the log, version was changed from 0.1 to 0.2) and then major version was published:

image

After that version of the document became 1.0 and check out status was set to None again. If we check document’s version history it will look like this:

image

Everything is clear here. Now lets see what happens with the same steps but with content approval enabled:

  9/10/2011 1:44:37 PM:    ItemAdded    Online    0    1
// was uploaded  
 

9/10/2011 1:44:51 PM:    ItemUpdating    Online    0    1
9/10/2011 1:44:51 PM:    ItemUpdated    Online    0    1
9/10/2011 1:44:51 PM:    ItemUpdating    Online    0    1
9/10/2011 1:44:51 PM:    ItemCheckingIn    Online    0    1
9/10/2011 1:44:51 PM:    ItemUpdated    None    0    1
9/10/2011 1:44:51 PM:    ItemCheckedIn    None    0    1

// metadata was set  
 

10.9.2011 13:45:11:    ItemCheckingOut    None    0    1
10.9.2011 13:45:11:    ItemCheckedOut    Online    0    2

// was checked out  
 

9/10/2011 1:45:25 PM:    ItemUpdating    Online    0    2
9/10/2011 1:45:25 PM:    ItemUpdated    Online    0    2

// metadata was set  
 

9/10/2011 1:45:53 PM:    ItemUpdating    Online    0    2
9/10/2011 1:45:53 PM:    ItemCheckingIn    Online    0    2
9/10/2011 1:45:53 PM:    ItemCheckedIn    None    0    2
9/10/2011 1:45:53 PM:    ItemUpdated    None    0    2

// was checked in with major version  
 

9/10/2011 1:48:01 PM:    ItemUpdating    None    0    2
9/10/2011 1:48:01 PM:    ItemUpdated    None    1    0

// was approved  

Everything the same before “metadata was set” step. The difference is in what happened after publishing of the major version (in bold). Instead of setting version to 1.0, item’s version was set to 0.2, despite of the fact that major version was explicitly selected in the check in dialog:

image

After that document occurred with Approval Status = Pending in the document library:

image

And in its version history there are only minor versions:

image

So this is what happed when Content approval is enabled in document library: when you check in the document with major version – it will be published with minor version anyway. That’s why it is not visible to the users yet – Sharepoint just reuses another mechanism for that: minor versions of the documents are not visible to end users.

In order to get real major version we need to perform one more step – approve the document:

image

And as you can see in the log, major version was set to 1.0 after that.

One more note which I would like to add: for Pages there is Publish button in the ribbon. If page is published via this button it gets major version immediately. However if it is published via check in functionality – then it became Pending as in the case with the document.

That’s all I would like to describe here. Hope this article will bring you better understanding of the internal publishing mechanisms in Sharepoint.

No comments:

Post a Comment