1

I use SharePoint Online and working in Visual Studio with SharePoint hosted app

I need to get list id and the id of the SELECTED item in the list

And i use a ribbon button to show a dialog and want the id to show in it

This is the code I am using:

var clientContext, hostweburl, parentContext, parentWeb, listItems;

clientContext = new SP.ClientContext.get_current();
hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
parentContext = new SP.AppContextSite(clientContext, hostweburl);
parentWeb = parentContext.get_web();
listItems = parentWeb.get_lists().getByTitle("RibbonTestSimon").getItems("");


$(document).ready(function () {
    clientContext.load(listItems);
    clientContext.executeQueryAsync(OnSuccess, OnFail);
});


function OnSuccess() {
    var listString = "";
    var listEnumerator = listItems.getEnumerator();
while (listEnumerator.moveNext()) {
    var currentItem = listEnumerator.get_current();
    listString += "<br/> " + currentItem.get_item('Title');
}
$('#divAllaItems').html(listString);
}


function OnFail() {
    alert("Error");
}


function getQueryStringParameter(paramToRetrieve) {
    var params =
    document.URL.split("?")[1].split("&");
    var strParams = "";
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] == paramToRetrieve)
            return singleParam[1];
    }
}

This is the code for the ribbon button in this code i have the code that open a dialog box can that be the reason why it isnt work

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction Id="31f85d51-7911-4be4-b56e-0a165adcad5e.RibbonTest"
            RegistrationType="List"
            RegistrationId="100"
            Location="CommandUI.Ribbon"
            Sequence="10001"
            Title="RibbonTest"
            HostWebDialog="TRUE"
            HostWebDialogWidth="480"
            HostWebDialogHeight="240">
<CommandUIExtension>
  <!-- 
  Update the UI definitions below with the controls and the command actions
  that you want to enable for the custom action.
  -->
  <CommandUIDefinitions>
    <CommandUIDefinition Location="Ribbon.ListItem.Actions.Controls._children">
      <Button Id="Ribbon.ListItem.Actions.RibbonTestButton"
              Alt="RibbonTest"
              Sequence="100"
              Command="Invoke_RibbonTestButtonRequest"
              LabelText="RibbonTest"
              TemplateAlias="o1"
              Image32by32=/>
    </CommandUIDefinition>
  </CommandUIDefinitions>
  <CommandUIHandlers>
    <CommandUIHandler
              Command="Invoke_RibbonTestButtonRequest"
              CommandAction="~appWebUrl/Pages/Default.aspx?{StandardTokens}&amp;SPListItemId={SelectedItemId}&amp;SPListId={SelectedListId}" />
  </CommandUIHandlers>
</CommandUIExtension >

2
var clientContext =  SP.ClientContext.get_current(); // Gets current site context
var id =  GetItemID(); //retrieve selected list item id to update data
var listId = SP.ListOperation.Selection.getSelectedList();
var oList = context.get_web().get_lists().getById(listId); 
this.oListItem = oList.getItemById(id);

function GetItemID()
{ 
     var selectedItems = SP.ListOperation.Selection.getSelectedItems(); // Retrieve selected items
     var count = SP.ListOperation.Selection.getSelectedItems().length;// CountDictionary(selectedItems);
     if (count > 0) { return selectedItems[0].id; } else { return null; }
}
  • Hi I cant get it to work where in my code should i put listId, oLists etc pallar inte mer – linkan94 Sep 17 '14 at 9:52
  • This code effectively replaces all of your code, if you want the selected items. – James Love Sep 17 '14 at 9:55
0
    var clientContext, hostweburl, parentContext, parentWeb, listItems;

clientContext = new SP.ClientContext.get_current();
hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
parentContext = new SP.AppContextSite(clientContext, hostweburl);
parentWeb = parentContext.get_web();
var id =  GetItemID(); // Gets Selected item's ID 
var listId = SP.ListOperation.Selection.getSelectedList();
var oList = context.get_web().get_lists().getById(listId); 
this.oListItem = oList.getItemById(id);
//listItems = parentWeb.get_lists().getByTitle("RibbonTestSimon").getItems("");


$(document).ready(function () {
    clientContext.load(listItems);
    clientContext.executeQueryAsync(OnSuccess, OnFail);
});

function GetItemID()
{ 
     var selectedItems = SP.ListOperation.Selection.getSelectedItems(); // Retrieve selected items
     var count = SP.ListOperation.Selection.getSelectedItems().length;// CountDictionary(selectedItems);
     if (count > 0) { return selectedItems[0].id; } else { return null; }
}

function OnSuccess() {
    var listString = "";
    var listEnumerator = listItems.getEnumerator();
while (listEnumerator.moveNext()) {
    var currentItem = listEnumerator.get_current();
    listString += "<br/> " + currentItem.get_item('Title');
}
$('#divAllaItems').html(listString);
}


function OnFail() {
    alert("Error");
}


function getQueryStringParameter(paramToRetrieve) {
    var params =
    document.URL.split("?")[1].split("&");
    var strParams = "";
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] == paramToRetrieve)
            return singleParam[1];
    }
}
  • You shouldnt be putting two answers down to the same question.. You should be editing your origional answer and make any further answers there ;) – Ali Jafer Sep 17 '14 at 10:12
0

This Answer was what i was looking for:

var clientContext, hostweburl, parentContext, parentWeb, selectedItems, itemIds, listId;

clientContext = new SP.ClientContext.get_current();
hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
listId = decodeURIComponent(getQueryStringParameter("SPListId"));
itemIds = decodeURIComponent(getQueryStringParameter("SPListItemId")).split(',');
parentContext = new SP.AppContextSite(clientContext, hostweburl);
parentWeb = parentContext.get_web();
selectedItems = parentWeb.get_lists().getById(listId).getItemById(itemIds);

function itemLoop(color)
{
    var i;
    for (i = 0; i < itemIds.length; i++)
    {
        selectedItems = parentWeb.get_lists().getById(listId).getItemById(itemIds[i]);
        selectedItems.set_item('Color', color);
        selectedItems.update();
        clientContext.load(selectedItems);
    }
    clientContext.executeQueryAsync(function () { window.parent.postMessage('CloseCustomActionDialogRefresh', '*'); }, function (sender, args) { alert('Error:' + args.get_message()); });

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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