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:
Approved
Denied
Draft
Pending
Scheduled

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

  1. private static void moderationPendingFix(SPListItem curItem)
  2. {
  3. if (curItem.ParentList.EnableModeration == true)
  4. {
  5. try
  6. {
  7. SPModerationInformation moderationInformation = curItem.ModerationInformation;
  8. if (moderationInformation.Status != SPModerationStatusType.Pending)
  9. {
  10. moderationInformation.Status = SPModerationStatusType.Pending;
  11. curItem.Update();
  12. }
  13. }
  14. catch (Exception exception)
  15. {
  16. //TODO: Exception Handling
  17. }
  18. }
  19. }

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!

share save 171 16 Setting SPListItem Moderation Status In C#

8 Comments

  1. sunil says:

    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.
    thanks
    gmail001

  2. Adam Buenz says:

    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);
    destinationFolder.Update();

    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. sunil says:

    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 2.it 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);
    destinationFolder.Update();

    thanks
    gmail001

  4. sunil says:

    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)
    mydestination.Update()
    error :
    Unable to cast object of type ‘Microsoft.SharePoint.SPFileStream’ to type ‘System.IO.FileStream’.

    thanks
    gmail001

  5. sunil says:

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

  6. Youngy says:

    Any chance you could submit your solution? I’m having the same problem.

  7. genesis says:

    hi! can somebody help me in moving a folder including the files to another site collection?

  8. pierrel says:

    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.

Trackbacks/Pingbacks

  1. Sharepoint link love 6-26-2007 at Virtual Generations - [...] Setting SPListItem Moderation Status In C# [...]

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>