I would like to get all the moderation status of a list item in a deleting event. I have enabled 'versioning'.
If the item approved once, the item should not be deleted, this is my requirement.
In Item deleting event you can write code like this
SPListItem item = properties.ListItem;
string Status =item[item.Fields.GetFieldByInternalName("_ModerationStatus").Id]
if (Status == "Approved")
{
properties.Cancel = true;
properties.ErrorMessage = "Cannot delete data";
}
properties.Cancel
is obsolete. You can use properties.Status
and set it with the SPEventReceiverStatus
enumeration.
I believe the values can be found in this enum:
Microsoft.SharePoint.SPModerationStatus
public enum SPModerationStatusType
{
Approved,
Denied,
Pending,
Draft,
Scheduled,
}
As to preventing it from being deleted once it's approved, maybe using permission levels are more suitable than implementing an event receiver ?
There is a property on SPListItem called HasPublishedVersion
Gets a Boolean value that specifies whether a published version of the item exists.
That will probably be your best bet.
Here is a code snippet you could use inside your ItemDeleting event:
if (properties.ListItem.HasPublishedVersion)
{
// don't allow delete
properties.Status = SPEventReceiverStatus.CancelWithError;
properties.ErrorMessage = "Cannot delete items with published versions";
}
With this code, I've used properties.Status
instead of properties.Cancel
because it is obsolete in SP2013. If you are using SP2010, then use properties.Cancel
.
Originally I thought you could simply check the version number? If it is >= 1 then it has been approved at some point. Draft versions increment the minor portion while published versions increment the major portion. This could be a backup option.
For example: Version 0.16 has never been published. Version 3.3 has been published 3 times.
If you want to check the version number, the SPListItem.Versions property returns a collection of item versions. You can index the collection like this var latestVersion = versionColl[0];
and check the number of the most recent version using the VersionLabel property
By the way if you came here for the same reason I did: SharePoint Online updated the internal name of the approval status for REST api to OData__ModerationStatus ( was just _ModerationStatus in SP2013.