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

Tom Molskow 381 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

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,702 questions
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
876 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,676 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 31,606 Reputation points Microsoft Vendor
    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.