MSDN > 論壇首頁 > 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?

  • 2009年7月3日 下午 12:23Hello World August 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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:

     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; }
    Could you please advise me for above coding?

    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

解答

  • 2009年7月3日 下午 05:39RaghavanS 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    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

所有回覆

  • 2009年7月3日 下午 12:31Naresh kilaru [MCTS] 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    i think you dont have permissions to approve. use Runwithelevated privilages code to approve the page

  • 2009年7月3日 下午 12:51Hello World August 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     包含代碼
    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 (~.^)
  • 2009年7月3日 下午 12:55Naresh kilaru [MCTS] 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    What error exactly u r getting while approving
  • 2009年7月3日 下午 01:05Hello World August 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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 (~.^)
  • 2009年7月3日 下午 01:19Naresh kilaru [MCTS] 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    After Page.ListItem.Update()

    First Check in tht file then publish & approve.

    Then do

    publishingWeb.Update();

    and then

    subweb.Update();

     

    After all these dispose all the webs


  • 2009年7月3日 下午 05:39RaghavanS 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    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
  • 2009年7月5日 上午 08:13Hello World August 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     包含代碼
    Thanks for your kindly advice,

    I tested the following code first, it's work ... but...

    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/>
    
    My queestion  is :

    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 (~.^)
  • 2009年7月6日 上午 11:21Naresh kilaru [MCTS] 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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