List control onchange event delete

Hi all, I have a list control with a people field.
I want to remove the user from a sharepoint group once deleted from the list but if I use the onchange event "delete" when I try to find the user it doesn't exists anymore in the site due to it's been already deleted.

Is there any method to have a before delete event in list controls?

Thank you

Dear @Pedro,
How are you deleting the user, on a form or via List View? In short, there's no good way to retrieve data once it's deleted.

An alternative might be to add a Mark for deletion column, and if Marked: Yes, then first delete user from a SharePoint group, and then delete the item. Similar approach is suggested here - Solved: Remove user from SharePoint group when item is del... - Power Platform Community

I'm deleting from the list control itself:
image

So there's not a before save trigger I can use to retrieve user name, right?.

You suggest me to hide "Delete" button and create a new one to remove the selected items from the sharepojnt group and the list by code, aren't you?

Regards

Dear @Pedro,
It seems like you're using Delete button from List or Library control - in that case, you can re-write the code that runs on Delete: instead of deleting the item, mark it for deletion and hide from view, the rest can be done without extra code in Power Automate. Here's how to work with buttons - List or Library control — SharePoint forms

You can also use pnpJS to update Mark for deletion column - PnPjs Library — SharePoint forms

Either way, this will require several steps to make it work, even if there was a beforeDelete trigger.

Thank you @Nikita_Kurguzov , I've achieve it re-writing the Delete button's function.
This function first of all remove selected users from the Sharepoint group and later deletes the selected items.
Works like a charm.

Dear @Pedro,
Great to hear it! If you can share the code, please, consider doing so here - other users might be interested in your case, and it will be great help for the community! Of course, only if you don't mind.

Sure here is the final solution, hope it helps.:

    var delButton = fd.control('TablaMiembros').buttons.find(function(b) {
        return b.id === 'DELETE';
    });
    
    delButton.click = function(){
        let items = fd.control('TablaMiembros').selectedItems;
        let strGrupo = fd.field("Title").value.toString().replaceAll(" ","").normalize("NFD").replace(/[\u0300-\u036f]/g, "");
        items.forEach(function(item){
            //Get list item from list
            pnp.sp.web.lists.getByTitle("Miembros").items.getById(item.ID).get().then(function(Participante){
                    //Get the user from corresponding column
                    pnp.sp.web.getUserById(Participante.MiembroId).get().then(function(Usuario){
                        //Remove Users from Sharepoint Group
                        pnp.sp.web.siteGroups.getByName(strGrupo).users.removeByLoginName(Usuario.LoginName).then(function(d){
                            console.log('User removed from group');
                        });
                        //Remove users from List
                        pnp.sp.web.lists.getByTitle("Miembros").items.getById(item.ID).delete().then(function(e){
                            console.log('Participant deleted from list');
                            fd.control('TablaMiembros').refresh();
                        });
                    });
                });
            
        });
    };
}

1 Like