1

At some point in time, I want to update a specific Item in a list.

I Know that I have to use the REST call to update a specific item. Below is my URL.

http://cloudapps.cctspl.com/sites/ClockInClockOut/_api/Web/lists/GetByTitle('ClockTrack')/items(5)

"items(5)" I have hard coded the item ID.

My question is how do I get that ID "items(5)" of a specific Item from a list.

I have a Scenario where I will inserting an Item to a few fields and later I'll be updating that Item with additional data.

my code for inserting data to list.

 public void insertDataItems()
{

    try {
        CookieManager cookieManager = (CookieManager)CookieHandler.getDefault();
        cookies = cookieManager.getCookieStore().getCookies();
       // String body ="{ '__metadata': { 'type': 'SP.Data.ClockTrackListItem' },'UId' : '171028', 'CurrentDate' : '2016-08-05T04:00:00Z' ," +
       //         "'TimeIn' : '10:12:12' , 'TimeOut': '22:12:12' ,'TotalTime': '10:12:12' ,'Title': 'Dharma'}";
        String body ="{ '__metadata': { 'type': 'SP.Data.ClockTrackListItem' },'UId': '32156','TimeIn' : '06:00:00' , 'TimeOut': '08:00:00' ,'TotalTime': '02:00:00'}";


        Uri uri = Uri.parse("http://cloudapps.cctspl.com/sites/ClockInClockOut/_api/Web/lists/GetByTitle('ClockTrack')/items(5)");
        URL url = new URL(uri.toString());
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setDoInput (true);
        urlConnection.setDoOutput(true);
        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestProperty("Accept", "application/json;odata=verbose");
        urlConnection.setRequestProperty("content-type", "application/json;odata=verbose");
        urlConnection.setRequestProperty("X-RequestDigest",getRequestDigest());
        urlConnection.setRequestProperty("X-HTTP-Method","MERGE");
        urlConnection.setRequestProperty("IF-MATCH","*");

        urlConnection.connect();
        OutputStream os = urlConnection.getOutputStream();
        os.write(body.getBytes());
        os.flush();
        os.close();
        int resCode = urlConnection.getResponseCode();
        Log.d("Response Code :" ," "+resCode);
        InputStream inputStream = urlConnection.getInputStream();
        BufferedReader reader =new BufferedReader(new InputStreamReader((inputStream)));
        StringBuffer buffer = new StringBuffer();
        String line;
        while((line = reader.readLine()) != null)
        {
            buffer.append(line + "\n");
        }
        String restResult = buffer.toString();
        Log.d("----Buffer String-----:" , restResult);
        JSONObject jobj = new JSONObject(restResult).getJSONObject("d");
    }
    catch (Exception e)
    {
        Log.v("Retrieve List", e.toString());
        e.printStackTrace();
    }

}
9
  • 1
    Post code on how you are inserting the item Aug 14, 2016 at 11:43
  • Assuming that you know the filter criteria for the item, you can use caml to get the item reference and then get the item id from it.
    – nbi
    Aug 14, 2016 at 11:59
  • @nbi I don't know how to filter out a field with null value.Is there any way to capture the item Id while Inserting the data for the first time.
    – Dharma Raj
    Aug 14, 2016 at 12:03
  • @Amal Hashim I have update the code.
    – Dharma Raj
    Aug 14, 2016 at 12:03
  • 1
    @DharmaRaj that code only have update logic. How are you inserting the data. If I am not mistaken you want to insert and item and then later update the same item? Aug 14, 2016 at 12:15

2 Answers 2

0

Here is the example how to get the ID in the first call after you insert item in the list. Later you update the record with your above code.

You can use either REST call or JSOM. i have shown here JSOM.

var itemID  =""; function logTicket() {

    var clientContext = SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('List Name');

    var itemCreateInfo = new SP.ListItemCreationInformation();
    this.oListItem = oList.addItem(itemCreateInfo);
    oListItem.set_item('Title', '');    
    oListItem.update();

    clientContext.load(oListItem);
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onticketSucceeded),
        Function.createDelegate(this, this.onQueryFailed)
    );
}


function onticketSucceeded() {

    itemID  = oListItem.get_id();
    console.log(oListItem.get_id());  
    //Here is the ID of newly inserted item in the list and assigned to global var itemID

}


function onQueryFailed(sender, args) {
    //alert('Failure');         
}

 $(document).ready(function () {        
    $(".attachment").click(function(){
       logTicketOnServerFromAttchment();

    }); });
0

Below mentioned example shows that the retrieval of list Items using SharePoint REST:

url: http://site url/_api/web/lists/GetByTitle('Test')/items
method: GET
headers:
    Authorization: "Bearer " + accessToken
    accept: "application/json;odata=verbose" or "application/atom+xml"

If you want to retrieve the specific list item, then you can check the below code:

url: http://site url/_api/web/lists/GetByTitle('Test')/items(item id)
method: GET
headers:
    Authorization: "Bearer " + accessToken
    accept: "application/json;odata=verbose" or "application/atom+xml"

For more detailed information you can go through this :

https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-lists-and-list-items-with-rest

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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