Pergunta

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.

Foi útil?

Solução

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a sharepoint.stackexchange
scroll top