cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Workflow Hero

Copy task attachments to Master Item on O365...

Jump to solution

Hi all,

 

just to give you the context, my company is working on a 300 workflows tenant splitted into a lot of site collections.

We have built a component workflows called "Copy TaskAttachments to Master Item" that copies the task attachments to the underlying item. It also update the NFFormData field to put the attachment not only in the "default" attachment components, but exactly where the attachment should be in the form (this part is not detailled in the post).

 

It works 95% of the time ! I'm here for the 5 last %

 

First, a few tips, if you want to do it too...

  1. Inside a component workflow, First do a POST webrequest call on {Variable:Current Site URL}‌/_api/contextinfo to retrieve the FormDigest.
  2. Retrieve names and URL of your task attachments using another GET webservice call on "{Variable:Current Site URL}‌/_api/Web/lists/getbytitle('‎‏{Variable:Workflow Tasks List Name}‌')/items(‎‏{Variable:Task Item ID}‌)/AttachmentFiles
    2019-02-16_14-31-24.png

     

    Retrieve URLS and files and store them in some collections...
  3. Create and delete a Dummy file (just to ensure that the "Attachment" folder is created using another webservice call  on
    ‎‏{Variable:Current Site URL}‌/_api/web/lists/GetByTitle('‎‏{Variable:List Name}‌')/items(‎‏{Variable:List Item ID}‌)/AttachmentFiles/add(FileName='dummy.txt')
  4. Then loop on those collections and call the POST HTTPService
    ‎‏{Variable:Current Site URL}‌/_api/web/getfilebyserverrelativeurl('‎‏{Variable:Attachment URL}‌')/copyto(strnewurl='‎‏{Variable:List Relative URL}‌/Attachments/‎‏{Variable:List Item ID}‌/‎‏{Variable:Attachment File Name}‌',bOverWrite=true)
    and for that use the following header
    2019-02-16_14-37-11.png
    You retrieved the {FormDigest} from the initial call.
  5. Package it...test it... it fails if you run it on another site/sitecoll with the status code "Forbidden" on the CopyTo service call ! Basically, your remote workflow app doesn't have the correct access on the local site. To solve this problem, follow the link :  https://docs.microsoft.com/en-us/sharepoint/dev/general-development/create-a-workflow-with-elevated-...
  6. You have now given the correct rights to your app... and now it should run properly.
  7. Here is my problem now...

    It runs most of the time, and for a few cases , the copyto Webservice call gives us an "Unauthorized" status code without explanation.

... we have been investigating for weeks now, and I have no clue on the solution!

We tried the following :

  • The formdigest is maybe wrong,but why then it would work on all the hundreds of other workflows?
  • The permissions have been updated on the site : Can't be...I have tried with my own admin account, same behavior.
  • We checked and the "workflow can use app permissions" feature is activated everywhere.

any idea would be appreciated!

thanks!

Labels: (1)
0 Kudos
Reply
4 Replies
Workflow Hero

Re: Copy task attachments to Master Item on O365...

Jump to solution
Hi Alex,

You mention a that this fails in a few cases, is it the same workflow/users/site where this fails, or is it randomly failing (e.g. same user runs the same workflow 20 times and one time it fails).

Thanks,

Callum
0 Kudos
Reply
Workflow Hero

Re: Copy task attachments to Master Item on O365...

Jump to solution
Hi Callum,
sadly not! Any user running some of those workflows are having the issue. Even me with my site coll admin account.
So, I wouldn't try to investigate on the user.


0 Kudos
Reply
Workflow Hero

Re: Copy task attachments to Master Item on O365...

Jump to solution

This was a tricky one, but I think I solved your issue!

The issue is because the REST call is limited to 400 characters, so when setting the URL, the variables are replaced and you can have a really long URL.
Just imagine doing this on a subsite ("/sites/MyProjectSite1/MyProjectSite11/MyProjectSite111"), add the length of the list name and the name of the file ("maybe_you_have_a_really_long_file_name.pdf"), it can become huge quite quickly.

The trick is to use REST query with parameters.
Instead of :

{Variable:Current Site URL}‌/_api/web/getfilebyserverrelativeurl('‎‏{Variable:Attachment URL}‌')/copyto(strnewurl='‎‏{Variable:List Relative URL}‌/Attachments/‎‏{Variable:List Item ID}‌/‎‏{Variable:Attachment File Name}‌',bOverWrite=true)

Use:

{Variable:Current Site URL}‌/_api/web/getfilebyserverrelativeurl(@v0)/copyto(strnewurl=@v1,boverwrite=true)?@v0='‎‏{Variable:AttachmentURL}‌'&@v1='‎‏{Variable:List Relative URL}‌/Attachments/‎‏{Variable:List Item ID}‌/{Variable:Attachment File Name}‌'

The parameters (@v0 and @v1) are not counted in the the length of the request, only what's before the "?". That's because it is not replaced at runtime by Nintex, but is interpreted by the SharePoint API, it accepts long URLs, but not long function calls.
This did the trick for me.

Reply
Workflow Hero

Re: Copy task attachments to Master Item on O365...

Jump to solution
mate, how could I do without you ?
Smiley Wink
0 Kudos
Reply