I am trying to use client side JScript to set a field, however I am not able to do so. Can someone look at the coding?
<script type="text/javascript">
function setRole() {
if (ctx.CurrentItem["loggedonuser"].value = "i:0#.w|mickey.mouse") {
// Setting the IMORole to Yes
ctx.CurrentItem["IMORole"].value = "Yes";
}
}
Thanks!
Your if
condition is wrong. It should be:
if (ctx.CurrentItem["loggedonuser"].value === "i:0#.w|mickey.mouse") {
// Setting the IMORole to Yes
ctx.CurrentItem["IMORole"].value = "Yes";
}
Notice the comparison operator ===
.
=
is for assignment and not comparison.
Update:
For finding out whether the value exists in an array or not, you could do one of the following:
var exists = $.inArray(value, array);
Example:
var userValue = ctx.CurrentItem["loggedonuser"].value;
var possibleValues = ["str1", "str2"];
var exists = $.inArray(userValue, possibleValues);
if (exists) {
ctx.CurrentItem["loggedonuser"].value = "Yes";
}
var exists = arrValues.indexOf('Sam') > -1;
Example:
var userValue = ctx.CurrentItem["loggedonuser"].value;
var possibleValues = ["str1", "str2"];
var exists = possibleValues.indexOf(userValue) > -1;
if (exists) {
ctx.CurrentItem["loggedonuser"].value = "Yes";
}
["Sam", "Great", "Sample", "High"].includes("Sam");
Example:
var userValue = ctx.CurrentItem["loggedonuser"].value;
var possibleValues = ["str1", "str2"];
var exists = possibleValues.includes(userValue);
if (exists) {
ctx.CurrentItem["loggedonuser"].value = "Yes";
}
The example below for your reference:
1.Create a custom list, add a Person or Group field "loggedonuser", add a Yes/No field "IMORole"(default value is No).
2.Add the code below into a script editor web part in the editform.aspx.
<script src="//code.jquery.com/jquery-3.2.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
var loggedonuser=$("input[name^='loggedonuser'][name$='ClientPeoplePicker_HiddenInput']").val();
if(loggedonuser.indexOf("i:0#.w|mickey.mouse")!=-1){
$("input[title='IMORole']").prop('checked', true);
}
});
</script>
To get current login name, we can also use REST API to achieve it.
<script src="//code.jquery.com/jquery-3.2.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
var userid = _spPageContextInfo.userId;
var loginName=getUserLoginName(userid);
if(loginName=="i:0#.w|mickey.mouse"){
$("input[title='IMORole']").prop('checked', true);
}
});
function getUserLoginName(userId){
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userId + ")";
// execute AJAX request
$.ajax({
url: requestUri,
type: "GET",
async: false,
headers: { "ACCEPT": "application/json;odata=verbose" },
success: function (data) {
loginName=data.d.LoginName;
},
error: function () {
}
});
return loginName;
}
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.