MKeeper

Hi,

I have a list which keeps version history.

I also have an event handler which traps the "Item Updating" event.

I want to stop people from restoring previous versions!

When you do a "Restore a Version" function then it does trigger my "Item Updating" event. But I cannot work out how to identify if the update is part of a genuine user updating the item or if they are doing a "restore" function.

Anyone have any ideas on how to do this

Thank you

MKeeper




Re: SharePoint - Development and Programming How can I identify a "Restore a Version" event?

MKeeper

Anyone have any ideas




Re: SharePoint - Development and Programming How can I identify a "Restore a Version" event?

in.the.dark

Perhaps you can check that Item's current version and compare it with the one in the document library's... Just a passing thought.

Edit:

Looking through, there is a SPListItem.Versions attribute, so it looks feasible to do it this way, though I don't have time to test it thouroughly.





Re: SharePoint - Development and Programming How can I identify a "Restore a Version" event?

MKeeper

NOTE - this is for a standard List .. not a Document Library!

I've looked at the "Versions" .. it's a collection of SPListItemVersion objects.

There is no "current version" property .. and there is no "published version" property .. as far as I can see.

Each version object has a VersionID and a VersionLabel .. but there is no way of correlating that to the item that is being updated.

Also .. each Version contains a "ListItem" property .. but that just points to the "current" list item (and doesn't contain any historic data or information).

To make matters even worse .. I tried writing an event handler which incremented values into the "Properties" hash table of the SPListItem. So a new Item would have a value of "1" and if you edit the item then it gives it a value of "2" .. then "3" and so forth.

Well .. I created 3 versions. Then I restored Version 1.

The newly restored "Item" had a "Properties" hash table value of "4". !!!

Which means that the Properties hash table does NOT get restored when you restore List Item versions.

When restoring a version .. the "AfterProperties" and "BeforeProperties" are also completely empty .. (which is exactly the same as if I opened an item for editing .. and just pressed "ok" .. so no way of distinguishing them).

Very very much a pain.

I am starting to think that "Restore a Version" actually executes code in the SharePoint API (written by Microsoft) which simply changes the field values and triggers an "Update" method.

This leads me to believe that working out if someone has restored a previous version .. or simply edited an item .. is impossible.






Re: SharePoint - Development and Programming How can I identify a "Restore a Version" event?

in.the.dark

Perhaps through a custom permissions level then...





Re: SharePoint - Development and Programming How can I identify a "Restore a Version" event?

MKeeper

Well I was thinking of that, but how can you give people the ability to modify and edit list items, but NOT give them the ability to roll-back to previous versions.

The main problem we've got is that we are running custom approval processes .. and we don't want people to roll back the document to a previous "approval stage" as it will start to mess up the workflows.






Re: SharePoint - Development and Programming How can I identify a "Restore a Version" event?

MKeeper

Aha !!!

Cracked it.

I figured that if it didn't store any versioning information .. why not roll my own

This is what I did:

* Added a new number column called "CustomVersion" (Default value 1). This was hidden completely from the user interface.

* Added an ItemUpdated event handler. This event handler increments the "CustomVersion" field value.

* Added an ItemUpdating event handler. In this I checked the properties.AfterProperties["CustomVersion"] and compared it to the properties.ListItem["CustomVersion"] value.

If the AfterProperties "MyVersion" was LOWER than the current "MyVersion" (i.e. from the ListItem) then it must be a restore operation from a previous version. If you had simply edited it from the user interface then the two values would be the same (as the ItemUpdated event wouldn't have fired yet).

So ... nailed it.

My source code for the Event Handler is below. (note .. this requires a column in the list called "CustomVersion").

Code Snippet

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.SharePoint;

namespace MyCustomClass.ReportEventHandler

{

public class EventReceiver : SPItemEventReceiver

{

public override void ItemUpdated(SPItemEventProperties properties)

{

try

{

DisableEventFiring();

// increment the custom versioning variable

SPListItem item = properties.ListItem;

int intVersion = (int)float.Parse(item["CustomVersion"].ToString());

intVersion++;

item["CustomVersion"] = intVersion;

// update without affecting versioning

item.SystemUpdate(false);

EnableEventFiring();

}

catch

{

// do nothing ... just making sure it doesn't *** out

// if the column isn't found

}

}

public override void ItemUpdating(SPItemEventProperties properties)

{

try

{

int intNewVersion = (int)float.Parse(properties.AfterProperties["CustomVersion"].ToString());

int intOldVersion = (int)float.Parse(properties.ListItem["CustomVersion"].ToString());

if (intNewVersion < intOldVersion)

{

properties.Cancel = true;

properties.ErrorMessage = "You are not allowed to Restore Previous Versions. This behaviour has been prohibited";

}

}

catch

{

// do nothing ... just making sure it doesn't *** out

// if the column isn't found

}

}

}

}

Edit - need to parse in the values as "Float" and then cast then to int data types.

hurrah!

(this took me a surprisingly long time!)

MKeeper






Re: SharePoint - Development and Programming How can I identify a "Restore a Version" event?

Yashvi

Can you please tell me how can i restore the previous version.

I am geting an exception saying this operation can not be performed.

a code sample for restoring previous version would be higjly appreciated

Thanks!!...





Re: SharePoint - Development and Programming How can I identify a "Restore a Version" event?

MKeeper

Yashvi wrote:

Can you please tell me how can i restore the previous version.

I am geting an exception saying this operation can not be performed.

a code sample for restoring previous version would be higjly appreciated

Thanks!!...

Erm ... my previous post was a full code-dump of the entire event handler.