0

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

1 Answer 1

0

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
           }
        }
     }
18
  • Do u have some example?
    – Batistuta
    Jun 18, 2015 at 6:18
  • i will update....
    – Batistuta
    Jun 18, 2015 at 6:37
  • I also get the error, cannot use == operand object,int
    – Batistuta
    Jun 18, 2015 at 6:43
  • ah sorry you neet to cast to string..i update it
    – Gwny
    Jun 18, 2015 at 6:45
  • Alright i will update again in the top
    – Batistuta
    Jun 18, 2015 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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