0

I have 4 custom list (test A, Test B, Test c,test D). I need to find current user login belongs to which list in sharepoint. if current user belongs to Test b list i need to show pop up "U belong to Test b team".

Only using Javascript and Jquery in the content editor webpart.

Thanks.

  • So your list contains list of usernames? – Aanchal Jun 6 '14 at 6:24
  • yes my list contains list of user name.totally i have 4 list. i need to check in which list the user name is present. – user876869 Jun 6 '14 at 6:50
1

You can read listitem properties from following code:

function retrieveListItems(siteUrl) {
    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('Announcements');

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml(
        '<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' + 
        '<Value Type=\'Number\'>1</Value></Geq></Where></Query>' + 
        '<RowLimit>10</RowLimit></View>'
    );
    this.collListItem = oList.getItems(camlQuery);

    clientContext.load(collListItem);
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded), 
        Function.createDelegate(this, this.onQueryFailed)
    ); 
}

function onQuerySucceeded(sender, args) {
    var listItemInfo = '';
    var listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        listItemInfo += '\nID: ' + oListItem.get_id() + 
            '\nTitle: ' + oListItem.get_item('Title') + 
            '\nBody: ' + oListItem.get_item('Body');
    }

    alert(listItemInfo.toString());
}

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

Also you can read current user login name from:

<script src="/SiteAssets/jquery.SPServices-2013.02a.js" type="text/javascript"></script>
<script src="/SiteAssets/jquery.js" type="text/javascript"></script>

<script type="text/javascript">
  var userid= _spPageContextInfo.userId;
  var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
  var requestHeaders = { "accept" : "application/json;odata=verbose" };
  $.ajax({
    url : requestUri,
    contentType : "application/json;odata=verbose",
    headers : requestHeaders,
    success : onSuccess,
    error : onError
  });

  function onSuccess(data, request){
    var loginName = data.d.Title;
    alert(loginName);
  }

  function onError(error) {
    alert("error");
  }
</script>

So you can modify the code snippets according to your business logic.

| improve this answer | |
  • yes my list contains list of user name.totally i have 4 list. i need to check in which list the user name is present or not. – user876869 Jun 6 '14 at 7:03
  • From your code i can able to find for only one list (announcements). how i need to check for four lists? – user876869 Jun 6 '14 at 7:12
  • 1
    You can check one by one and replace the list name. – Aanchal Jun 6 '14 at 7:13
0

If you don't mind using thrid party libraries, then I created SharepointPlus that is a JavaScript API to deal with Sharepoint.

With it (and jQuery), the code will be:

// we test each of the lists
$(["Test A", "Test B", "Test C", "Test D"]).each(function(i, list) {
  // we use the WHERE clause "Name = '[Me]'" where "Name" is the column name of the user
  // '[Me]' will automatically target the current user
  $SP().list(list).get({fields:"ID",where:"Name = '[Me]'",rowlimit:1}, function(data) {
    // if data.length is bigger than 0 it means we found the user in that list
    if (data.length>0) alert("You're in the list "+list)
  })
})
| improve this answer | |
0

Check current login user is present in custom list or not

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
    <script type="text/javascript" src="http://azurespdev01:7491/SiteAssets/jquery.SPServices-0.7.2.js"></script>

    <script type="text/javascript">
    // var userId = $().SPServices.SPGetCurrentUser({
    // fieldName: "UserId"});  // U cant match user name record with current user

    $(document).ready(function() {
    var userid = _spPageContextInfo.userId;// use userid to match
    var query = '<Query>' +
    '<Where>' +
    '<Eq>' +
    '<FieldRef Name="Columname" />' +
    '<Value Type="Lookup">' + userid + '</Value>' +
    '</Eq>' +
    '</Where>' +
    '</Query>';

     $().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "ListName",
    CAMLViewFields: "<ViewFields><FieldRef Name='columname' /></ViewFields>",
    completefunc: function(xData, Status) {    
    var matchFound = false; //define flag

    //loop through all rows
    $(xData.responseXML).SPFilterNode("z:row").each(function() {
    var split = ($(this).attr("ows_User"));

    var id = split.split(';')[0];
        if (userid == id) {
            matchFound = true; //set the flag to true indicating matched record
        } 
    });

    if(matchFound)
        document.getElementById("imageHolder").innerHTML="<img src='/YourImage.jpg' alt=''/>";// show ur foto
    else
        document.getElementById("imageHolder").innerHTML="<img src='/NOimage.jpg' alt=''/>";// show others foto
}
});
});

</script>
| 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.