Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

code:

<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();
            ctx.load(this.web);
            this.currentLib = web.get_lists().getById(listViewTechnologies);
            ctx.load(currentLib);
            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;
                ctx.load(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?!
                }
            }
    </script>

How could I add my items in the Request now?

Solution:

<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);
            }
        }
</script>
share|improve this question
    
I will post the working code, when it works. –  Maikm88 Sep 10 '12 at 12:29
add comment

1 Answer 1

up vote 1 down vote accepted

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

share|improve this answer
    
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 '12 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. –  Nadeem Yousuf Sep 10 '12 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 '12 at 12:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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