Domanda

So I'm creating a SharePoint 2010 project in Visual Studio that contains a Sequential Workflow (Farm Solution only) item that is associated with a certain list so that when an item is added to the list the workflow starts.

My question is, say an item was added in the following format:

Name | Email

Dave | dave@dave.com

Is there a way, programatically, to store this data in variables? As is, without using any hardcoded indexes or anything, just programatically have it so that I can pull this data and store it in two C# variables (int iD, string emailAddress) and the workflow knows which list item kicked it off?

At the moment the way I'm doing it is:

using (SPWeb oWeb = SPContext.Current.Web) {
    SPList oList = web["List Name"];
    string name = list.Items[(oList.ItemCount - 1)]["Name"].ToString();
}

But I'd rather not use indexers as there's a chance that the index is off if another item is added rapidly and if the list is reordered then ... disaster.

Thanks in advance!

È stato utile?

Soluzione

Solved it guys! Very stupid of me to overlook this, but here is the solution:

C# creates a workflowProperties variable for you of type SPWorkflowActivationProperties which contains all the methods and properties to get the data you need:

public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties(); // auto generated code.

Then to get the list item data or even list data all you do is:

SPList oList = workflowProperties.List; // get the list that contains the list item on which the workflow instance is running.
SPListItem oItem = workflowProperties.Item; // get the list item on which the workflow instance is running.

So to get my "Name" data I would've had to:

string name = workflowProperties.Item["Name"].ToString();

Hope this helps someone, although fortunately nobody is as stupid as me so you guys will probably figure it out yourself.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top