0

I am using the below code to checkout the document in a library when an item was updated. The library has versioning and content approval enabled. My problem is that when you check in the document to publish a new version the receiver checks out the document. Is there a way to code that if the item is awaiting approval then to not check it out?

/// <summary>
/// List Item Events
/// </summary>
public class EventReceiver1 : SPItemEventReceiver
{
   /// <summary>
   /// An item was added.
   /// </summary>
   public override void ItemUpdated(SPItemEventProperties properties)
   {
       base.ItemUpdated(properties);
       SPListItem item = properties.ListItem;
       item.File.CheckOut();
       item.SystemUpdate(false);
   }


}
1

Yes you can check what is the status of the document in the library.

Check this code:

SPListItem listItem = GetListItem();
var moderationInformation = listItem.ModerationInformation;
if (moderationInformation != null)
{
    if (moderationInformation.Status == SPModerationStatusType.Approved)
    {         
        //approved     
    }
    else if (moderationInformation.Status == SPModerationStatusType.Denied)
    {         
        //rejected     
    }
    else if (moderationInformation.Status == SPModerationStatusType.Draft)
    {         
        //item is in edit mode and yet send to pending state.     
    }
    else if (moderationInformation.Status == SPModerationStatusType.Scheduled)
    {         
        //approval is waiting to be processed by a timer service.     
    }
}

Help Link

Let me know if you have any query.

Thanks

  • I think the above is useful but I am unsure on how to add this to my original code (still a beginner). – Mike Blair Jul 30 '14 at 10:56
1

You can put a check like this :

       SPFile file=item.File;
       if(file.Level==SPFileLevel.Published)
        {
          if (file.CheckOutType == SPFile.SPCheckOutType.None)
           {
            file.CheckOut();  // Checking out the file
           }
        }
  • This seems to put it in a loop as I cannot check in the item also I added systemupdate(false) to your code and the version number still increase. Any ideas?? :-) – Mike Blair Jul 30 '14 at 10:53
  • Get list var list = properties.List; then use list.Update(); after System.Update(false); – priyanka gupta Jul 30 '14 at 11:13

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.