1

I am trying to set up a simple subscription tool where users can subscribe to or unsubscribe from various distribution list. The idea is that you select a user through a people picker ('subuser'), select a list ('sublist') and click either subscribe (so you are added to the selected list) or unsubscribe (so you are deleted from the selected list). I have succeeded with the subscribe part, but got stuck at unsubscribe. I know how to delete an item from a list, but to do that I need to have an item's ID (or is there any other way?). This is the part of my code that I want to use to get the ID for the item that includes a user selected via the people picker, but itemID is undefined:

function removeUser()     
{    
var clientContext = new SP.ClientContext(siteurl);    
var value1 = document.getElementById('subuser').value;    
var value2 = document.querySelector('input[name="sublist"]:checked').value;    
var queryLookup = "<Where><Eq><FieldRef Name='UserName' LookupId='TRUE' /><Value Type='Lookup'>"+ value1 +"</Value></Eq></Where>";
var itemID="";

$().SPServices({    
    operation: "GetListItems",    
    async: false,    
    listName: value2,    
    CAMLViewFields: "<ViewFields><FieldRef Name='ID'/></ViewFields>",    
    CAMLQuery: queryLookup,    
    completefunc: function(xData,Status) {    
        itemID = $(xData.responseXML.xml).find("z\\:row").attr("ows_ID");
        }
    });
}

SPServices can be replaced by any other working solution. Thank you for any suggestions.

1

Here is the sample code using that we can get last item ID using REST API coding :

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> 
<script type="text/javascript">
    function getListData() { 
        $.ajax({ 
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('ListA')/items?$select=ID,Title",
            type: "GET",
            headers: { "accept": "application/json;odata=verbose", "content-type": "application/json;odata=verbose", },
            success: onQuerySucceeded, error: onQueryFailed
        });
    }
    function onQuerySucceeded(data) {
        console.log(data.d.results);
        $("#newsitems").append('<table>');
        $("#newsitems").append('<tr><td style="font-weight:bold">ID </td><td style="font-weight:bold">Title </td></tr>');
        $.each(data.d.results, function (key, value) {
        $("#newsitems").append('<tr>'); 
        $("#newsitems").append('<td>'+value.ID+ '</td>'); 
        $("#newsitems").append('<td>'+value.Title+ '</td>');                                    
        $("#newsitems").append('</tr>')                  });
        $("#newsitems").append('</table>');
    }
    function onQueryFailed() {
        alert('Sorry An Error Has Occurred!');
    }
    ExecuteOrDelayUntilScriptLoaded(getListData, "sp.js")
</script>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
<div id="newsitems"></div>

Reference URL :

https://social.msdn.microsoft.com/Forums/office/en-US/e9964e5a-5496-44ba-a969-f239df1b2651/sharepoint-and-rest-api-trying-to-display-list-item-id?forum=sharepointdevelopment

| improve this answer | |
  • Thanks, this way I can indeed get items' IDs from a list. However, I have two issues: 1. Is it possible to get data from a person/group column? It seems I cannot do that. In the example above we have "ID" and "Title" columns, it works, when I add another text or numeric column, it works, but it doesn't work for a person/group column. 2. I have the list of IDs, but how can I get from this the ID of the item that includes a particular value? A user gives their email through filling in a form, I need to get the ID of the item that includes this email. Can I somehow query this fetched data? – nutshell99 Feb 23 at 21:27
  • Yes, we can get person or group column as well - there we need to use the expand attribute in the select query, please see the below example how it works : google.com/amp/s/sharepointeducation.wordpress.com/2016/07/19/… – Habibur Rahaman Feb 24 at 1:23
  • And at the end end of your REST query in the select parameter you can add "and emailid (email column) ne null (or" ") " - this will return the result which has email id – Habibur Rahaman Feb 24 at 1:30
  • Getting there! One more obstacle: I can send the relevant ID to the console with the above code ('$("#newsitems").append('<td>'+value.ID+ '</td>');'), however, how can I assign this ID to a variable? – nutshell99 Feb 26 at 18:15
  • Just create a variable var myId =Value. Id or use this example – Habibur Rahaman Feb 26 at 18:40

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.