Event receiver, unpublish
-
04-10-2020 - |
Question
Hi how could i use a event receiver based on a document library, to se if the user has unpublished a dokument in the library, in the mean time also i want to update a field in the document library when the user unpublish the document?
Any kind of help or suggestions...
============================`
UPDATE
I only want to se if the user has entered unpublish on a document then i want to update a field value, if the document is published do nothing..
public override void ItemUpdated(SPItemEventProperties properties){
//was published/approved before
if(properties.BeforeProperties["ows__ModerationStatus"] == 0){
//is now unpublished/pending
}
}
UPDATE 08:47
When user enter unpublish i want to clear a field but the field does not get updated? in a document library
//was published/approved before
if (properties.BeforeProperties["ows__ModerationStatus"].ToString() == "0")
{
//is now unpublished/pending
if (properties.AfterProperties["ows__ModerationStatus"].ToString() == "2")
{
string mycol_internal = "";
using (SPWeb web = properties.OpenWeb())
{
mycol_internal = web.Lists[properties.ListId].Fields["DisplayNameField"].InternalName;
web.AllowUnsafeUpdates = true;
}
properties.AfterProperties[mycol_internal] = "";
}
}
I am using ItemUpdated
Solution
You can write an event receiver for document library and check on itemupdated or updating event if the publishing state has been changed. You can use the ModerationInformation to see if it is in a published version or not. Approved is published. If you don't have approval, you can go for "Level", where 1 is published and 2 is unpublished.
Example:
public override void ItemUpdated(SPItemEventProperties properties){
//was published before
if(properties.BeforeProperties["Level"].ToString() == "1"){
//is now unpublished
if(properties.AfterProperties["Level"].ToString() == "2"){
//do what you want to do
}
}
}