I discovered a curious thing when writing a web part for SharePoint the other day. I had to check for an item in a list to have an approved status before pulling it into the selection of list items I was going to be working with. I discovered there are two ways to check for the approval status of an item as well as some interesting ways about how they worked.
The first option to use if (item.HasPublishedVersion) where item is an SPListItem. The second option is to use if (item.ModerationInformation.Status == SPModerationStatusType.Approved) again, where item is an SPListItem. The second option obviously gives you a little more flexibility since you can choose more than just approved when checking the status type. However, for the purpose of this post, we are going to just focused on approved items.
My first surprise came when I implemented the code above; made sure I had a few “Pending” items in my list and browsed to the page containing my web part. What do you know, even though the items were pending, there they were. Come to find out, if you have approver rights on your farm, even though you supposedly check the approval status, it still returns pending items. So, when you are testing code implementing this type of check, test your code with a non-approver account and also warn any users that are approvers, not to freak out if they see “pending” items.
Now, for my second surprise, once I approved an item and then went back and edited it (thus putting it back in a pending state) if I went back to the page with the web part, I still saw the previous version of that list item. I know, you’re going to say is versioning on? It actually wasJ, so I went and turned of versioning for that list. But, what do you know, I still was getting the same behavior with versioning off. So, apparently, even with versioning off on a list, there is still a “last approved version” there. I suppose this makes some sense, because if the edit was rejected, you would still need to roll back to the last version.
Now for a small rant. I can see HasPublishedVersion working this way…it’s just looking for some published version of the list item…ok…I can understand that. But ModerationInformation.Status == SPModerationStatusType.Approved having this behavior? I would expect this would actually look at the status that is displayed in the list and completely pull it out, even if there is an approved version. Ok, I’m done…but this is just something to look out for when using this particular code against an SPListItem.
One last caveat is that, although I’ve been working with SharePoint for the last 5 years, it has been on the administration/configuration/installation side of it and I am fairly new to development. So if someone reads this and has an explanation to what I observed or can tell me something I was doing wrong, I’m not too proud to take some correction regarding this issue, so please, comment if you have some further insight into this.