This article is about how to set default value to person or group field when new list form opens using JavsScript and JQuery.
It includes the following
1. Get the members of a SharePoint group using SPServices JQuery library
2. Set the value to Person or Group field using JavaScript
<script type="text/javascript" language="javascript" src="/Style%20Library/JS/jquery-1.8.3.min.js"></script>
<script type="text/javascript" language="javascript" src="/Style%20Library/JS/jquery.SPServices-0.7.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var strHTMLGroupUsers = "";
$().SPServices({
operation: "GetUserCollectionFromGroup",
groupName: "Approvers",
async: false,
completefunc: function(xData, Status) {
$(xData.responseXML).find("User").each(function() {
strHTMLGroupUsers += $(this).attr("Name")+";";
});
}
});
//get the people pickers input div
var pp = getPickerInputElement("Approvers");
//set it to the current user if we've found it
if(pp != null)
pp.innerHTML = strHTMLGroupUsers;
$("img[title='Check Names']").parent().click();
});
function getPickerInputElement(fieldsInternalName)
{
var result = "";
var divs = document.getElementsByTagName("DIV");
for(var i=0; i < divs.length ; i++)
{
if(divs[i].id=="WebPartWPQ3")
{
var tds = divs[i].getElementsByTagName("TD");
for(var j=0; j < tds.length; j++)
{
var cellHTML = tds[j].innerHTML;
if(cellHTML.indexOf('FieldInternalName="' + fieldsInternalName + '"') >= 0)
{
var innerDivs = tds[j].getElementsByTagName("DIV");
for(var k=0; k < innerDivs.length; k++)
{
if(innerDivs[k].id.indexOf("UserField_upLevelDiv") > 0)
{
result = innerDivs[k];
break;
}}}
}
}
}
return result;
}
</script>
It includes the following
1. Get the members of a SharePoint group using SPServices JQuery library
2. Set the value to Person or Group field using JavaScript
<script type="text/javascript" language="javascript" src="/Style%20Library/JS/jquery-1.8.3.min.js"></script>
<script type="text/javascript" language="javascript" src="/Style%20Library/JS/jquery.SPServices-0.7.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var strHTMLGroupUsers = "";
$().SPServices({
operation: "GetUserCollectionFromGroup",
groupName: "Approvers",
async: false,
completefunc: function(xData, Status) {
$(xData.responseXML).find("User").each(function() {
strHTMLGroupUsers += $(this).attr("Name")+";";
});
}
});
//get the people pickers input div
var pp = getPickerInputElement("Approvers");
//set it to the current user if we've found it
if(pp != null)
pp.innerHTML = strHTMLGroupUsers;
$("img[title='Check Names']").parent().click();
});
function getPickerInputElement(fieldsInternalName)
{
var result = "";
var divs = document.getElementsByTagName("DIV");
for(var i=0; i < divs.length ; i++)
{
if(divs[i].id=="WebPartWPQ3")
{
var tds = divs[i].getElementsByTagName("TD");
for(var j=0; j < tds.length; j++)
{
var cellHTML = tds[j].innerHTML;
if(cellHTML.indexOf('FieldInternalName="' + fieldsInternalName + '"') >= 0)
{
var innerDivs = tds[j].getElementsByTagName("DIV");
for(var k=0; k < innerDivs.length; k++)
{
if(innerDivs[k].id.indexOf("UserField_upLevelDiv") > 0)
{
result = innerDivs[k];
break;
}}}
}
}
}
return result;
}
</script>
No comments:
Post a Comment