none
SharePoint 2010 Get ID of Selected List Item using C# RRS feed

  • Question

  • Hi All,

    Can anybody help me with this?

    I have a list view, wherein I select many list items. I want to get the ids of the selected list items and depending on those ids I will perform some action.

    Can you all please help me getting ids of selected list items using C#?

    Your help is highly appreciated.

    Thanks & regards,

    Manish

    Thursday, June 27, 2013 9:48 AM

Answers

  • Hi Mahesh/Stefan,

    Thanx for your help. Your suggestions really helped me.

    I have used hidden field for the same.

    in .ascx page i added javascript as below:-

    <asp:HiddenField ID="HdnFldSelectedItemID" runat="server" />

    <script type="text/javascript">
            ExecuteOrDelayUntilScriptLoaded(GetSelectedItems, "sp.js");
            function GetSelectedItems() {
                try {
                    var ctx = SP.ClientContext.get_current();
                    var items = SP.ListOperation.Selection.getSelectedItems(ctx);
                    //alert(items);
                    var myItems = '';
                    var i;

                    for (i in items) {
                        myItems += ';' + items[i].id;
                    }
                    //alert(myItems);
                    document.getElementById("<%= HdnFldSelectedItemID.ClientID %>").value = myItems;

                    if (myItems.toString().length == 0) {
                        alert("Please Select List Item.");                   
                    }      

                    //alert('done');
                }
                catch (e) {
                    alert('Exception Occured' + e.Message);
                }
            }

    and in code behind i.e. .cs file

    List<string> ItemID = new List<string>(HdnFldSelectedItemID.Value.Split(';'));

    foreach (string itemsid in ItemID)

    {some action here}

    Please refer below link for more info:-

    http://ethandeng.blogspot.com/2012/12/program-sharepoint-mulitple-item.html

    http://arsalkhatri.wordpress.com/2012/12/01/sharepoint-2010-get-selected-items-from-list-view-in-visual-web-part/

    Thanks & reagrds,

    Manish

    Thursday, June 27, 2013 3:01 PM

All replies

  • Hi Manibal,

    You can do this by Custom Actions,

    Please go through following article:

    http://ranaictiu-technicalblog.blogspot.sg/2011/05/sharepoint-2010-approvereject-multiple.html

    You can customize the code provided in this article, for your needs. This piece of code returns multiple selected items, You can perform you action on these items.

    This is for multiple selected items:

    //get current list id var listId = SP.ListOperation.Selection.getSelectedList();

    //get all selected list items var selectedItems = SP.ListOperation.Selection.getSelectedItems(ctx);



    Please don't forget to 'mark answer/propose answer' or 'vote as helpful' as appropriate.


    • Edited by Mahesh.Nalam Thursday, June 27, 2013 10:12 AM more info
    Thursday, June 27, 2013 10:08 AM
  • You cannot use it directly in c# you need to use a JavaScript of the SharePoint JavaScript Object Model that you can pass to an application page. SP.ListOperation.Selection.getSelectedList()

    Once you have passed it to the query string of the application page you can reuse it in your code.

    You will also find a complete tutorial on "Get selected items form SharePoint 2010 list".

    Kind regards
    Stefan


    http://www.n8d.at/blog
    Follow me on Twitter: StFBauer | n8design

    Microsoft Community Contributor 2011 / 2012
    MCTS - SharePoint / WSS Configuration and Development

    Thursday, June 27, 2013 10:09 AM
  • Hi Stefan,

    Can you please tell me how to use javascript and pass those ids in code behind.

    Thanks

    Thursday, June 27, 2013 11:24 AM
  • Hi Mahesh,

     

    Can you please tell me how to use javascript and pass those ids in code behind.

     

    Thanks

    Thursday, June 27, 2013 11:25 AM
  • With the Javascript function you will get all selected items in an array. For example 1,2,3,7,20

    This can be passed to an appplication page: http://yourserver/_layouts/doesomething.aspx?selectedIds=1,2,3,7,20

    In the code behind of the article page you can split by comma and know the id's the user have selected.

    Check the "Get selected items from SharePoint 2010 list" this will show how it can be done.

    Kind regards
    Stefan


    http://www.n8d.at/blog
    Follow me on Twitter: StFBauer | n8design

    Microsoft Community Contributor 2011 / 2012
    MCTS - SharePoint / WSS Configuration and Development

    Thursday, June 27, 2013 12:47 PM
  • Hi Mahesh/Stefan,

    Thanx for your help. Your suggestions really helped me.

    I have used hidden field for the same.

    in .ascx page i added javascript as below:-

    <asp:HiddenField ID="HdnFldSelectedItemID" runat="server" />

    <script type="text/javascript">
            ExecuteOrDelayUntilScriptLoaded(GetSelectedItems, "sp.js");
            function GetSelectedItems() {
                try {
                    var ctx = SP.ClientContext.get_current();
                    var items = SP.ListOperation.Selection.getSelectedItems(ctx);
                    //alert(items);
                    var myItems = '';
                    var i;

                    for (i in items) {
                        myItems += ';' + items[i].id;
                    }
                    //alert(myItems);
                    document.getElementById("<%= HdnFldSelectedItemID.ClientID %>").value = myItems;

                    if (myItems.toString().length == 0) {
                        alert("Please Select List Item.");                   
                    }      

                    //alert('done');
                }
                catch (e) {
                    alert('Exception Occured' + e.Message);
                }
            }

    and in code behind i.e. .cs file

    List<string> ItemID = new List<string>(HdnFldSelectedItemID.Value.Split(';'));

    foreach (string itemsid in ItemID)

    {some action here}

    Please refer below link for more info:-

    http://ethandeng.blogspot.com/2012/12/program-sharepoint-mulitple-item.html

    http://arsalkhatri.wordpress.com/2012/12/01/sharepoint-2010-get-selected-items-from-list-view-in-visual-web-part/

    Thanks & reagrds,

    Manish

    Thursday, June 27, 2013 3:01 PM