I develop a WebPart using VS2010 for a SharePoint page. My problem: I have to submit the selected items of the ListView to the WebPart code behind. The best solution would be to do it via the Request or Requestheaders. For this I use the ClientObjectModel in a script embedded in the ascx file of my WebPart.


<SharePoint:ListView runat="server" ID="listViewTechnologies" />
...some other html here...
    <script type="text/C#">
            var itemsArray = new Array();
            var ctx = new SP.ClientContext.get_current();
            this.web = ctx.get_web();
            this.currentLib = web.get_lists().getById(listViewTechnologies);
            var selectedItems = SP.ListOperation.Selection.getSelectedItems(ctx);
            var item;
            for (item in selectedItems) {
                var id = selectedItems[item].id;
                var Listitem = currentLib.getItemById(id);
                this.itemsArray[this.itemsArray.length] = Listitem;
// code to add my items to the Request?!

            ctx.executeQueryAsync(Function.createDelegate(this, this.OnSuccess),
                    Function.createDelegate(this, this.OnFailed));

            function OnSuccess(sender, args) {
                for (i = 0; i < this.itemsArray.length; i++) {
                    var item = this.itemsArray[i];
// code to add my items to the Request?!

How could I add my items in the Request now?


<asp:ImageButton runat="server" ID="ibtnAdd" ImageUrl="~/_layouts/images/CALADD.GIF" OnClientClick="ibtnAdd_OnClientClick()" ToolTip="Technologien hinzufügen" />
...some other html here...
    <script type="text/javascript">
        function ibtnAdd_OnClientClick() {
            var selectedItems = ctx.dictSel;
            var itemIds = '';
            var currentItem;
            var containsItems = false;
            for (currentItem in selectedItems) {
                itemIds += selectedItems[currentItem].id + ",";
                if (!containsItems) {
                    containsItems = true;
            if (containsItems) {
                // trim trailing ',' from end
                itemIds = itemIds.substring(0, itemIds.length - 1);
                __doPostBack("ibtnAdd", itemIds);
  • I will post the working code, when it works.
    – Maikm88
    Sep 10, 2012 at 12:29

1 Answer 1


You can use javascript __dopostback for this

__doPostBack('RibbonDemo', itemIds + ";" + listId);

In the page load event you can read the items

if (this.Page.Request["__EVENTTARGET"] == "RibbonDemo")
    string parameter = this.Page.Request["__EVENTARGUMENT"];
    int index = parameter.IndexOf(';');
    string listID = parameter.Substring(index + 1);
    string itemIds = parameter.Substring(0, index);
    string[] itemIdArray = itemIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
    // Further code implementation

For more information you can refer: http://sharepointnadeem.blogspot.in/2012/07/invoke-server-side-code-on-sharepoint_14.html

  • At the moment I don't have a custom ribbon and for my purposes this would be a little bit overpowered. The other problem would occure when I use js: I don't know wether the code to read out these ListItems would still work.
    – Maikm88
    Sep 10, 2012 at 10:54
  • What I mean to say here is that after you are done reading your list items, you can force the page postback using __dopostback and read the items in the page load of the webpart control. You don't have to do anything with ribbon. Since I had written the blog post I simply copied it from there. Sep 10, 2012 at 12:20
  • Perfectly, I tried something and now nearly works. The suggestion with the doPostback method is the perfect solution. Thanks a lot.
    – Maikm88
    Sep 10, 2012 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.