About      |       Articles      |      Services      |      Software      |      Contact

Latest Free SharePoint Software

ARB Security Solutions regularly releases free SharePoint software, including WebParts, Client Applications, Framework Extensions, and other Miscellaneous Components.
The most recent freeware is:

Title: Simple SharePoint Rollup WebPart
Date Published: 10/22/2009

Previous Two Free WebPart Releases:

SecureCenter For SharePoint

By SharePoint security integrators, for SharePoint security integrators.

SharePoint Security Assurance Program™

For externally facing SharePoint deployments, security is an acutely important deployment concern. Learn how through daily security scanning, you can ensure external business users and partners that they can collaborate in confidence!

Security Assurance WebPart:



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.

C#:
  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/Bookmark

9 Comments »

  1. [...] Setting SPListItem Moderation Status In C# [...]

    Pingback by Sharepoint link love 6-26-2007 at Virtual Generations — June 26, 2007 @ 3:29 am

  2. 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

    Comment by sunil — July 3, 2007 @ 7:57 am

  3. 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.

    Comment by Adam Buenz — July 3, 2007 @ 8:27 am

  4. 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

    Comment by sunil — July 3, 2007 @ 1:05 pm

  5. 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

    Comment by sunil — July 5, 2007 @ 1:29 pm

  6. 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

    Comment by sunil — July 5, 2007 @ 1:34 pm

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

    Comment by Youngy — July 16, 2007 @ 1:52 pm

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

    Comment by genesis — July 30, 2007 @ 2:50 am

  9. 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.

    Comment by pierrel — August 24, 2007 @ 12:52 am

RSS feed for comments on this post. TrackBack URL

Leave a comment