0

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.

  • Have you considered declaring it as a record? – Bunzab Aug 18 '15 at 7:27
0

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";

  }
  • Hi Amit, it will trigger if the current status is 'Approved'. if the user edit the iten then it status will go to 'Pending' and this error won't appear. I need to get all the 'ModerationStatus' of a particular item. – user2395176 Aug 18 '15 at 7:52
  • Then use Item Updated event also so that whenever item is updated or updating you can get the Moderationstatus of that item also – Amit Kotha Aug 18 '15 at 7:57
  • In SP2013, properties.Cancel is obsolete. You can use properties.Status and set it with the SPEventReceiverStatus enumeration. – Lance Aug 19 '15 at 14:05
0

I believe the values can be found in this enum:

Microsoft.SharePoint.SPModerationStatus

public enum SPModerationStatusType
{
   Approved,
   Denied,
   Pending,
   Draft,
   Scheduled,
}

MSDN Source

As to preventing it from being deleted once it's approved, maybe using permission levels are more suitable than implementing an event receiver ?

0

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.