Freigeben über


Module 7 - Code Snippets: Developing Business Processes with SharePoint 2010 Workflows

The following code shows how to return data from a workflow initiation form to the workflow.
Note: This code relies on there being an ASP.NET TextBox on the initiation form named MaxPrice.  Note also that the code snippet that follows this one uses the data returned.

namespace ApproveRejectProducts.Workflow1
{
public partial class GetMaxPrice : LayoutsPageBase
{
protected void Page_Load(object sender, EventArgs e)
{
InitializeParams();

        // Optionally, add code here to pre-populate your form fields.
}

    // This method is called when the user clicks the button to start the workflow.
private string GetInitiationData()
{
return (MaxPrice.Text);
}

  }

The following code shows how to use data returned from a workflow initiation form (as in the above example) and use it in a code activity in a SharePoint 2010 workflow.
Note: This code relies on there being a list named Products in the current SharePoint site.

namespace ApproveRejectProducts.Workflow1
{
public sealed partial class Workflow1 : SequentialWorkflowActivity
{
public Workflow1()
{
InitializeComponent();
}

    public Guid workflowId = default(System.Guid);
public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties();

    private void codeActivity1_ExecuteCode(object sender, EventArgs e)
{
double maxPrice = double.Parse(workflowProperties.InitiationData.ToString());
SPWeb thisWeb = workflowProperties.Web;
SPList products = thisWeb.Lists["Products"];
foreach (SPListItem product in products.Items)
{
if (double.Parse(product["SellingPrice"].ToString()) > maxPrice)
{
product.ModerationInformation.Comment = "Price too high";
product.ModerationInformation.Status = SPModerationStatusType.Denied;
product.Update();
}
else
{
product.ModerationInformation.Comment = "Price OK";
product.ModerationInformation.Status = SPModerationStatusType.Approved;
product.Update();
}

      }

    }
}
}