Monday 30 June 2014

USING SMARTOBJECTS IN INFOPATH FORMS TO UPLOAD FILE ATTACHMENTS TO SHAREPOINT


CREATING UP THE SHAREPOINT DOCUMENT LIST
I’ll first create up a new document library to store my file attachments.
View All Site ContentCreateDocument Library
Name: FileAttachments
CREATE THE FILE ATTACHMENT SMARTOBJECT
Site Actions --> K2 Site Settings --> K2 SmartObject Site Lists and Libraries (SmartObject Management)
Select document library and click create
You can test Smart Object using ServiceBroker
c:\Program Files\K2 blackpearl\ServiceBroker
1. Let’s create up blank form template.
2. Now I am going to create up a couple of fields that I need in my main data source. Click on Data Source on the side pane.
3. First create the repeating node for the file attachment. Right click on the myFields node and select Add Field or Group. Create the repeating node as shown on the right. I am calling it fileAttachment.
4. Next add a file attachment field. I am calling mine file.
5. Next add a field for the URL link to the document after we upload it to SharePoint. I am calling this urllink.
6. The last field we add is used as a trigger field to fire the SmartObject call for each line item when we submit the form. I’ll call this field trigger and set an initial value of 0 to it.
7. I am then going to drag the file field to the form as a repeating file attachment control. I’ll also add a button control to do the form submission.
8. I’ll drag the urllink to the bottom of the form as a repeating expression. This will be used to show how the URLs are created after the submission. Now note that in a real life case, the URL listing would be on the approval view.
9. Now let’s add the SmartObject integration to the form. First save and close the form. Then right click on the form and select Integrate with SmartObject
10. Now add in the Create method for the SmartObject we created earlier.
11. Click Finish to complete the SmartObject integration.
12. Now right click on the template file and select Design. First change the Submit button rules to set the trigger field to 1 when clicked on. This is one part of the trick to cause the rule processing to fire for each row.Go to Button Properties.
13. Under Rules, configure an action to set the trigger field to 1.
14. Next let’s set a rule under the trigger field properties.
15. Click the Add button to add a rule.
16. Under the condition, configure the rule to only fire when trigger is equal to 1. This is the other part of the trick which works in combination with the other rule that we created on the Submit button. So when we click on the Submit button, this rule will fire for every single line item in this repeating group.
17. Next configure the first action in the rule to copy the file attachment data to the input field of the SO create method data source. The field is called content.
18. The final setting should look like this. Click OK.
19. Next add an action to execute the SmartObject create function.
Next add an action to set the urllink field to contain the returned URL link. Note that we will use the following Formula to strip off the surrounding HTML tags that won’t be used.
20. substring-before(substring-after(LinkToItem_K2_ServiceDefined, ""),"")
21. Next add an action to remove the file data from the InfoPath form. Set the file field to an empty value. This is important as the file data will still be in the InfoPath form if you do not do this.
TESTING THE FORM
Now we have completed setting up the basic functionality of how we can upload multiple documents in the
InfoPath form to SharePoint using SmartObjects.
1. I first open up my form and I then attach 2 text files for testing.
2. Click Submit. Note the first time executing this might be a bit slower when the SmartObject web service is being called the first time (it should be quicker on the subsequent calls). Now notice how the two attachment files are removed and note the URL links listed under the Submit button.
3. Now if you look at the document library that created earlier. We can now see the 2 file attachments that uploaded via the form. Cool!