Skip to main content
Nintex Community Menu Bar
Question

Best Approach to Updating Attachments in SharePoint via K2 Form

  • February 17, 2025
  • 2 replies
  • 27 views

Forum|alt.badge.img+5

I have created a K2 form that allows users to upload up to 10 attachments. Upon submission, these attachments are saved to a SharePoint library using the UploadDocuments method of the SmartObject.

During the upload process, I check if each attachment control contains a file and then call the UploadDocuments function accordingly. This process is repeated for all ten attachments.

Now, I need to handle the scenario where a user opens the form in edit mode and wants to update the attachments.

My Attempt:

I tried detecting changes using the rule "When a control in the view raises an action", but this event is triggered as soon as the form retrieves the attachments from SharePoint.

My Question:

  • What is the correct approach to updating attachments in the SharePoint library when the form is in edit mode?
  • Do I need to delete all existing attachments and re-upload them each time the form is edited, or is there a more efficient method to detect and update only the changed files?

Any guidance or best practices would be greatly appreciated!

Translate
Did this topic help you find an answer to your question?

2 replies

Forum|alt.badge.img+5
  • Nintex Employee
  • 46 replies
  • February 24, 2025

When editing a form with attachments already loaded from SharePoint, the key is to distinguish between the initial load of existing attachments and genuine user-initiated changes. Rather than deleting and re-uploading every attachment on every edit, you can implement a more targeted approach.

Recommended Approach

  1. Flag or Track Changes:
    Instead of relying on the file control’s “raise action” event—which fires during the initial load—you can introduce a flag (or a hidden field) that indicates when a user has actually modified a file control. For example, the flag would only be set when the user explicitly selects a new file.

  2. Compare Original and New Data:
    When the form is loaded, store the original file metadata (such as file name, size, or an identifier) in a hidden field or variable. Upon submission, compare the current file control’s value with the stored original. Only if there’s a difference should you trigger an update for that specific attachment.

  3. Update Only Changed Attachments:
    For each attachment control, if a change is detected:

    • If a file is replaced: Call the SmartObject’s UploadDocuments function to update the file.
    • If a file is removed: You might need to call a delete function or adjust your SharePoint record accordingly.
    • If no change is detected: Leave the existing attachment untouched.
  4. Manage Initial Data Load:
    Use a form-level flag (like “IsFormLoaded”) that’s set only after the form has fully loaded from SharePoint. Modify your rules so that the “control raised an action” event only acts if this flag indicates the form is no longer in its loading phase. This prevents the event from triggering on initial data retrieval.

Step-by-Step Implementation

  • On Form Load:

    • Retrieve and display existing attachments.
    • Store relevant attachment metadata (e.g., file names, IDs) in hidden fields.
    • Set a flag (e.g., IsFormLoaded = true) once all initial data is loaded.
  • On File Control Change:

    • Check if IsFormLoaded is true. If it is, then the change is likely user-initiated.
    • Set a secondary flag for that specific file control to indicate it has been modified (e.g., Attachment1Changed = true).
  • On Form Submission:

    • For each attachment control, check if its “changed” flag is set.
    • For changed attachments, call the SmartObject’s UploadDocuments method.
    • For unchanged attachments, simply leave the SharePoint record as is.
Translate

MillaZ
Nintex Employee
Forum|alt.badge.img+21
  • Nintex Employee
  • 652 replies
  • March 3, 2025

Hi ​@N-I-1990 has this reply answered your question? 

Translate

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie Settings