问题
I am new to SharePoint. I'm creating a workflow in SharePoint 2013 in which I want to iterate a List using a loop. How to perform looping through list items in SharePoint 2013 Designer Workflows?
回答1:
I'm not aware that there's any out-of-box way to get a workflow to "loop through" all entries in a list, but you can try to make two workflows bouncing back to each other until all entries are updated, here is the detailed steps,
assuming that the list that we are working on is named
list_work
, in this list, create a column named e.g.cargo
define another list, name it e.g.
list_control
,- define a column named e.g.
listID
, - define a column named e.g.
cargo
, - create one entry in the list, set the
cargo
to a value.
- define a column named e.g.
define a workflow, name it e.g.
workflow_list_control
, which is triggered every time the entry inlist_control
is updated, and in this workflow,- search for an entry in the
list_work
where thelist_work.cargo
doesn't equallist_control.cargo
- if any entry is found, update the entry of
list_work
and set thelist_work.cargo
withlist_control.cargo
, in order to trigger theworkflow_list_work
(see #4 below) - if no entry is found, stop this workflow.
- search for an entry in the
define the workflow that needs to be run on the
list_work
, e.g. namedworkflow_list_work
, make it's triggered every time the entry inlist_work
is updated, and in this workflow, make sure it updates the only entry in thelist_control
with the ID of the entry fromlist_work
that is being modified, only to trigger theworkflow_list_control
. In this step, try to put a workflow step to wait and verify the ID is properly updated, in order to allow enough time for the previousworkflow_list_control
properly finishes.
Every time if a looping-through is necessary, modify the entry in list_control
with a different value in the cargo
field.
Also refer to calculated-column-to-retrieve-total-number-of-id
回答2:
You can do this in SharePoint 2013 (ONLY!) using the new "HTTP web service". The "trick" is to
- Create a task that will call the HTTP Web Service and call ".../client.svc/web/lists/getbyid(
<List Guid>
)/items" - Return the (JSON) results in a (new in 2013 also!) "dictionary" variable
- Use the "Get an Item from a Dictionary" action
- "item by name or path" and enter "d/results"
- select the "dictionary" variable (that you captured the results in)
- output to another dictionary variable (call it "data")
- Then get the count (for the loop) by using the action "Count Items in data" and output that to a variable (totalItems) to get the number of records
For the "loop" you will use the "Loop with condition"; use a variable called "index" and set it to 0. "Loop with condition" until "index < totalItems"
In the body of the loop:
- "Get an item from a dictionary" use "item by name or path"
- using string builder "d/results/(
<index>
)/". The "Column Name to Retrieve" will be whatever column(s) you want to evaluate (like maybe "DueDate").
Note: You will have to use the "Static Name" of the column, which can be found by going to the Task List Settings and clicking the name of the column, then in the URL find the "Field=<Name>
" portion.. It is that "<Name>
" that you will use.
I know this is very CRUDE maybe even bordering on "useless" for alot of users, but I felt I should at least attempt a quick reply. When I have more time, I will try to edit this and format my reply better. Although, I found this because of a similar need and since I figured out a way to accomplish what I was needing, I felt compelled to at least share it the best I could, in the limited time I had available. ;) So, maybe SOMEONE will find what I have attempted to describe, "useful". :)
来源:https://stackoverflow.com/questions/25810296/how-to-perform-looping-through-list-items-in-sharepoint-2013-designer-workflows