SP.ClientContext get list items

Hi,

I'm not sure and I want to ask, if I want to get List items in client side, then these code works all user if the list has unique permission? example the list is a Parameter list - only admin can edit and maybe see, and I want to use this script in other list new form

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());
}

Thanks,

Mykie

February 9th, 2015 3:08am

Hi Mykie,

You can use the same script for any other list, just change the list name and change the column names which you want to use.  If the logged in user has at least read permission on the list, then he is able to get the data.

Hope it answers your query.

Please don't forget to mark it as answered, if your problem resolved or helpful.

Free Windows Admin Tool Kit Click here and download it now
February 9th, 2015 3:47am

Thanks for your reply!

If the user not has permission, can we use Permission delegate like in c#

SPSecurity.RunWithElevatedPrivileges(delegate(){}; ?

February 9th, 2015 3:59am

Hi ,

SharePoint JavaScript client object model code runs using the permissions of the current user logged in. There is no way to elevate privileges of the user as is done in Server Object model code.

Thanks,

Nadeem

Free Windows Admin Tool Kit Click here and download it now
February 9th, 2015 4:18am

Hi Mykie,

You can create a SharePoint Hosted app and give app only permission to simulate the similar functionality such as RunwithElevatedPreviliges as in C#.

Please refer to the following article.

App Only Permission Policy

Update: #My aplogizes, please use the Provider hosted app to simulate such functionality.

Please don't forget to mark it answered, if your problem resolved or helpful.


February 9th, 2015 4:26am

Hi Mokhtar,

App only policy cannot work in SharePoint Hosted apps. The App Only policy works by using a separate OAuth token. Since OAuth is required to generate the app token, the App-Only policy can only be used in the Auto-hosted and Provider-Hosted apps. 

Thanks,

Nadeem

Free Windows Admin Tool Kit Click here and download it now
February 9th, 2015 5:17am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics