I need to calculate a value for a custom ListItem field, based on some values on the same form and also on some values from other lists. I should be able to do this, using ECMA Script. I started by getting the current user (I need it to query my other lists).

Now, I am trying to get my current List Item, so I can gather the fields required for the calculation.

Is there any way to do that? Am I going for the right approach.

Thank you! Ana


I am not sure if I understood your question correctly but if you want to get the current selected item's ID then you can use below code

var ctx = SP.ClientContext.get_current();
var items = SP.ListOperation.Selection.getSelectedItems(ctx);

using above ID you can get listitem. Below is the URL on how to get list items in JavaScript http://msdn.microsoft.com/en-us/library/hh185007(v=office.14).aspx

If you are adding any CustomAction in ECB then you can use token {ItemId}. Refer below URL to know more on tokens http://msdn.microsoft.com/en-us/library/jj163816.aspx

| improve this answer | |
  • Thank you. I tried the code and the SP.ListOperation.Selection.getSelectedItems(ctx) returns an empty object. Maybe I didn't explain very well my scenario. Basically, when I save a new list item, I want to fill in a field, based on some other fields on the same form. – Ana Oct 28 '13 at 13:45
  • Joe's answer will be correct if you want to update any text field on form, but not sure if this will work with controls like TaxonomyField, PeopleEditor. – Vikram Shityalkar Oct 29 '13 at 10:07

Based on your reply to Vikram, i.e. updating fields before saving a new list item, you could include an additional click handler for the save controls on your page. A new item doesn't receive an ID until after it has been saved.

jQuery example:

    //Update some fields here before the default save
    $("#someFieldID").val("New Value");

This should grab any save button, including the ribbon save, of a default form. If you have a custom element, just update the selector. The script inside here will execute before heading out to the built in click handler.

| improve this answer | |

I was looking for a way to access current page field values in SP 2010 and came across this question.

There are basically two ways I found to use fields of the current PAGE:

  1. Just use the web control field value rendering as you would in any page (if you only want to display/check a value).

    console.log(' < SharePointWebControls:TextField FieldName="FormMailContact" runat="server" InputFieldLabel="FormMailContact" / > ');
  2. Alternatively you can take the long road and get the value based on the current list and page id in an async call:

    $(document).ready(function () { ExecuteOrDelayUntilScriptLoaded(getItem, "sp.js");   });
    var item;
    function getItem(){
       var clientContext = new SP.ClientContext.get_current();
       var web = clientContext.get_web();
       var list = web.get_lists().getById(_spPageContextInfo.pageListId);
       this.item = list.getItemById(_spPageContextInfo.pageItemId);
       clientContext.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this.onFailure));
    function onSuccess(){
    function onFailure(){
| improve this answer | |

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.