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.
- private static void moderationPendingFix(SPListItem curItem)
- {
- if (curItem.ParentList.EnableModeration == true)
- {
- try
- {
- SPModerationInformation moderationInformation = curItem.ModerationInformation;
- if (moderationInformation.Status != SPModerationStatusType.Pending)
- {
- moderationInformation.Status = SPModerationStatusType.Pending;
- curItem.Update();
- }
- }
- 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!
8 Comments
Trackbacks/Pingbacks
- Sharepoint link love 6-26-2007 at Virtual Generations - [...] Setting SPListItem Moderation Status In C# [...]
Articles & Research
SharePoint Security
SharePoint Development
SharePoint Architecture
Claims Authentication
Forefront For SharePoint
AIS / Dynamics GP
Team Foundation Server
Pex And Moles
ISA/TMG/IAG/UAG
DPM
Cardspace
Research Methodology
Rural ICT Development
Numerical Analysis
Multi-Level Research
Knowledge Management
Personal/Off-Topic
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
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.
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
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
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
Any chance you could submit your solution? I’m having the same problem.
hi! can somebody help me in moving a folder including the files to another site collection?
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.