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>
