1

How to get current (opened) list item using object model. I have a blog which has list named posts. when user opens anyone post, i need to do operation taking that current opened list item. I am using object model and CEWP will be placed on a post page. (the page which comes after opening any post). (I want to refer information of that opened list item in my code.) please help.

thanks, Rajeev

  • 2
    See here: it's available in the page context object. – Markus May 27 '14 at 13:50
7

_spPageContextInfo Object could be used for retrieving information about current page on the client side, specifically pageListId property returns the current List Id. List Item Id could be extracted from a query string in Display/Edit pages, SharePoint client API GetUrlKeyValue function is used that purpose in the below example.

The example demonstrates how to retrieve current List Item via JSOM:

function getCurrentItem(success,error){  
   var context = SP.ClientContext.get_current();
   var web = context.get_web();
   var listId = _spPageContextInfo.pageListId;
   var itemId = parseInt(GetUrlKeyValue('ID'));

  var list = web.get_lists().getById(listId);
  var listItem = list.getItemById(itemId);
  context.load(listItem);

  context.executeQueryAsync(
   function() {
        success(listItem);
   },
   error
  );
}

Usage

getCurrentItem(
  function(item){
       console.log(item.get_item('Title')); 
  },
  function(sender,args){
       console.log(args.get_message());
  }
);
0

There is no way to simply do anything such as '$web.get_ThisOpenItem()'. I think what you need to do is take the page Url & extract an ID number or Item Name (not sure which it would be using). Then query your list for the item.

Below is if your URL will contain the ListItem ID value as a query parameter. If it does not then the page URL your looking at will likely end with '/itemTitle.aspx' & you can grab that value to use an SPQuery on the $list to return your item.

 //something along these lines [javascript]
var context ;
var web ;
var list ;
var item ;
function getInfo(){       
    itemId = GetUrlParam("Id");
    context = new SP.ClientContext.get_current()
    web = context.get_web();
    list = web.get_lists().getByTitle("SOME_LIST_NAME");
    item = list.getItemById(itemId);
    context.load(item);
    context.executeQueryAsync(functionNameOfShowData, functionNameWhenFails);
}
function functionNameOfShowData(){
    var someData = item.get_item("SOME_FIELD_NAME");
}

function GetUrlParam(name){
    var value = null;
    var paramSegment = window.location.href.split("?");
    var paramSet = paramSegment[1].split("&");
    for(var i =0; i<paramSet.length; i++){
        if(paramSet[i].indexOf(name+"=")==0){
             value = paramSet[i].substring(name.length+1)
             i = paramSet.length; //or use break;
        }
    }
    return value;
}
  • hi, thanks for reply. I have sharepoint page url as below: https://*****/sites/******/MyBlog/Lists/Posts/Post.aspx?List=301694f4%2De621%2D4800%2Db490%2D0e966a3eeda9&ID=66&Web=5d8125c6%2D8954%2D48be%2D9210%2D3f92cce6356a. This is the URl of one of the posts. Now I want to get ID of this post. We can see ID is 66 in the URL. how to get it. example will be helpful for me. I have added CEWP on this page only – Rajeev K May 28 '14 at 8:33
  • Example is above. Pass "ID" instead of "Id" to GetUrlParam function I wrote & it should return 66 – user13186 May 28 '14 at 13:48

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.