0

I need to get item from list sharepoint by Id I use this code and it give me all the items of the list :

<script>

function parseURLParams(url) {
        var queryStart = url.indexOf("?") + 1,
            queryEnd = url.indexOf("#") + 1 || url.length + 1,
            query = url.slice(queryStart, queryEnd - 1),
            pairs = query.replace(/\+/g, " ").split("&"),
            parms = {}, i, n, v, nv;

        if (query === url || query === "") {
            return;
        }

        for (i = 0; i < pairs.length; i++) {
            nv = pairs[i].split("=");
            n = decodeURIComponent(nv[0]);
            v = decodeURIComponent(nv[1]);

            if (!parms.hasOwnProperty(n)) {
                parms[n] = [];
            }

            parms[n].push(nv.length === 2 ? v : null);
        }
        return parms;
    }
 $( window ).load(function() {
  var urlString = document.URL;
 urlParams = parseURLParams(urlString);

ExecuteOrDelayUntilScriptLoaded(retrieveActualite, "sp.js");

        var siteUrl = '/sites/securite';

    function retrieveActualite() {

            var clientContext = new SP.ClientContext(siteUrl);
            var oList = clientContext.get_web().get_lists().getByTitle('Actualite');
            var camlQuery = new SP.CamlQuery();

            camlQuery.set_viewXml('<Query><Where><Eq><FieldRef Name=”ID” LookupId="TRUE"/><Value Type="Text">' + urlParams.IdArt[0] + '</Value></Eq></Where></Query>');
            this.collListItem = oList.getItems(camlQuery);
            clientContext.load(collListItem);
            clientContext.executeQueryAsync(onQuerySucceededActualite, onQueryFailed);
        }

        function onQuerySucceededActualite(sender, args) {

            var listItemInfo = '';

            var listItemEnumerator = collListItem.getEnumerator();

           while (listItemEnumerator.moveNext()) {
                var oListItem = listItemEnumerator.get_current();


                var desc=oListItem.get_item('Description');
                $('#idArticle').append('<label class="titleArticle">'+oListItem.get_item('Title')+'</label> <br><br><p class="ContenuArticle" >'+desc+'</p>');             
            }
        }

        function onQueryFailed(sender, args) {

            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }


    });



</script>

so how can I get only the considered item ?

  • I think it should be <FieldRef Name="ID"><Value Type="Integer">. No LookupId attribute, and type integer. – Jesús López Feb 25 '15 at 16:21
  • Did you realice ID is enclosed between strange characters? ”ID”, It should be "ID". – Jesús López Feb 25 '15 at 16:23
  • I tried <Value Type="Integer"> but it still give me all list items – Imen Turki Feb 25 '15 at 16:28
  • Did you remove LookupId attribute? – Jesús López Feb 25 '15 at 16:29
  • 1
    Yes Vadim, I should add the root – Imen Turki Feb 25 '15 at 17:20
7

Value for SP.CamlQuery.viewXml property should always contains the View root element:

<View>
   <Query>...</Query>
</View>

There is no need to perform CAML query to return List Item by id, use SP.List.getItemById Method for that purpose.

Secondly, SharePoint JavaScript library already includes functions for parsing query string, for example GetUrlKeyValue.

Example

function getListItem(listId,itemId,success,error)
{
   var ctx = SP.ClientContext.get_current();
   var web = ctx.get_web();
   var list = web.get_lists().getById(listId);
   var item = list.getItemById(itemId);
   ctx.load(item);

   ctx.executeQueryAsync(
      function() {
          success(item); 
      },
      error);
}





var listId = _spPageContextInfo.pageListId;
var itemId = parseInt(GetUrlKeyValue('ID'));

getListItem(listId,itemId, 
   function(item){
       console.log(item.get_item('Title'));
   },
   function(sendera,args){
       console.log(args.get_message());
   });
0

Lets say that you need three fields corresponding to the list item you are searching for - name, age, city. Here is how the code for the same looks like, assuming 'Name', 'Age' and 'City' are the internal names -

var itemId = 'yourItemID';   
var listName = 'yourListName';
var targetListItem;

function getListDetails(itemId) {
  var clientContext = new SP.ClientContext(); 
  var targetList = clientContext.get_web().get_lists().getByTitle(listName);
  targetListItem = targetList.getItemById(itemId);
  clientContext.load(targetListItem, 'Name');
  clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {   
   var myName = targetListItem.get_item('Name');
   var myAge= targetListItem.get_item('Age');
   var myCity= targetListItem.get_item('City');
   alert('Name: ' + myName +'\n'+ 'Age: ' + myAge + '\n' + 'City: '+ myCity);
}

function onQueryFailed(sender, args) {
   alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}

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.