Modern SharePoint Online List View Web Part Use JavaScript to Get Selected Item

Tom Molskow 386 Reputation points
2023-06-08T19:57:20.98+00:00

Hello Community!

I am working in SharePoint Online Modern, and I have a list view web part. I also have a Modern (SPFx) Script Editor Web Part. My question is can I capture the selected item values in the List View Web Part using JavaScript? If so - how?

Has anyone else done this? If so then please provide code examples and guidance.

Thanks!

Tom

Microsoft 365 and Office Development Office JavaScript API
Microsoft 365 and Office SharePoint Development
Microsoft 365 and Office SharePoint For business Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2023-06-09T02:36:23.1266667+00:00

    Hi @Tom Molskow ,

    If you want to get the selected items from the list view, you can use the SP.ListOperation.Selection.getSelectedItems() to get the selected items using Javascript. Please refer to following code

    function clickMethod() {
    var ctx = SP.ClientContext.get_current();
    var items = SP.ListOperation.Selection.getSelectedItems(ctx);
    var mySeletedItems = '';
     var i;
     for (i in items)
     {
          mySeletedItems += '|' + items[i].id;
     }
    alert (mySeletedItems);
    }
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.