Requirement:

When a form is loaded, user wants to set the default value of a ‘Division’ dropdown depending on the division he/she belongs to.

 

Solution:

After several vain attempts to set the value of dropdown, I finally(justifying the featured image here) figured out how to and had to share!

To get logged in user info and the how-tos of which SharePoint group the current user belongs to, check the previous post series

  1. https://swethasankaran.com/2016/08/01/users-and-groups-in-nintex-forms-for-office-365-part-1-get-current-user
  2. https://swethasankaran.com/2016/08/01/users-and-groups-in-nintex-forms-for-office-365-part-2-get-groups-of-current-logged-in-user

Moving on from there, now let us see how to dynamically set the division of logged in user in the ‘Division’ dropdown list in your form.

Lookup list for list of division, its directors/supervisors.

2016-08-19_09h10_02.png

  • on form load call this function getDivisionFn()
  • write a caml query (sans humps ofcourse) to get the list item that has the logged in user, to get the name of the division.
  • clientContext load and executeQueryAsync to set the default value of the form’s dropdown control.

Store client id javascript variables of the controls, for getting and setting their values.
2016-08-19_09h21_41.png

Make sure to have the internal name of the column you are planning to use in the query/form. The beautiful and totally relevant name _x0062_ad3  is the ‘Director’ column from the list above. Cool eh! 😉

var pollSP;
var lItem;
var loggedInUser;
NWF.FormFiller.Events.RegisterAfterReady(function(){
pollSP=setInterval(checkSPLoad,500);
});
function checkSPLoad(){
if(clientContext){
window.clearInterval(pollSP);
onSPLoad();
}
}
function onSPLoad() {
getCurrentUserFn();
getSPGroupFn();
getDivisionFn();
}

function getDivisionFn() {
try {
var list = clientContext.get_web().get_lists().getByTitle(“DivisionDirectors”);
var query = new SP.CamlQuery();
query.set_viewXml(“<View><Query><Where><Eq><FieldRef Name=’_x0062_ad3’/><Value Type=’Text’>SharePoint Test</Value></Eq></Where></Query></View>”);
alert(query);
lItem = list.getItems(query);
clientContext.load(lItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, function () { OnDivSuccess(lItem) }),
Function.createDelegate(this, function () { OnDivFail() })
);
}
catch (err) {
alert(err);
}
}

function OnDivSuccess(lItem) {
division = NWF$(“#” + divisionVar );
try{
var itemEnum = lItem.getEnumerator();
while (itemEnum.moveNext()) {
var item = itemEnum.get_current();
alert(item.get_item(‘ID’));
var userDivision = item.get_item(‘ID’);
division.on(‘change’, function (e) {
if (e.originalEvent == undefined) {
if (this.value == “”) {
this.value = userDivision;
}
}
});
}
}
catch (err) { alert(err);}
}
function OnDivFail() {
alert(“no luck sistah! try and try again!”);
}

And that’s that.

When your form loads now, the value of the division is prepopulated with the value based on ID of the lookup list item, which the logged in user is the ‘Director’.

Contd….

For those one and half folks that read my blog and felt like…’Gotcha! I found an issue!!!’ Yes, I did too…hence this update and the HUGE script. Mostly because am lazy and probably will come back to refer this like a zillion times in my foreseeable future.

Whats the change you ask? If you haven’t spotted, I shall not say. But I’ll say, because I probably won’t remember this tomorrow.

  • Remember the title of the post, logged in user and her department? So, the previous method(s) had a static ‘caml’. I kinda sorta cheated myself into thinking ‘Swetha Sankaran’ is everyone at any given point of time. She is indeed omnipresent, but the bits/jQuery doesn’t  concur with her status of being everyone.
  • Dropdown list selected value does NOT preload correctly on all browsers. Why? Thats mystery number 1213234. However, we care about the data and NOT about control(at this time). Since we have the info handy thanks to clientContent, we’ll just preload the division data to textbox.

And the final script bwlow …now, Go back to work and good luck.

var pollSP;var lItem; var curUser;
NWF.FormFiller.Events.RegisterAfterReady(function () {
pollSP = setInterval(checkSPLoad, 200);
});
function checkSPLoad() {
if (clientContext) {
window.clearInterval(pollSP);
onSPLoad();
}
}
function onSPLoad() {
getCurrentUserFn();
getSPGroupFn();
}
function getSPGroupFn() {
try {
var spGroups = clientContext.get_web().get_currentUser().get_groups();
clientContext.load(spGroups);
clientContext.executeQueryAsync(
Function.createDelegate(this, function () { OnSuccess(spGroups); }),
Function.createDelegate(this, this.failed)
);
}
catch (err) {
alert(err);
}
}
function OnSuccess(spGroups) {
//alert(“Groups loaded”);
try {
var isMember = false;
var groupsEnumerator = spGroups.getEnumerator();
while (groupsEnumerator.moveNext()) {
var userGroupNames;
var currentGroup = groupsEnumerator.get_current();
userGroupNames += currentGroup.get_title() + “\n”;
if (currentGroup.get_title() == “SPDirectorsGroup”) {
//alert(“User is a member of SharePoint Group:” + currentGroup.get_title());
isMember = true;
}
}
//alert(“User belongs to these SharePoint Groups:\n” + userGroupNames);
} catch (err) { alert(err); }
}
function OnFail() {
alert(“Failed to load groups”)
}
function getCurrentUserFn() {
try {
var currentUser = clientContext.get_web().get_currentUser();
clientContext.load(currentUser);
clientContext.executeQueryAsync(function () {
var name = NWF$(“#” + nameVar);
curUser = clientContext.get_web().get_currentUser().get_title();
name.attr(“value”, curUser);
var div = NWF$(“#” + divisionVar);
divisionChange(div);
getDivisionFn();
});
}
catch (err) { alert(err); }
}
function getDivisionFn() {
try {
var list = clientContext.get_web().get_lists().getByTitle(“DivisionDirectors”);
var query = new SP.CamlQuery();
alert(curUser);
query.set_viewXml(“<View><Query><Where><Or><Eq><FieldRef Name=’_x0062_ad3’/><Value Type=’Text’>” + curUser + “</Value></Eq><Eq><FieldRef Name=’gzmj’/><Value Type=’Text’>” + curUser + “</Value></Eq></Or></Where></Query></View>”);
alert(query);
lItem = list.getItems(query);
clientContext.load(lItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, function () { OnDivSuccess(lItem) }),
Function.createDelegate(this, function () { OnDivFail() })
);
}
catch (err) {
alert(err);
}
}
function OnDivSuccess(lItem) {
//division = NWF$(“#” + divisionVar );
try{
var itemEnum = lItem.getEnumerator();
var divisionText = NWF$(“#” + txtDivisionVar);
while (itemEnum.moveNext()) {
var item = itemEnum.get_current();
alert(item.get_item(‘ID’));
divisionText.attr(“value”, item.get_item(‘Title’));
//var userDivision = item.get_item(‘ID’);
//div.on(‘change’, function (e) {
//    if (e.originalEvent == undefined) {
//        if (this.value == “”) {
//            this.value = userDivision;
//            divisionText = this.value;
//    }
//    }
//});
//
alert(divisionText);
}
}
catch (err) { alert(err);}
}
function OnDivFail(sender, args) {
alert(“Returned error: ” + args.message());
}
function divisionChange(div) {
try {
div.change(function () {
var oDivision = clientContext.get_web().get_lists().getByTitle(“DivisionDirectors”);
var listItem = oDivision.getItemById(this.value);
clientContext.load(listItem, “_x0062_ad3”, “gzjx”, “gzmj”);
clientContext.executeQueryAsync(function () {
var txtDirector = NWF$(“#” + txtDirectorVar);
var lDir = listItem.get_item(‘_x0062_ad3’);
txtDirector.attr(“value”, lDir);
var sup = NWF$(“#” + supervisorVar);
var lSup = listItem.get_item(‘gzmj’);
sup.attr(“value”, lSup);
},
function () {
alert(“fail”);
});
});
}
catch (err) {
alert(“error”);
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s