0

I would like getting a specific item from a specific list but i don't have ID, just a field (column) value.

Here's my list: enter image description here

On the site, i have only the name...i'd like to recover the paid leave field value.

Here's what i tried to do but...

SPWeb thisWeb = SPContext.Current.Web;
SPUser currentUser = thisWeb.CurrentUser;
SPQuery myQuery = new SPQuery();
myQuery.Query = "<Where><Eq><FieldRef Name='User'/>" +
                "<Value Type='Text'>"+ currentUser.Name +"</Value></Eq></Where>";
SPListItemCollection _items = thisWeb.Lists["Sumarry"].GetItems(myQuery);
1

You must change your query to:

"<Where><Eq><FieldRef Name='User' />"+
      "<Value Type='User'>"+ currentUser.LoginName +"</Value></Eq></Where>"

See the link for details

  • Thanks...but now how can i get the value of the Paid Leave column? – n0r1s Nov 25 '13 at 16:43
  • SPListItemCollection _items = thisWeb.Lists["Sumarry"].GetItems(myQuery); _items[0]["Paid Leave"]; – Andrey Ungur Nov 25 '13 at 16:45
0

Code to get the data from list using ecma script object model.

<SharePoint:ScriptLink Name="SP.js" runat="server" OnDemand="true" Localizable="false" />
<SharePoint:FormDigest runat="server" />

<script src="../jquery-2.1.0.min.js" type="text/javascript"></script>
<script type="text/javascript">

    var rowscount = 1;

    function retrieveCurrentListProperties() {
      var  clientContext = new SP.ClientContext.get_current();
        this.web = clientContext.get_web();
        var list = web.get_lists().getByTitle("News");
        var camlQuery = new SP.CamlQuery();
        //var q = "<View><Query><Where><Eq><FieldRef Name='End_x0020_Date'/><Value Type='DateTime'><Today/></Value></Eq></Where></Query></View>";
        //var q = "<View><Query><Where><Eq><FieldRef Name='ApprovalStatus'/><Value Type='Text'>Approve</Value></Eq></Where></Query></View>";
        var q = "<View><Query><Where><Eq><FieldRef Name='ApprovalStatus'/><Value Type='Text'>Approve</Value></Eq></Where></Query><RowLimit>" + rowscount + "</RowLimit></View>";
       // var q = "<View></View>";
        camlQuery.set_viewXml(q);
        this.listItems = list.getItems(camlQuery);
        clientContext.load(this.listItems);
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onSuccess),
        Function.createDelegate(this, this.onFail));

    }

    function onSuccess(sender, args) {
        //  alert('web title:' + this.list.get_ApprovalStatus() + '\n ID:' + this.list.get_id() + '\n Created Date:' + this.list.get_created());
        // alert("success " + this.listItems.get_id() + "  arbs " + args);

        var linkUrl = "";
        var imageUrl = "";
        var listEnumerator = this.listItems.getEnumerator();
        while (listEnumerator.moveNext()) {
            var currentItem = listEnumerator.get_current();
            linkUrl = currentItem.get_item('Title');
           // imageUrl = currentItem.get_item('ImageURL');
           //  document.write('<img src="' + imageUrl + '"+>');
           // alert(linkUrl);
            $('#maindiv').append("<div>" + linkUrl + "</div>");
        }
    }
    function onFail(sender, args) {
        alert('failed to get list. Error:' + args.get_message());
    }

   SP.SOD.executeFunc('sp.js', null, function() {
        retrieveCurrentListProperties();
    });

    function filldata() {
        // alert("called");
        $('#maindiv').empty();
        rowscount = rowscount + 1;
        retrieveCurrentListProperties();
    }
</script>
<input id="Button1" type="button" onclick="filldata()"  value="Loadmore" />
<div id="maindiv">   </div>

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.