Setting SPListItem Moderation Status In C#

I ran into something simple today when I was writing a new event receiver for my current gig at Shaw AFB. The event handler itself was very simplistic. All it does is reference a piece of metadata within the SPListItem being uploaded into a document library, and based on that piece of metadata, route the document to possibly other locations, in this case a different instance. Besides the obvious limitation of the SPFile.MoveTo() function not being able to go cross-sites (which, honestly, I really despise since the method name is misleading IMHO), I had to set the the moderation status of the SPListItem being consumed within a seperate method via passing it in by a parameter to pending. Well, I take that back, I set the moderation status of the item if moderation status for the item is enabled, and if the SPModerationStatusType is not already pending :-) The code itself, is very, very simple, and when doing document moves, it adds a little touch of elegancy to your programming if you are doing such transitions within your development!

The SPModerationType enumeration has 5 members:

You see in the below method that I am using the Pending member.

private static void moderationPendingFix(SPListItem curItem)
if (curItem.ParentList.EnableModeration == true)
SPModerationInformation moderationInformation = curItem.ModerationInformation;
if (moderationInformation.Status != SPModerationStatusType.Pending)
moderationInformation.Status = SPModerationStatusType.Pending;
catch (Exception exception)
//TODO: Exception Handling


As you can see, minus the fact I leave the exception handling to the reader, it is very easy to achieve this particular piece of functionality, and when moving documents, is very helpful!


9 thoughts on “Setting SPListItem Moderation Status In C#”

  1. hi there
    i havea requiemtn of moving files to sub sites based on the custom colum which contains the site name or number.
    in this scenerio would u wriete a work flow on the parent doc lib or write an even handler to move files to sub sites.

    in case we write an even thandler i read some where thas psfile.move is not supported to moves files to subsites. wht is your pointer to this. any guidence is appreciated.

  2. You are correct, SPFile.MoveTo() and SPFile.Copy() does not work across sites. In this case, you have to do something a little different. Essentially, it would look something like this:

    private SPFolder destinationFolder;

    myFile = ((SPFile) null);
    myFolder = this.destinationFolder;

    Stream myStream = myFile.OpenBinaryStream();
    destinationFolder.Files.Add(myFile.Name, myStream);

    And I would consider doing it through an event receiver, since I tend to only use SharePoint workflows for when I need object persistence, for which your requirement doesn’t subscribe. It wouldn’t make sense since you are only doing a singular action which doesn’t consist of a series of steps or involve any real type of human oriented interaction to complete the routine.

  3. Hey thanks for your quick response
    I am getting confused there in my event handler i am confused. I have in SiteA doc lib – name of the folder source.
    SiteB has the destination doc library with SPFolder Destination1.
    SiteC has the destination doc library with SPFolder Destination2.
    2 subsites :
    http://server/sitea/source, http://server/siteb/destination1 http://server/sitec/destination2

    Since the destination folder is in different sub sites, doc folder,
    how does the even handler know the subsites destination folder r.
    SPFolder destinationFolder =http://server/sitea/destination1.
    I am sure if I give just destination 1 or destination will not know. And also when u say
    myfile it is in the source doc library.
    Private SPFolder destinationFolder;
    my File = ((SPFile) null);
    myFolder = this.destinationFolder;
    Stream myStream = myFile.OpenBinaryStream();
    destinationFolder.Files.Add(myFile.Name, myStream);


  4. hey there
    Unable to cast object of type ‘Microsoft.SharePoint.SPFileStream’ to type ‘System.IO.FileStream’

    Dim site1 As New moss.SPSite(“http://S11-wsstest”)
    Dim myweb As moss.SPWeb = site1.OpenWeb
    Dim mysource As moss.SPFolder = myweb.GetFolder(“Shared Documents”)
    Dim mydestination As moss.SPFolder = site1.AllWebs(1).Folders(“Shared Documents”)
    Dim myFiles As moss.SPFileCollection = mysource.Files
    Dim thisfile As moss.SPFile = myFiles.Item(0)
    Dim mystream As IO.FileStream
    mystream = thisfile.OpenBinaryStream()
    mydestination.Files.Add(thisfile.Name, mystream)
    error :
    Unable to cast object of type ‘Microsoft.SharePoint.SPFileStream’ to type ‘System.IO.FileStream’.


  5. sorry to toruble you , i got it , thanks a lot, i dont knwo that there is i corrected and the code is working fine
    thanks a lot

  6. If you call the Update method of the SPListItem object after explicitly setting the moderation status to any other value than Pending, your moderation status will be set back to Pending.
    MOSS asserts that there’s a modification on the item and needs approbation for this new version.
    Bottomline, never call Update after setting moderation status if you want your status to be taken in count.

Leave a Reply

Your email address will not be published. Required fields are marked *