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

In the previous article we finished the build of our "Auto ID" workflow and deployed it to our "Project Library".

In this article we’ll finish off the rest of the customizations needed on our library, then test the functionality we’ve built to make sure everything works as expected.

Getting Started

To start, the first thing we need to do is modify the column ordering to make them appear in a more logical fashion on our "Edit" form page.

In the "Projects Library" go to its "Settings" page and under the "Columns" section, click the "Column Ordering" link.

Set the order of the fields as:

Title = 1
Name = 2
New Project = 3
Existing Project = 4
Project Code = 5
Document Type = 6
Auto Number = 7
Version Code = 8
Project ID = 9
Run Main Workflow = 10


Next, we need to modify the default view of the library so it only shows the fields we’ll need and to configure the “Grouping” needed to allow for our custom versioning.

Before we do this however, we need to create one more column that’s sole purpose is for the grouping view (this will let us group by project).

Create a new column called “Project ID View” and make it a “Calculated” type with the following formula:

=[Project Code]&"."&[Document Type]&[Auto Number]


Since our final “Project ID” value will contain the “Version Code” along with the rest of the ID values, we can’t group on it by itself (each version would be a completely new value, so the grouping wouldn’t work), so by having a value that doesn’t include the version, we can safely use this column in our grouping to associate each related document.

To apply the grouping, and set which fields we actually want to view, go to the default view and choose “Modify this view” (or just click the view name under “Views” if you’re still in the library settings page), and set it as follows:

Uncheck all but those listed below and in the following order:

Type (icon linked to document) = 1
Name (linked to document with edit menu) = 2
Project ID = 3
ID = 4 (this is only going to be used for initial debugging and can be removed later)


Next, while still in the edit view page (or go back to it if you’re not still there), scroll down and expand the “Group By” section and select “Project ID View” in the “First group by the column” dropdown then click “OK” to save the modified view.


The final step in the customization is to modify the "Edit" forms page with a CEWP and a small JavaScript that will clean up the values listed on the "Existing Project" dropdown list.

Since this field is a "Lookup" to the "Auto Number" field, each document will have a value that will be listed on it. The problem we run into is that when adding in new version documents, they will have an "Auto Number" that is already in existence in the library, so we’ll wind up with duplicate values listed in the dropdown. Functionality-wise, this isn’t important because when adding a new version we could choose any of the values listed and things would still work fine, but if you wind up having many versions added to existing projects, the dropdown list will eventually become a bear to scroll through (duplicate after duplicate) just to get to the value you want. The script we’ll be adding will simply parse through the values displayed in the list and remove any duplicates making it cleaner to navigate.

Since this is a system page, we don’t have the "Edit" option in the browser (yet), so we need to open it in SharePoint Designer and add in the html needed, then the CEWP with the script.

Open SharePoint Designer, connect to your site and expand the "Projects Library" folder in the "Folder List" then expand the "Forms" folder.


Double-click the "EditForm.aspx" file then in the html (code) view, scroll down to line #55, put your cursor after the "" line and press "Enter" to add in a new line then paste in the following:

This will add in a new web part zone below the list view where we can add in our CEWP.


Click "Save" and switch to the "Design" view to see the new zone added to the bottom of the page (click "Yes" on the "Site Definition Page Warning" message that pops up).


In the new "Bottom" zone, click on the "Click to insert a Web Part" link then in the "Web Parts" list that opens (by default in the upper right-hand corner of SPD), click-and-drag a "Content Editor Web Part" into the zone.


Next, in the code view, go to line 86 and look for the "" section.


Replace the section with the following:

Old code:

New code:

01
06 {
07 for (var j = 1; j <>
08 {
09 if ((opt[i].options[j + 1] != null) && (opt[i].options[j + 1].text == opt[i].options[j].text))
10 {
11 opt[i].options[j] = null;
12 j -= 1;
13 }
14 }
15 }
16}
17]]>

Once you have the script added, the last piece is to hide the CEWP from displaying its title (since it just has script in it, the only part of the web part that will display is its title) by setting its "Chrome" to "None" (alternatively, you could set its visibility to "Hidden" – either will work fine).

In SPD, switch to the "Design" view, right-click on the "Content Editor Web Part" and in the menu that opens, select "Web Part Properties".

Expand the "Appearance" section and in the "Chrome Type" dropdown, select "None" then click "OK" to close the properties page.


Now that we’ve added in our script, we won’t see any duplicate items on the "Existing Projects" dropdown list.

Save the page.

Before we leave SPD and move on to the testing phase, you may be asking why didn’t we just do this in the browser? Well, the answer is simply because until we’ve added something to the library, we don’t have access to the "Edit" page. We could have waited to do this last bit of customization until after we added in our first document, but since we were already in SPD, it’s simple enough to go ahead and add in the script here.

To test things, close SPD and return to our site in the browser.

If you’ve been following along throughout this entire series so far, both the "Auto ID Master" list and the "Projects Library" should be empty (if they’re not – because you decide to jump ahead a bit, you’ll have to add/delete items in both places until you get to the point where both the list and library are empty, and the next item you could create in the "Auto ID Master" list will be one "System ID" number higher than that of the last item deleted in the "Projects Library". If you have difficulty getting things sorted out on this, post a comment and I’ll help you get them back in sync).

In the "Auto ID Master" list, create a new item with "Next Available Number" for its "Title" and "100" for its "Next Number Available" value.


Next, go to the "Projects Library" and upload a document.

For these tests, I’ll be using a series of test documents for new projects and new versions named appropriate to their version ("A" indicates new project, "B" or "C" indicates new version of existing project).

Remember, we do have two separate workflows attached to this library and as soon as we upload a document the first workflow will start – even before we exit the "Edit" form page, the workflow is already running because the document will already be added to the library.

Once you’ve uploaded a new document, you’ll be taken to the "Edit" page where you can specify its details.

In the "Title" field add in an appropriate value.

The "Name" field should already be populated, but you can change it if needed.

Select the value from the "New Project" dropdown (this should be "100").

In my example, I’ll leave the "Project Code" and "Document Type" fields as is.

Ignore the "Auto Number" field – this will always be populated by the workflow.

Since this is a new project, we’ll also leave the "Version Code" as the default "A" value.

Finally, select "Yes" in the "Run Main Workflow" dropdown in order to have our "Auto ID" workflow start.


Click "OK" to save and exit the "Edit" page.


After each workflow has completed, the "Project ID" is built with each piece (“Project Code”, “Document Type”, “Auto Number”, and “Version Code”) and is added to a group based on the "Project ID" minus the version letter.

Add in a second "New Project" and you’ll notice that the value in the "New Project" dropdown is now one number higher than it was last time.


Depending on how long it takes the workflows to complete, you may on occasion see the temporary state of "In Progress" listed for the "Auto ID" workflow which results in the "Project ID" having a value of "xxxx" listed in its "Auto Number" section. If this happens, just refresh the page and you’ll see things completed.


To test the "Version" capabilities, upload another document but instead of choosing a value from the "New Project" dropdown, select a value from the "Existing Project" dropdown.




Adding in more documents, you’ll see that as each new document is added, if it’s a "New Project" it will get its own group, and if it’s a new version of an "Existing Document", it’ll get added to an existing group.





As a final note, depending on how you want to organize the documents in the projects, you may need to modify the grouping to be based on just the "Project Code" if you wind up having multiple "Document Types" within the same project. In that case, project codes in the form of "02.01.1255.A", "02.01.2255.A", "02.01.3255.B" would all fall under the group "02.01", so modify the list and views as needed to handle your type of scenario (you can also create new calculated columns for use in other views as well).

Summary

What we’ve accomplished throughout this series is a way in which to add in extra functionality that didn’t exist in a default site.

Since the default "System ID" value is not immediately available after creating a new item, it can be difficult to base an auto-numbering system on it alone. To get around this limit, we created our own custom ID scheme that is built on several different pieces of information that we define after a document has been uploaded to the library.

The workflows we built also allowed us to get past a specific drawback on document libraries that makes workflow initiation on item creation problematic since the only time we get a chance to edit the document properties and give it the required bits of information is after its already been uploaded.

The solution in this case, was to build an initialization workflow that on item creation would wait for a specific value, then perform an update. With this as the trigger to start a second workflow that performed the bulk of our actions to auto-increment our custom number and finally provide the necessary details to allow our calculated columns to build out our final "Project ID", we accomplished all steps necessary to meet our initial requirements of our custom "Project ID" and versioning.

source:endusersharepoint.com

0 nhận xét:

Post a Comment

thanks comment