Windows SharePoint Services >
SharePoint Products and Technologies Forums
>
SharePoint - Development and Programming
>
I cannot approve the publishing page (submitted for approval), could you please advise my code?
I cannot approve the publishing page (submitted for approval), could you please advise my code?
- Hi all ^^,
I wanna override approve button (the qucik access buttion in Page Editing Tool Bar),
the basis approve function must be kept and just need to add some custom action...
When I try to add the following code to act the work flow like SharePoint default action ( for Status : Waiting for approval ),
I cannot approve that without any exception:
Could you please advise me for above coding?SPSite spSite = new SPSite(strPageUrl); SPWeb spWeb = spSite.OpenWeb(); //Test Only - Start ^o^ if (PublishingWeb.IsPublishingWeb(spWeb)) { spWeb.AllowUnsafeUpdates = true; //Test Only - End XD //Approve Work Flow - Fake - Orz SPListItem listItem = SPContext.Current.ListItem; if (PublishingPage.IsPublishingPage(listItem)) { Common.WriteTxtFile("Is Publishing Page", strOutPutPath_Log); PublishingPage page = PublishingPage.GetPublishingPage(listItem); if (page.ListItem.ModerationInformation.Status.Equals(SPModerationStatusType.Draft) || page.ListItem.ModerationInformation.Status.Equals(SPModerationStatusType.Pending)) { string checkInComment = "Custom Approve Action";
SPModerationInformation modInfo = listItem.ModerationInformation; modInfo.Status = SPModerationStatusType.Approved;
modInfo.Comment = checkInComment;
listItem.Update(); //Approve Work Flow - Fake - Approve Failed without Exception - >o< page.ListItem.File.Approve(checkInComment); //Approve Work Flow - Fake - Publish Failed without Exception - >o< page.ListItem.File.Publish(checkInComment); } } // Test only spWeb.AllowUnsafeUpdates = false; }
Moreover, my testing SharePoint seems has the "send E-mail" workflow and it will act with approve workflow,
if the page approved in code (e.g. if Approve() is OK) , Is Approve() will trigger "send E-mail" workflow or not?
If not, how can I do?
Thank you very much for any help or advice.
.NET Beginner 3.5- Edited byHello World August Friday, July 03, 2009 12:38 PM
Answers
- Hi,
Kindly apply a try catch block for the following code.. like [I am typing this from a notepad, kindly check the code in Visual Studio]
try
{
page.ListItem.ModerationInformation.Status = SPModerationStatusType.Approved;
page.ListItem.ModerationInformation.Comment = checkInComment;
page.ListItem.Update();
}
catch(Exception ex)
{
throw ex;
}
try
{
page.ListItem.File.Approve(checkInComment);
page.ListItem.File.Publish(checkInComment);
//I THINK YOU ARE MISSING AN UPDATE STATEMENT HERE
}
catch(Exception ex)
{
throw ex;
}
Hope this helps
Raghavan- Marked As Answer byAaron Han - MSFTModeratorFriday, July 17, 2009 10:44 AM
All Replies
- i think you dont have permissions to approve. use Runwithelevated privilages code to approve the page
- Thank you for your help, but the statue still cannot be changed by following code... >O<
SPSecurity.RunWithElevatedPrivileges( delegate() { SPSite spSite = new SPSite(strPageUrl); SPWeb spWeb = spSite.OpenWeb(); if (PublishingWeb.IsPublishingWeb(spWeb)) { spWeb.AllowUnsafeUpdates = true; SPListItem listItem = SPContext.Current.ListItem; if (PublishingPage.IsPublishingPage(listItem)) { Common.WriteTxtFile("Is Publishing Page", strOutPutPath_Log); PublishingPage page = PublishingPage.GetPublishingPage(listItem); Common.WriteTxtFile("ModerationInformation Status - " + page.ListItem.ModerationInformation.Status, strOutPutPath_Log); if (page.ListItem.ModerationInformation.Status.Equals(SPModerationStatusType.Draft) || page.ListItem.ModerationInformation.Status.Equals(SPModerationStatusType.Pending)) { string checkInComment = "Custom Approve Action"; Common.WriteTxtFile("Try to publish...", strOutPutPath_Log); SPModerationInformation modInfo = listItem.ModerationInformation; page.ListItem.ModerationInformation.Status = SPModerationStatusType.Approved; page.ListItem.ModerationInformation.Comment = checkInComment; page.ListItem.Update(); page.ListItem.File.Approve(checkInComment); page.ListItem.File.Publish(checkInComment); Common.WriteTxtFile("No error, no break, no exception and no changes!"); SPModerationInformation modInfo = listItem.ModerationInformation; <br/> } } spWeb.AllowUnsafeUpdates = false; } } );
.NET Beginner 3.5 (~.^)- Edited byHello World August Friday, July 03, 2009 1:07 PM
- Edited byHello World August Friday, July 03, 2009 1:07 PM
- Edited byHello World August Friday, July 03, 2009 1:07 PM
- Edited byHello World August Friday, July 03, 2009 12:52 PM
- Edited byHello World August Friday, July 03, 2009 12:52 PM
- Edited byHello World August Friday, July 03, 2009 12:52 PM
- What error exactly u r getting while approving
- No error, no break, no exception after run the code ....... but no changes too (~.~)
And I can see the log file include the message and it created by:
Common.WriteTxtFile("No error, no break, no exception and no changes!");
.NET Beginner 3.5 (~.^)- Edited byHello World August Friday, July 03, 2009 1:09 PM
- After Page.ListItem.Update()
First Check in tht file then publish & approve.
Then dopublishingWeb.Update();
and then
subweb.Update();
After all these dispose all the webs
- Hi,
Kindly apply a try catch block for the following code.. like [I am typing this from a notepad, kindly check the code in Visual Studio]
try
{
page.ListItem.ModerationInformation.Status = SPModerationStatusType.Approved;
page.ListItem.ModerationInformation.Comment = checkInComment;
page.ListItem.Update();
}
catch(Exception ex)
{
throw ex;
}
try
{
page.ListItem.File.Approve(checkInComment);
page.ListItem.File.Publish(checkInComment);
//I THINK YOU ARE MISSING AN UPDATE STATEMENT HERE
}
catch(Exception ex)
{
throw ex;
}
Hope this helps
Raghavan- Marked As Answer byAaron Han - MSFTModeratorFriday, July 17, 2009 10:44 AM
- Thanks for your kindly advice,
I tested the following code first, it's work ... but...
My queestion is :bool flagApproved = false;<br/>SPFile page.ListItem.File = page.ListItem.File<br/>publishFile.CheckOut(); // 101<br/> publishFile.CheckIn(checkInComment,SPCheckinType.MajorCheckIn); //102 publishFile.Publish(checkInComment); // 103 publishFile.Approve(checkInComment); // 104<br/><br/><br/><br/><br/><br/><br/>
1. Can I don't check out and just publish it as Major Version? (It cannot work if just insert line 103 and 104)
2. Can I check the page/file status in SPFile ?
e.g. I want to set flagApproved = true if the page approved, how can I check the status....? (e.g. Is Major Version ?)
3. Is the above coding risky? Could you please let me know iif you have any solution is better than above coding...
4. I think my concept is not clear for Publishng Page Approval Workflow ...
4.1 Why I just can aprrove it by using SPFile method? And why I failed to publish page in Page Editing Toolbar by using Page.ListItem.XXX or Page.XXX() etc.
Thank you very much for any help or advice.
.NET Beginner 3.5 (~.^)- Edited byHello World August Sunday, July 05, 2009 8:15 AM
- You can't approve a page with out it being checked out.
You can know the page status. You already checking in your code if it is not in draftmode and not in pening state


