Thursday, December 9, 2010

Generate auto generate filename for infopath form

Auto generate Filename in Infopath 2007
There are some situations where we need to auto generate an infopath filename instead of user giving their own filename and messing the work. Here we go.
Create a simple blank form with single textbox and single button (Save).


Create Filename field:
Infopath store all fields in “Main Data Source” data source. You can list all fields (text box, button, repeater control etc) in Main Data Source and all Data Connection which you create (to access external data source) are stored in “Secondary Data Source” data source. You can open Data Source with below steps:
1. View -> Data Source.
2. Select Main Data Source from drop down and right click on myfields.
3. Click on Add and give filename as Filename in Name field with Text type.

Create Data Connection: Submit
This data connection will submit infopath form when user fills data in the form and click on submit button. Thereafter, the form get submit to the respective Form Library (Form library path given during creation of Submit data connection (We will see it in later step).      Follow below steps to create Submit connection.
  1. Tools -> Data Connections. This will open Data Connection window which will list all data connection in the form.
  2. Click on Add button to add new connection (Submit).
  3. Click on 'Submit data' option on 'Create new connection to' as we are submitting form onto form library and not receiving any data.
  4. Choose Microsoft sharepoint list as we are submitting infopath form onto sharepoint list.
  5. Give your respective form library path (where your form will save) in Document Library text box.
  6. Click on FX button (beside Filename textbox).
  7. Click on 'Insert Field or group' to choose Filename field (created above).
  8. Enter valid Data connection name.
  9. Click Finish button.

Create Data Connection: Generate ID
Data connection is a mandatory step to connect to data source. Data source is the location where data are stored and we need to do some operation with those data (Post or GET). For example: take total nos. of rows (retrieve operation). So, here we will generate auto increment number and append that nos. to Filename when infopath file is saved. Here, we don't have to write query to sharepoint. Instead sharepoint itself gives total count of list items (rows) in given list. Let’s begin with creating Generate ID data connection.

  1. Tools -> Data Connections. This will open Data Connection window which will list all data connection in the form.
  2. Click on Add button to add new connection.
  3. Click on 'Receive data' option on 'Create new connection to' as we getting count from respective form library. Note: Sharepoint define default column know as 'ID' which gives incremental value of forms in form library (same like SQL Server 2005 and later provides).
  4. Chose Microsoft sharepoint list because we will fetch total count from sharepoint list.
  5. Give your respective form library path (where you infopath will fetch total count of form in form library)
  6. Choose your respective Form Library.
  7. Uncheck all check box except ID (this field will give total count of rows in form library)
  8. Click next.
  9. Enter valid Data connection name.
  10. Click Finish button.
  11. Note: I assume you have created Form Library on your sharepoint site. This is the library where your form will be saved with auto generate filename when submit / Save button is clicked.
  12. Now, we have created 2 data connection: Submit & Generate ID. Now, we will bind this connection with Save button. So when Save button is clicked. We will perform two actions:
  13. · Generate ID data connection will get ID from form library and
  14. · Submit the form with metadata and save the form with format (Form-N). N value will be generated by Generate Id data connection.

Business logic on Save button:
  1. Double click on Save button to open its property window.
  2. Click on rules. Rules are event-handler which is executed when some action is perform by users when button is clicked. Every controls on the form are having its respective rules.
  3. Create Rules to generate auto generate filename: In this case, we should create 2 rules.

Rule-1: will execute when we save form for the first time. At this time, we should generate auto-generate filename with unique ID (Generate ID data connection will take care to provide us with unique ID).

Rule-2: will execute when filename is already exists and we just need to save the content back to form library.



Creating 1st Rule:
1. Double click Save button.
2. Click Rules button.
3. Add rules by adding Add button
4. Click Add button
5. Give your Rule name in Rule dialog box.
6. We should add one condition; whether we form filename exists or doesn’t exist. If it doesn’t exist then we should let form know this. So click on Set Condition… button.
7. Condition dialog box will open. Specify Filename field in 1st drop down. Select “is blank” in 2nd drop down.
8. Click Ok to apply this condition.
9. Now, we should set filename with user friendly name, let say filename should be “File-N” (N is auto increment value from document library) e.g. File-1, File-2… Click on “Add Action”. Action dialog box will open.
10. Select “Set a field’s value” from Action drop down.
11. Select Filename field from field drop down.
12. Set value in value drop down by clicking on FX button (beside value text box).
13. Click on Insert function button. Click on concat() function from the list.
14. Double click on 1st parameter in contact() function and add “File-“. As a second parameter, you can double click on it. Select Secondary data source from Data Source drop down and select ID field.
15. Bind ID field inside MAX() function.


16. Click Verify Formula button to verify for errors.
17. Click Ok.
18. It should look like below screen shot.
19. Click on Add Action button to add more action.
20. Select “Submit using data connection” from Action drop down.
21. Select your Data connection name where form will get submitted to forms library.
22. Ok
23. Click on Add Action button to add more action.
24. Select “Close the form” from Action drop down.
25. Ok
26. Ok



Creating 2nd Rule:
Add another rule when Filename already exists. This time we should not set any value in Filename field. But we should only save the content and close the form.
1. Click Rules button.
2. Add rules by adding Add button
3. Click Add button
4. Give your Rule name in Rule dialog box.
5. We should add one condition; whether filename exists or doesn’t exist. So click on Set Condition… button.
6. Select your filename field in 1st drop down and “is not blank” item in 2nd drop down.
7. Click on Add Action button to add more action.
8. Select “Submit using data connection” from Action drop down.
9. Select your Data connection name where form will get submitted to forms library.
10. Ok
11. Click on Add Action button to add more action.
12. Select “Close the form” from Action drop down.
13. Ok
14. Ok



Now, you are done with your stuffs. Execute the form by clicking on File -> Preview. Preview mode is almost same as if you are running form on MOSS. This is one of very good work by Microsoft. This help developer to save lots of time and help them to see the result in a click.

3 comments:

  1. hiii..this article help me so much..thank youuu ::D

    ReplyDelete
  2. Your statement - Note: Sharepoint define default column know as 'ID' which gives total count of forms in form library.

    ID does not really give total count of forms in form library. It only supplies an incremental number, much like what SQL server does for an Identity field.Just because the ID is 3012, does not mean that there really is 3012 items in the library, it just means that's the number given to it because that was next. A person could delete all items thus the count is zero, and the next number might be 3013. Is there really 3013 documents? Nope, just one.

    ReplyDelete