Sunday, December 23, 2012

Program SharePoint Mulitple Item Selection in List View

Reference:
http://tomaszrabinski.pl/wordpress/2012/02/25/get-selected-list-items-sharepoint-2010/
http://programmers.stackexchange.com/questions/122357/how-should-data-be-passed-between-client-side-javascript-and-c-code-behind-an-a

This solution involes using SharePoint Client Object Model and jQuery ...

I recently worked on a project that required triggering server side code based on what user selects in a list view. A typical senario is a user select multiple customers from a contact list and send a emails to them with one click.

Just as OOTB of SharePoint, you can select more than one items in a list and click the "Delete Item" button in the ribbon to delete them all.


I need to implement this in a web part instead of add custom action to the ribbon since the portal site we developed won't show ribbon.

To implement this, I have both the XsltListViewWebPart and the custom "Actions" web part provisioned in a web part page.

The custom "Actions" web part is implemented as SharePoint visual web part and in the ascx file, it has

<script type="text/javascript">
 
     function CustomActionOnSelectedItem() {
        // get current client context
        var context = SP.ClientContext.get_current();
        var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
        var selectedList = SP.ListOperation.Selection.getSelectedList(context);
 
        var itemIds = "";
        for (i in selectedItems) {
            itemIds += selectedItems[i].id + ";";
        }
 
        var hiddenFieldId = '<%= HiddenField1.ClientID %>';
        $('#' + hiddenFieldId).val(selectedList + ":" + itemIds);
    }
 
 
 
</script>
 
<asp:HiddenField ID="HiddenField1" runat="server"/>
<asp:Button ID="btOK" runat="server" Text="OK" OnClientClick="CustomActionOnSelectedItem()" onclick="btOK_Click" Width="133px" />
The JavaScript use sSharePoint Client Object Model to get the list ID and the selected item IDs and pass the information to server side through ASP.NET HiddenField control.

One trick to notice is to get the ASP.NET control client side  ID by using inline .NET code



var hiddenFieldId = '<%= HiddenField1.ClientID %>';
If you are not going to have the JavaScript directly in the ascx file and loading it from a seperate js file, then this trick will not work.

If this is the case, you can do the jQuery selection based on class name as

$('.MyHiddenField').val(itemIds);

However if you use this technique, you cannot use the ASP.NET HiddenField control since the it doesn't support "CssClass" attribute.

You can use the HTML input control instead but add runat="Server" to make sure the server side code can get its value.


<input ID="HiddenField1"  type="hidden" runat="server" class="MyHiddenField"/>

The server side will be able to get the item ID as next



HashSet<Guid> itemGuidList = Utility.GetItemGuidList(HiddenField1.Value);

The "GetItemGuidList" is a custom utility method that will parse the item IDs from the string.
        public static HashSet<Guid> GetItemGuidList(string value)
        {
            HashSet<Guid> guidList = new HashSet<Guid>();
 
            if (string.IsNullOrEmpty(value))
                return guidList;
 
            string[] guids = value.Split(';');
            foreach (string idstr in guids)
            {
                try
                {
                    if (idstr == null || idstr.Trim().Length == 0)
                        continue;
                     
                    Guid guid = new Guid(idstr);
                    guidList.Add(guid);
                }
                catch (Exception expt)
                {
                    EventLogger.LogError("Invalid guid : " + idstr + " " + expt.Message);
                }
            }
 
            return guidList;
        }

Happy SharePointing!

1 comment: