SharePoint: How to Create an Auto-Incrementing Number Field For Use in a Custom ID – Part 4

In the previous article we built out the first part of our "Auto ID" workflow to handle new items added to the "Project Library".

We defined actions to grab the current number and the current number incremented by one, then used those values to create a new item in the "Auto ID Master" list, delete the old item, update our current item in the "Project Library" and set a value to end the workflow on its next run.

In this article, we’re going to add in the functionality to handle new items that are versions of existing projects.

Getting Started

To start, open SharePoint Designer and connect to our site. Once connected, expand the "Workflows" folder, select the "Auto ID" workflow in the folder list then double-click the "Auto ID.xoml" file in the "Website" content list in the main window to re-open the existing workflow designer window.




In the first part of our workflow, we defined a condition to check for that would handle new items added for "New Projects". In order to handle new "Versions" added for existing projects, we need to define a second set of conditions.

Click the "Add ‘Else If’ Conditional Branch" link under the existing set of actions.


This will add in a second set of menu’s for "Conditions" and "Actions".

Similar in how we defined the actions in the first condition, we’ll now define the condition to check for then perform our calculations, create/delete, and update (with a small modification to the order in which the actions take place).

NOTE – all of these steps will take place in the second condition, so make sure that you’re using the lower “Conditions” and “Actions” buttons.

Under “Conditions”, select “Compare Projects Library Field”.


Click the "field" link and select "Version Code" in the dropdown.


Click the "equals" link and select "not equals" in the dropdown then click the "value" link and choose "A" from the dropdown.


Since the only time we’ll be using "A" in our uploaded documents (all new versions will have values like "B" and "C"), this first condition will be satisfied.

Additionally, since the "Initialization" workflow is waiting on the "Run Main Workflow" field to have a value of "Yes" before it performs an update (which will then fire off this workflow), we need to verify that the field is indeed set for "Yes" before this workflow will run, so we need to add in a second condition to check for (same as we did in the first half of this workflow we built previously).

Under the "Conditions" menu choose "Compare Projects Library Field" a second time then click the "field" link and choose "Run Main Workflow" in the dropdown then click the "value" link and choose "Yes" in the dropdown.




Since we now have our conditions to check for, we can now build out our actions.

Under the "Actions" menu, select "Update List Item".


Normally, you’d want to perform all updates towards the end of your actions, but in the case we want to update the current item in the document library first to set its "Auto Number" value to that of the existing project id value we chose (on the "Edit" form page).

We’ll also be setting the value of the "Run Main Workflow" value to "No" to make sure the next instance of this workflow that will soon start (we’re performing an update, so that will once again start the workflow) will end immediately, then continue on to the actions that will update the item in the "Auto ID Master" list.

Click the "this list" link and verify that "Current Item" is selected in the "Update list item" window that opens, then click the "Add" button to open a "Value Assignment" window.




Select "Auto Number" in the "Set this field" dropdown then click the "fx" button to open a "Define Workflow Lookup" window.


In the "Define Workflow Lookup" window, select "Current Item" in the ""Source" dropdown and "Existing Project" in the "Field" dropdown, then click "OK" to close the window and return to the "Value Assignment" window then click "OK" again to return to the "Update List Item" window.






Click the "Add" button again, and in the "Value Assignment" window that opens, select "Run Main Workflow" in the "Set this field" dropdown and "No" in the "To this value" dropdown then click "OK" to return to the "Update List Item" window then click "OK" again to return to the main workflow designer window.






Next we need to get the current value in the item on the master list and assign it to a variable so we can create a new item with the same number then delete the old item (we’re not incrementing the number since this is a new version only, so we’re just going to keep the same number in the new item, but since again, all comparisons are based on matching ID values – and we’re still adding new item to the document library which will get the next system ID value, we need to create a new item on the master list that will also have a new ID value in order to keep the list and library in sync.)

Under the "Actions" menu, select "Do Calculation".


Following the same process we used in the first part of the workflow (from the last article), click the first "value" link then the "fx" button to open a "Define Workflow Lookup" window and select "Auto ID Master" in the "Source" dropdown to expand the window to allow us to further define the value we’re looking for.


In the upper half of the window, select "Next Number Available" in the "Field" dropdown, "Auto ID Master:ID" in the "Field" dropdown in the lower half of the window, then click the "fx" button to open a new "Define Workflow Lookup" window and choose "Current Item" in the "Source" dropdown and "ID" in the "Field" dropdown then click "OK" to close the window






Click "OK" to close the window and return to the main workflow designer window.


Click the remaining "value" link and type in a zero "0", then click the "Output to Variable: Calc" and select "Variable: NextNumber" in the dropdown.


Now that we have the variable set, it’s time to create a new item on the master list and use this variable as its next number value.

Under the "Actions" menu, select "Create List Item".


Click the "this list" link and in the "Create New List Item" window that opens, select "Auto ID Master" in the "List" dropdown – this will add in a "Title(*)" entry in the fields list.


Click the "Title(*)" entry then click the "Modify" button and in the "Value Assignment" window that opens, type in "Next Available Number" in the "To this value" field then click "OK" to return to the "Create…" window.




Click "Add" and in the "Value Assignment" window that opens, select "Next Number Available" in the "Set this field" dropdown then click the "fx" button to open a "Define Workflow Lookup" window and select "Workflow Data" in the "Source" dropdown and "Variable: NextNumber" in the "Field" dropdown, then click "Ok" to close the lookup window and "OK" again to close the "Value Assignment" window to return to the "Create New Item" window.






Click "OK" to return to the main workflow designer screen.


Now that we’ve created our new item with the same value in the "Next Number Available" field as it already had (thus, incrementing its system ID so future comparisons will still match), we need to delete the old item to make sure that on new items added to the "Projects Library", we’ll still only see one number to choose from in the "New Project" dropdown.

Under the "Actions" menu, select "Delete Item".


Click the "this list" link and in the "Choose Item" window that opens, select "Auto ID Master" – this will expand the options to allow us to further define the item we’re looking for.


Select "Auto ID Master:ID" in the "field" dropdown then click the "fx" button to open a "Define Workflow Lookup" window and select "Current Item" in the "Source" dropdown and "ID" in the "Field" dropdown then click "OK" to return to the "Choose list item" window.




Click "OK" to return to the main workflow designer window.


To finish out the build of the workflow, we’ll add in a manual stop in the same fashion as we did in the first half to make sure that if something does happen to fail or not work properly, the workflow will still end.

Under the "Actions" menu, select "Stop Workflow" then click on the "this message" link and type in "Workflow completed with new version added".

With this action added, we now have completed workflow.


Click the "Finish" button to build the workflow and deploy it to our "Project Library".

Summary

In our workflow, we follow a few basic steps to determine what type of document has been uploaded, and depending on whether it’s a new project or a new version of an existing project, we use two separate sets of actions in order to set the "Auto Number" value in the document and increment the "Next Number Available" if needed.

The process involves a comparison between the System ID of the current item that has just been uploaded, and that of the System ID of the most recent item on the master list (which will equal the newly uploaded document ID). Once processed, we then create a new item in the master list to house the new "Next Number Available" value (incremented by one, if the document was a new project, and set to equal the previous value if it was just new version), then delete the previous item. This is required because after all workflows have ran, the most recent item in the master list should always have an ID that is one higher than the most recent item uploaded to the document library in order to be able to perform the comparison on the next upload (they will be the same at the time a new document is uploaded only).

In the next article we’ll perform the last bits of customization needed on the library "Edit" form and default view in order to make it cleaner to work with.

source:endusersharepoint.com

0 nhận xét:

Post a Comment

thanks comment