I am trying to read a List Properties using SharePoint ECMA but could not success.
Anyone can help - here is the code - tried with method get_fieldValues and get_item and both have returned "undefined".
var key = "vti_level";
function getListProperty() {
var clientContext = new SP.ClientContext();
var listGuid = GetUrlKeyValue('List', window.location.href);
this.props = clientContext.get_web().get_lists().getById(listGuid).get_rootFolder();
clientContext.load(this.props);
clientContext.executeQueryAsync(Function.createDelegate(this, getListPropertySuccess), Function.createDelegate(this, getListPropertyFailed));
}
function getListPropertySuccess() {
var propKey1 = this.props.get_properties().get_fieldValues()[key];
var propKey2 = this.props.get_properties().get_item(key);
}
function getListPropertyFailed() {
alert('Request failed on getListProperty.');
}
try this:
var key = "vti_level";
function getListProperty() {
var clientContext = new SP.ClientContext();
var listGuid = _spPageContextInfo.pageListId;
this.props = clientContext.get_web().get_lists().getById(listGuid).get_rootFolder().get_properties();
clientContext.load(this.props);
clientContext.executeQueryAsync(Function.createDelegate(this, getListPropertySuccess), Function.createDelegate(this, getListPropertyFailed));
}
function getListPropertySuccess() {
var propKey1 = this.props.get_fieldValues()[key];
var propKey2 = this.props.get_item(key);
}
function getListPropertyFailed() {
alert('Request failed on getListProperty.');
}
Related
Is there a nicer way to flatten two multi-value fields from a Notes document into a map collection in SSJS?
so far I came to the following:
var vw:NotesView = database.getView("configuration");
if(null != vw){
var doc:NotesDocument = vw.getFirstDocument();
if (null != doc){
var itemname1:string = "userName";
var itemname2:string = "inboxFile";
if(doc.hasItem(itemname1) && doc.hasItem(itemname2)){
var itemvalues1:java.util.Vector = doc.getItemValue(itemname1);
var itemvalues2:java.util.Vector = doc.getItemValue(itemname2);
if(itemvalues1.size() == itemvalues2.size()){
var map = new java.util.HashMap();
var iterator1 = itemvalues1.iterator();
var iterator2 = itemvalues2.iterator();
}
while (iterator1.hasNext()) {
var itemvalue1 = iterator1.next();
var itemvalue2 = iterator2.next();
if ((typeof(itemvalue1)).endsWith("string")) {
map.put(itemvalue1.left(80), itemvalue2.left(80));
}
}
sessionScope.put("setupInbox",map)
}
}
}
If the JavaScript forEach construct is supported you can use this:
function mergeVector(keyVector, valueVector) {
var map = new java.util.HashMap();
keyVector.forEach(function(curKey, index) {
if (index < valueVector.size()) {
map.put(curKey, valueVector.get(index));
}
});
return map;
}
otherwise use:
function mergeVector(keyVector, valueVector) {
var map = new java.util.HashMap();
for(var index = 0; index < keyVector.size(), index++) {
if (index < valueVector.size()) {
map.put(keyVector.get(index), valueVector.get(index));
}
};
return map;
}
Hope this helps
I'm building an app using firebase and Node.js. I need to get data from nested foreach. How to do it correctly? Need to return the results of all iterations simultaneously.
exports.userParty = function (userInfo, cb) {
var userID = userInfo.userID;
var clubID = userInfo.clubID;
var refUserParty = ref.child(userID).child('my_party_id');
var party = {};
refUserParty.orderByValue().once("value", function (snapshot) {
var party = {};
snapshot.forEach(function (partyID) {
var refParty = dbb.ref('clubs').child(clubID).child('party').child(partyID.val());
refParty.once('value', function (partyBody) {
party[partyID.val()] = partyBody.val();
//console.log(party);
});
});
cb(party); // {}
});
};
You need to call the callback after all the async functions in the forEach block have completed. You can do this using a simple counter to check all async functions are complete:
...
let completedSnapshots = 0;
snapshot.forEach(function (partyID) {
var refParty = dbb.ref('clubs').child(clubID).child('party').child(partyID.val());
refParty.once('value', function (partyBody) {
party[partyID.val()] = partyBody.val();
completedSnapshots++;
if (completedSnapshots === snapshot.val().length) {
cb(party);
}
});
});
...
I am enumerating all terms using SP.Taxonomy.js JSOM in SharePoint.While enumerating I want to check if currentTerm has children or not.I need some property to check like children count.How can I do this with minimum round trip to server.
I am using following code get taxonomy and it is working fine.
Please help
$(document).ready(function () {
ExecuteOrDelayUntilScriptLoaded(function () {
SP.SOD.registerSod('sp.taxonomy.js', "/_layouts/15/sp.taxonomy.js");
`SP.SOD.executeFunc('sp.taxonomy.js', false, Function.createDelegate(this,`
function () {
var context = SP.ClientContext.get_current();
var taxonomySession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
var termStore = taxonomySession.get_termStores().getByName("Taxonomy_qeFlDdEX32yZ3Q7EpEIeMQ==");
var termSet = termStore.getTermSet("ed6d3beb-6a49-4444-bc5d-456f747e139d");
var terms = termSet.getAllTerms();
context.load(terms);
context.executeQueryAsync(Function.createDelegate(this, function (sender, args) {
var termsEnumerator = terms.getEnumerator();
var menuItems = new Array();
while (termsEnumerator.moveNext()) {
var currentTerm = termsEnumerator.get_current();
var targetGroups = document.getElementById("selectTaxonomy");
var taxoGroup = document.createElement("option");
taxoGroup.text = currentTerm.get_name();
targetGroups.add(taxoGroup);
}
}), Function.createDelegate(this, function (sender, args) {
alert('The error has occured: ' + args.get_message());
}));
}));
},"sp.js")
});
You could use SP.Taxonomy.Term.termsCount property to get the number of child Term objects, for example:
var termSetId = '--guid goes here--';
getTerms(termSetId,
function(terms){
for(var i = 0;i < terms.get_count();i++){
var term = terms.get_item(i);
var hasChildTerms = (term.get_termsCount() > 0);
//...
}
},
function(sender,args)
{
console.log(args.get_message());
});
where
function getTerms(termSetId,success,error) {
var context = SP.ClientContext.get_current();
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
var termStore = taxSession.getDefaultSiteCollectionTermStore();
var termSet = termStore.getTermSet(termSetId);
var terms = termSet.getAllTerms();
context.load(terms);
context.executeQueryAsync(function () {
success(terms)
},
error);
}
Some recommendations
1) Prefer SP.SOD.loadMultiple function for loading multiple libraries, for example
SP.SOD.registerSod('SP.ClientContext', SP.Utilities.Utility.getLayoutsPageUrl('sp.js'));
SP.SOD.registerSod('SP.Taxonomy.TaxonomySession', SP.Utilities.Utility.getLayoutsPageUrl('sp.taxonomy.js'));
SP.SOD.loadMultiple(['SP.ClientContext', 'SP.Taxonomy.TaxonomySession'], function(){
//your code goes here
});
2) Avoid any hard-coding, for example:
/_layouts/15/sp.taxonomy.js -> SP.Utilities.Utility.getLayoutsPageUrl('sp.taxonomy.js')
Replace the line:
var termStore = taxonomySession.get_termStores().getByName("Taxonomy_qeFlDdEX32yZ3Q7EpEIeMQ==");
with this one:
var termStore = taxonomySession.getDefaultSiteCollectionTermStore();
Function.createDelegate Function could be avoided in most cases
I need to get all root folders of lists on current web, with shrepoint client object model.
I try to use this code, but i have error
var context = SP.ClientContext.get_current();
var lists = context.get_web().get_lists();
context.load(lists);
context.executeQueryAsync(function (sender, args) {
var enumerator = lists.getEnumerator();
while (enumerator.moveNext()) {
var list = enumerator.get_current();
var rootFolder = list.get_rootFolder();
context.load(rootFolder, 'ServerRelativeUrl');
context.executeQueryAsync(
function (sender, args) {
//error
var url = rootFolder.get_serverRelativeUrl();
console.log(url);
},
function (sender, args) {
console.log('error');
});
}
},
function (sender, args) {
console.log('error');
});
Thanks
This error occurs because List.RootFolder property has not been initialized since it was not requested.
In order to load List.RootFolder replace the line:
context.load(lists);
with this one:
context.load(lists,'Include(RootFolder)');
But the specified example contains another flaws:
Since SP.ClientContext.executeQueryAsync method is async and
it is used in a loop, it will not print all folders as you expect
it to
There is no need to perform a subsequent query for ServerRelativeUrl
property
Below is demonstrated the fixed version that prints root folders for all lists:
var context = SP.ClientContext.get_current();
var lists = context.get_web().get_lists();
context.load(lists,'Include(RootFolder)');
context.executeQueryAsync(function () {
var enumerator = lists.getEnumerator();
while (enumerator.moveNext()) {
var list = enumerator.get_current();
var rootFolder = list.get_rootFolder();
var url = rootFolder.get_serverRelativeUrl();
console.log(url);
}
},
function (sender, args) {
console.log('error');
});
I havent found a specific example of how to get the current user and then check if it belongs to a specific sharepoint group, as I havent found anything I cant provide a code,
help on the right direction is appreciated.
SharePoint 2013 CSOM
Prerequisites: compatible with SharePoint 2013 CSOM API only since
SP.GroupCollection.getByName Method is not available in
SharePoint 2010
How to check if current user belongs to SharePoint group via CSOM (JavaScript):
function IsCurrentUserMemberOfGroup(groupName, OnComplete) {
var currentContext = new SP.ClientContext.get_current();
var currentWeb = currentContext.get_web();
var currentUser = currentContext.get_web().get_currentUser();
currentContext.load(currentUser);
var allGroups = currentWeb.get_siteGroups();
currentContext.load(allGroups);
var group = allGroups.getByName(groupName);
currentContext.load(group);
var groupUsers = group.get_users();
currentContext.load(groupUsers);
currentContext.executeQueryAsync(OnSuccess,OnFailure);
function OnSuccess(sender, args) {
var userInGroup = false;
var groupUserEnumerator = groupUsers.getEnumerator();
while (groupUserEnumerator.moveNext()) {
var groupUser = groupUserEnumerator.get_current();
if (groupUser.get_id() == currentUser.get_id()) {
userInGroup = true;
break;
}
}
OnComplete(userInGroup);
}
function OnFailure(sender, args) {
OnComplete(false);
}
}
Usage
IsCurrentUserMemberOfGroup("Approvers", function (isCurrentUserInGroup) {
if(isCurrentUserInGroup)
{
//...
}
});
SharePoint 2010/2013 CSOM
function isUserMemberOfGroup(userId, groupId, success,error) {
var ctx = SP.ClientContext.get_current();
var allGroups = ctx.get_web().get_siteGroups();
var group = allGroups.getById(groupId);
ctx.load(group,'Users');
ctx.executeQueryAsync(
function(sender, args) {
var userInGroup = findUserById(group.get_users(),userId);
success(userInGroup);
},
error);
var findUserById = function(users,id){
var found = false;
var e = group.get_users().getEnumerator();
while (e.moveNext()) {
var user = e.get_current();
if (user.get_id() == id) {
found = true;
break;
}
}
return found;
};
}
Usage
var currentUserId = _spPageContextInfo.userId;
var groupId = 4;
isUserMemberOfGroup(currentUserId, groupId,
function (isCurrentUserInGroup) {
if(isCurrentUserInGroup)
console.log('Current user is a member of Owners group');
else
console.log('Current user is not a member of Owners group');
},
function(sender,args){
console.log(args.get_message());
});
Here's a quicker way with SharePoint 2013:
function CheckCurrentUserMembership() {
var clientContext = new SP.ClientContext.get_current();
this.currentUser = clientContext.get_web().get_currentUser();
clientContext.load(this.currentUser);
this.userGroups = this.currentUser.get_groups();
clientContext.load(this.userGroups);
clientContext.executeQueryAsync(OnQuerySucceeded);
}
function OnQuerySucceeded() {
var isMember = false;
var groupsEnumerator = this.userGroups.getEnumerator();
while (groupsEnumerator.moveNext()) {
var group= groupsEnumerator.get_current();
if(group.get_title() == "Administrator Group") {
isMember = true;
break;
}
}
OnResult(isMember);
}
function OnQueryFailed() {
OnResult(false);
}
If anyone is interested. This approach can be used when you want to check if a user is a member of a group using the group name.
var currentUserIsMemberOf = function(groupName){
var found = false;
var dfd = $.Deferred(function(){
SP.SOD.executeOrDelayUntilScriptLoaded(function(){
context = new SP.ClientContext.get_current();
allGroups = context.get_web().get_siteGroups();
context.load(allGroups);
context.load(allGroups, 'Include(Users)');
context.executeQueryAsync(
function(){
var groupsEnumerator = allGroups.getEnumerator();
while (groupsEnumerator.moveNext()) {
var group = groupsEnumerator.get_current();
if(group.get_title() == groupName) {
var usersEnumerator = group.get_users().getEnumerator();
while (usersEnumerator.moveNext()) {
var user = usersEnumerator.get_current();
if(user.get_id() == _spPageContextInfo.userId) {
found = true;
break;
}
}
}
}
dfd.resolve(found);
},
function(){
dfd.reject(args.get_message());
}
);
}, 'sp.js');
});
return dfd.promise();
}
You can use like this
currentUserIsMemberOf("Members of Demo").done(function(result){
alert(result)
});
Note this code use Promise, you can reference jQuery use your own custom Deferred object or remove Deferred object.