Reputation: 1541
I need to get info for a user stored in a SharePoint list, such as title, id, and loginName.
Basically my situation is this: I have an "Organization Contacts" list that includes the Office365Account for a particular user, and a Manager that is associated with that user. Both of these fields are SharePoint "Person" fields.
The code below is my attempt to:
I know something's wrong with the technique I'm trying because the app freezes up when the line starting with var managerName
is uncommented. Chrome Dev tools seem to be telling me that all other lines are working, or at least not causing errors, so I expect the issue is with the var managerName
line.
function getManagerNameforModal() {
var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
var hostContext = new SP.AppContextSite(currentContext, hostUrl);
var hostweb = hostContext.get_web();
var list = hostweb.get_lists().getByTitle(contactsListName);
//A caml query to get manager name for the record where user is equal to current user.
var queryXml = "<View><Query><Where><Eq><FieldRef Name='Office365Account' /><Value Type='Integer'><UserID /></Value></Eq></Where></Query></View>"
var query = new SP.CamlQuery();
query.set_viewXml(queryXml);
var items = list.getItems(query);
var managerName = items.get_item["Manager"].get_title(); //This doesn't work.
currentContext.load(items);
currentContext.executeQueryAsync(onGetManagerNameSuccess, onGetManagerNameFail);
Upvotes: 2
Views: 3273
Reputation: 59328
Your example contains the following issues/typos :
First, items
is a SP.ListItemCollection, so in order to get item you could use the following example:
for(var i = 0;i < items.get_count();i++) {
var item = items.getItemAtIndex(i);
}
Second, the appropriate syntax to access SP.ListItem.item Property in JSOM is:
var managerName = item.get_item("Manager"); //not a square brackets
Third, since JSOM is an async API, list items collection have to be requested first using SP.ClientContext.executeQueryAsync
method, and only then you could access it using successHandler
:
context.executeQueryAsync(function(){
for(var i = 0;i < items.get_count();i++) {
var item = items.getItemAtIndex(i);
var managerName = item.get_item("Manager");
}
},
function(){
console.log('Error');
});
Forth, managerName
is of SP.FieldUserValue
type that does not contain property Title
var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
var hostContext = new SP.AppContextSite(currentContext, hostUrl);
var hostweb = hostContext.get_web();
var list = hostweb.get_lists().getByTitle(contactsListName);
//A caml query to get manager name for the record where user is equal to current user.
var queryXml = "<View><Query><Where><Eq><FieldRef Name='Office365Account' /><Value Type='Integer'><UserID /></Value></Eq></Where></Query></View>"
var query = new SP.CamlQuery();
query.set_viewXml(queryXml);
var items = list.getItems(query);
currentContext.load(items);
currentContext.executeQueryAsync(function(){
for(var i = 0;i < items.get_count();i++) {
var item = items.getItemAtIndex(i);
var managerName = item.get_item("Manager").get_lookupValue();
}
},
function(sender,args){
console.log(args.get_message());
});
Upvotes: 2