5

Using Javascript CSOM and want to return items created by logged in user, but the code below returns all items. Could someone advise?

var camlQuery = new SP.CamlQuery();

var specifier = "<Eq><FieldRef Name='Author'/><Value Type='Integer'><UserID/></Value></Eq>";

camlQuery.set_viewXml = "<View><Query><Where>" + specifier + "</Where></Query></View>"; 

this.collListItem3 = oList.getItems(camlQuery);

UPDATE

var oList = clientContext33.get_web().get_lists().getByTitle("MyAppList");

var camlQuery = new SP.CamlQuery();

camlQuery.set_viewXml = '<Where><Eq><FieldRef Name="Author" LookupId="TRUE" /><Value Type="Integer"><UserID/></Value></Eq></Where>'; 

this.collListItem3 = oList.getItems(camlQuery);

clientContext33.load(collListItem3,'Include(Title,ID,App_x003a_Link,App)');

clientContext33.executeQueryAsync(Function.createDelegate(this, this.onQueryAppSucceeded), Function.createDelegate(this, this.onQueryAppFailed));        

}

but still shows all items

  • 1
    You still need to have <View><Query>YourCAML</Query></View> – Anders Aune Aug 7 '15 at 13:28
6
 var context = new SP.ClientContext.get_current();
    var list = context.get_web().get_lists().getByTitle("ListName");
    var cQuery = new SP.CamlQuery();
    var camlXML = "<View><Query><Where><Eq><FieldRef Name='Author' LookupId='True'/><Value Type='Lookup'>" + _spPageContextInfo.userId + "</Value></Eq></Where></Query></View>";
    cQuery.set_viewXml(camlXML);
    var listitems = list.getItems(cQuery);
    context.load(listitems);
    context.executeQueryAsync(function() {
            var enumerator = listitems.getEnumerator();
            while (enumerator.moveNext()) {
                var item = enumerator.get_current().get_fieldValues();
                console.log(item);
            }
        },
        function(s, a) {
            console.error(a.get_message());
        });
  • Although this (currently accepted) answer should function as well, I prefer the answer of Yayati (that means, using the <UserID/> CAML tag in the comparision, instead of relying the _spPageContextInfo variable). The _spPageContextInfo might not exist on the page, depending in which context you use your script. – pholpar Oct 3 '18 at 11:57
4

The Author column is of LookUp Type. Here is the complete list.

var ctx = new SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle("Vacation Replacement");
var query = new SP.CamlQuery();
var specifier = "<Where><Eq><FieldRef Name='Author' LookupId='True'/><Value Type='Lookup'><UserID/></Value></Eq></Where>";
query.set_viewXml("<View><Query>"+specifier+"</Query></View>");
var collListItems = list.getItems(query);
ctx.load(collListItems);
ctx.executeQueryAsync(
    function(){
        var enumerator = collListItems.getEnumerator();
        while(enumerator.moveNext()){
            var item = enumerator.get_current();
            alert("ID : " + item.get_id());
        }
    },
    function(sender,args){
        alert("Request Failed."+args.get_message() + "\n" + args.get_stackTrace());
    }
);
2

Create a custom view of the list and select radio button

Show items only when the following is true: created by is equal to type [Me]

save the list. enter image description here

 function getItemsFromView()
{
var context = new SP.ClientContext.get_current();
    var list = context.get_web().get_lists().getByTitle('ListName');
    var view = list.get_views().getByTitle('CustomViewName');
    context.load(view);

    context.executeQueryAsync(
        function(sender, args) {OnSuccess("<View><Query>" + view.get_viewQuery() + "</Query></View>")},
        function(sender, args) {alert("error: " + args.get_message());}
    );
}

It will fetch your records of logged in user only.

Its Done!!

2

To get current user Id

clientContext = new SP.ClientContext.get_current();
 var user=clientContext.get_web().get_currentUser();
 clientContext.load(user);
clientContext.executeQueryAsync(function(){
 var userId=user.get_id();

 var query="<Where><Eq>"+
    "<FieldRef Name="Author" LookupId="TRUE" />"+
     "<Value Type="Integer">" +userId + "</Value></Eq></Where>";

},failCallbackMethod);

This link might help you

  • here author is using client object model so "SPContext.Current.Web.CurrentUser.ID" will not work – Kalpesh Vaghela Aug 7 '15 at 12:30
  • Oh! will update the answer – nbi Aug 7 '15 at 12:31
  • And one could switch +userid+ with <UserID/> for current user. The OP didn't add LookupId="TRUE" in his CAML – Anders Aune Aug 7 '15 at 12:43
  • Thanks for help. I'm using camlQuery.set_viewXml and trying to use my existing code but i still see all items, see update question in OP pls. – Orange Juice Jones Aug 7 '15 at 13:13

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.