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
Related
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 have a requirement to change the value of a SharePoint List Workflow Status Column.
Lets assume we could have 2 to many workflow columns in one list (The below code works when a list has 1 workflow status column).
The issue is when it has multiple Workflow Status Column, it updates only the last column as there are nested executeQueryAsync calls.
I have tried the same with Deferred/Promise as well - see the second code block.
<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(initialize, "sp.js");
//Get our objects
function initialize() {
var t0 = performance.now();
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getById(_spPageContextInfo.pageListId)
var fieldCollection = list.get_fields();
var spField;
context.load(fieldCollection);
context.executeQueryAsync(Function.createDelegate(this, onFieldCollectionSucceeded), Function.createDelegate(this, onListDataFailed));
function onFieldCollectionSucceeded(sender, args) {
var fieldEnumerator = fieldCollection.getEnumerator();
while (fieldEnumerator.moveNext()) {
var field = fieldEnumerator.get_current();
var fType = field.get_fieldTypeKind();
//Value 28 correspond to SPWorkflowStatus
if (fType === 28) {
console.log(fieldEnumerator.get_current().get_title() + ": " + fType);
spField = field;
context.load(spField, "SchemaXml");
context.executeQueryAsync(Function.createDelegate(this, onFieldSucceeded), Function.createDelegate(this, onListDataFailed));
}
}
}
function onFieldSucceeded(sender, args) {
console.log("Schema changes processing for column: " + spField.get_title());
var schema = spField.get_schemaXml();
var newSchema = schema.replace('Abgelehnt', 'Rejected');
spField.set_schemaXml(newSchema);
spField.update();
context.executeQueryAsync(
Function.createDelegate(this, schemaChanged),
Function.createDelegate(this, onListDataFailed));
};
function schemaChanged(sender, args) {
console.log('Field Schema Updated');
}
function onListDataFailed(sender, args) {
console.log('List Data fetch failed. ' + args.get_message() + 'n' + args.get_stackTrace());
};
var t1 = performance.now();
console.log("Total Execution Time " + (t1 - t0) + " milliseconds.")
};
<script type="text/javascript">
//http://johnliu.net/blog/2015/12/convert-sharepoint-jsoms-executequeryasync-to-promise-in-the-prototype
ExecuteOrDelayUntilScriptLoaded(registerJsomPromise, "sp.js");
ExecuteOrDelayUntilScriptLoaded(initialize, "sp.js");
function initialize() {
var t0 = performance.now();
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getById(_spPageContextInfo.pageListId)
var fieldCollection = list.get_fields();
var spField;
context.load(fieldCollection);
context.executeQueryAsync(Function.createDelegate(this, onFieldCollectionSucceeded), Function.createDelegate(this, onListDataFailed));
};
function onFieldCollectionSucceeded(sender, args) {
var fieldEnumerator = fieldCollection.getEnumerator();
while (fieldEnumerator.moveNext()) {
var field = fieldEnumerator.get_current();
var fType = field.get_fieldTypeKind();
//Value 28 correspond to SPWorkflowStatus
if (fType === 28) {
console.log(fieldEnumerator.get_current().get_title() + ": " + fType);
spField = field;
context.load(spField, "SchemaXml");
var promise = context.executeQuery();
setTimeout(function () { }, 500);
promise.done(function () {
console.log("Schema changes processing for column: " + spField.get_title());
var schema = spField.get_schemaXml();
var newSchema = setSchema(schema);
spField.set_schemaXml(newSchema);
spField.update();
context.load(spField);
context.executeQueryAsync(
Function.createDelegate(this, schemaChanged),
Function.createDelegate(this, onListDataFailed));
});
promise.then(function (sArgs) {
console.log('Field Schema Updated');
//sArgs[0] == success callback sender
//sArgs[1] == success callback args
}, function (fArgs) {
//fArgs[0] == fail callback sender
//fArgs[1] == fail callback args.
//in JSOM the callback args aren't used much -
//the only useful one is probably the get_message()
//on the fail callback
var failmessage = fArgs[1].get_message();
});
//context.executeQueryAsync(Function.createDelegate(this, onListDataSucceeded), Function.createDelegate(this, onListDataFailed));
}
}
}
function schemaChanged(sender, args) {
console.log('Field Schema Updated');
}
function onListDataFailed(sender, args) {
console.log('List Data fetch failed. ' + args.get_message() + 'n' + args.get_stackTrace());
};
var t1 = performance.now();
console.log("Total Execution Time " + (t1 - t0) + " milliseconds.")
function registerJsomPromise() {
SP.ClientContext.prototype.executeQuery = function () {
var deferred = $.Deferred();
this.executeQueryAsync(
function () { deferred.resolve(arguments); },
function () { deferred.reject(arguments); }
);
return deferred.promise();
};
}
The execute query async changes the state of iterator. This is why you have a weird behavior. You should load your fields, update those, and do one single execute at the end out of the while to actually update stuff. The only issue with that approach is that if you're code fails at some point, all the updates will be pending. So careful error handling is required.
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.');
}
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.