Hi Cem,
Thank you for reaching out on Microsoft Q&A!
You've requested a Teams session with me to solve the issue. To help others, and for them to be able to re-use (parts) of the solution I've added the steps we've discussed below:
- Your list reference in your source list contains an URL with a lot of information. We need to create another URL based on the original one. For this we use the split() function to split on the slash. With this we cut the URL into pieces which we use to create a new URL
- The 'Get Lists' action in SharePoint only returns the display name of the list, while the internal name (which is in the URL) differs. Therefor we need to take another approach to get the correct list on the destination site
- You want a specifc naming convention for the attachments in the destination list item attachment
The steps we took to solve your issue:
- Because your list URL which you filled in the source contains the internal list name, and this is not responded by the 'Get Lists' action (it only contains the displayName) we've used the 'Send HTTP request to SharePoint' action with your newly created site URL using split:
concat('https://',split(body('Get_item_2')['SharePointListe'],'/')[2],'/sites/',split(body('Get_item_2')['SharePointListe'],'/')[4])
For which we used the endpoint:
/_api/Web/Lists
- This returns all list details, also containing the field 'EntityTypeName' with the internal list name, appended by the word 'List'. To filter the correct list detail we've used the 'Filter array' action filtering on the results of the HTTP request to SharePoint using the array below:
body('Send_an_HTTP_request_to_SharePoint')['d']['results']
Where we did an exact match on the internal list name appended with the word 'List' using the concat() function like this:
concat(split(body('Get_item_2')['SharePointListe'], '/')[6],'List')
- Now we've got our destination site and list, we can create a new list item on that list using the 'Create Item' action. For this we just reuse the newly created URL and retrieve the list id where to create the item using:
first(body('Filter_array_3'))['Id']
- As we've got our new item created now we can start adding attachments. You've already added a 'Get Attachments' action in your case to retrieve attachments from the source list, which we'll reuse for this purpose. We're going to loop over them to be able to use the 'Get Attachment Content' action which actually retrieves the files from the source list
- Next step is to write the attachments to the destination list. We can use the 'Add Attachment' action for this, were we're reusing the newly created URL again with the list ID and the newly created item ID from the destination list. The contents we can use from the 'Get Attachment Content' action of the source, and for the filename we've initialized a variable called 'indexNumber' with initial value 1.
We're using this value to create the naming convention for your filename, together details from the source list, derived from the trigger. We're also using the displayName of the filename, to split it at the '.' and take the last part, which leaves the file extension. The complete expression is as follows:
concat(triggerBody()?['Nummer'],'-',triggerBody()?['Title'],'-',variables('indexNumber'),'.',last(split(items('For_each_2')['DisplayName'],'.')))
- Last step is to increment the variable 'indexNumber' with 1 for the next iteration. Please note that we've set the concurrency control of the 'For Each' action to 1 for this to function properly (using variables in a loop is always something to carefully consider). In this case we needed the concurrency control also for writing attachments to the list item, as we'd otherwise would run into conflicts on the SharePoint actions.
Cem, would you please be so kind to attach the screenshots of the current solution below for future reference, to also help others? Thanks a lot for your time! It was nice working with you.
Please click “Accept answer” if you find this helpful. Feel free to drop additional queries in the comments below!
Kind regards,
Sonny