In SharePoint 2010 OnPremise, our Customer came up with one new
small requirement to show the default value for people picker control in Custom
List OOB New Form. As it was for a simple OOB new form for a Custom List, we
analyzed the options accordingly in 2010.
- If you want to do it in InfoPath Form then there is very nice article presented here by Laura Rogers
- If you want to do it in SharePoint 2013 then please refer this nice article which describes how to do it with JSLink.
In our case it was SharePoint 2010 and a Custom List OOB New
Form. Actually the problem is we cannot set default values for People Picker
control from design time so we had two options basically, one with the help of Custom
JQuery Injection or second with the help of third party SharePoint
Add-On
We observed following Pros and Cons for the SharePoint Add-On:
Pros:
- It can be very useful for setting default values during design time for lookup or people picker fields.
Cons:
- It is not free. Free trial period is only for 30 days
- Setup will install a new wsp to the farm so might be a problem with maintenance or updates in future
So we decided to go ahead with the first
option instead i,e. Custom JQuery Injection approach, that was more simple and
quick to implement.
For this we edited the default new form for
the custom list, then added one Content editor webpart in the form so as to
attach a custom .js file with it (Remember there was no JSLink in SP 2010). Now
is the time to create actual JQuery file which will inject default value into
the People Picker control.
In our case Customer wanted to show a particular person’s name
in the people picker and not the logged in person as default. If you have the
requirement to show the logged in user as default then you may refer this link with some little variation and use of “jquery.SPServices”
libraries.
We created following JQuery Injection script
for our purpose. Here is the custom JQuery script named “setdefaultvalue.js” (I
hope you have “jquery.min.js” already available in “_layouts/js” folder in 14
hive, if not you can download it from here and
place it in 14 hive path)
setdefaultvalue.js
<script
src="/_layouts/js/jquery.min.js"></script>
<script
type="text/javascript">
$(document).ready(function
() {
user = "Domain\\Username";
// Set “Domain\\Username” as default user
// Set “user” as default
value in the required people picker control
var counter = 0;
$('div[title="People
Picker"]').each(function() {
if (counter == 1) {
$(this).html(user);
}
counter++;
});
// Call the click event
of “Check names” button explicitly
counter = 0;
$('a[title="Check
Names"]').each(function() {
if (counter == 1) {
$(this).click();
}
counter++;
});
});
</script>
Note: In this script we used “counter”
variable to check at which position our People Picker control appears in the
form (required only in case of multiple People Picker control on form).
And then referred it in the Content Editor
webpart property “Content Link” as below
Just make sure of one thing, keep the Zone
index of the content editor webpart later than the main form webpart. Suppose
if main form webpart zone index is 1 then keep the zone index of content editor
webpart as 2. This will make sure that all the controls in the form are loaded
first and then JQuery script will fire.
After all these steps, default value started
showing up correctly in the desired People Picker control on the list new form.
Happy Learning! J
No comments:
Post a Comment