Updating 'ModerationStatus' field

You might have experienced a road block when trying to the update the ‘ModerationStatus’ field with other fields. Any attempt to do so will result in an exception “You cannot change moderation status and set other item properties at that same time”. This is an expected behavior since the document deals with stream, promotion and demotion and for non document libraries we are not touching the streams, so it allows updating other fields with “ModerationStatus”.

Here is the code: In this sample code, we are trying to update the 'Editor' field along with the 'ModerationStatus’.

      

    1: SPSite oSite = new SPSite("https://localhost:100/sites/Internetsite/");
    2:         SPWeb oWeb = oSite.OpenWeb(); 
    3:         SPList oList = oWeb.Lists["Shared Documents"]; 
    4:         SPListItem oListItem = oList.Items[0]; 
    5:         SPUser oUser = oWeb.AllUsers[@"domain\user123"]; 
    6:         SPFieldUserValue oUserValue = new SPFieldUserValue(oWeb, oUser.ID, oUser.LoginName); 
    7:         oListItem["Editor"] = oUserValue;                
    8:         oListItem.ModerationInformation.Status = SPModerationStatusType.Approved; 
    9:         oListItem.Update();

 

Note: This behaviour is only with libraries of type document libraries(SPBaseType.DocumentLibrary).

Good Luck!!!