Restrict user to only add one item in a list
- vegard.grutle
- Posts: 59
- Joined: Fri Mar 17, 2017
Hi!
Is there a quick way to restrict user to only add one item in a list?
Thanks!
Is there a quick way to restrict user to only add one item in a list?
Thanks!
- Nikita Kurguzov
- Posts: 889
- Joined: Mon Jul 03, 2017
Do you mean for the Related Items control? Or just the List in general?
Cheers
- Nikita Kurguzov
- Posts: 889
- Joined: Mon Jul 03, 2017
Well, there are two ways to do it with Form, but you'll need to turn off quick edit for the List, in List settings, because otherwise one user would be able to add many entries without even opening the form.
Now, about what you can do with the form. One way is easy, but doesn't give much feedback to user as to what went wrong, another way is more complicated, but you can give any error message you want to on save. If it's not very important, you can proceed with #1 as it is easier to implement, but #2 gives better control over the process and feedback.
1) Create New People Picker field and select Enforce unique values -> Yes. Then add this field to your Form. You need to hide it from users, so they can't manually change it. Just add this line to the Style of the field:
Finally, you'll need to add this code to the JavaScript editor, so it auto fills this hidden field with the current user:
If you have another onsubmit block already, just add fd.field line before return true. This will assign all items an author on creation and will not allow same user to add more than one entry to the List.
2) Second way doesn't require you to create any new fields. As you may know, there is already an Author (Created By) field. It's possible to use this field for checking if there are already items in the List by this user.
But that's quite a bit more complicated. You need to make an async request when you try to save the form, this request will check if the list already has an item by the same author, then it will either save the item or tell user that he already has created an item in this list.
This approach is better because it allows you to give feedback, plus it will work with already created items, but it's not super easy. If you want to go with this one, let us know, we can either help you do it by answering any questions you might have or even do it for you, but that will require you to purchase 60 minutes of extra support from our store and contact us via email support@spform.com, if you want us to write the code for you.
Now, about what you can do with the form. One way is easy, but doesn't give much feedback to user as to what went wrong, another way is more complicated, but you can give any error message you want to on save. If it's not very important, you can proceed with #1 as it is easier to implement, but #2 gives better control over the process and feedback.
1) Create New People Picker field and select Enforce unique values -> Yes. Then add this field to your Form. You need to hide it from users, so they can't manually change it. Just add this line to the Style of the field:
Code: Select all
display: none;
Code: Select all
fd.onsubmit(function() {
fd.field('FIELDNAME').value(_spPageContextInfo.userLoginName);
return true;
});
2) Second way doesn't require you to create any new fields. As you may know, there is already an Author (Created By) field. It's possible to use this field for checking if there are already items in the List by this user.
But that's quite a bit more complicated. You need to make an async request when you try to save the form, this request will check if the list already has an item by the same author, then it will either save the item or tell user that he already has created an item in this list.
This approach is better because it allows you to give feedback, plus it will work with already created items, but it's not super easy. If you want to go with this one, let us know, we can either help you do it by answering any questions you might have or even do it for you, but that will require you to purchase 60 minutes of extra support from our store and contact us via email support@spform.com, if you want us to write the code for you.
Cheers
-
- Information
-
Who is online
Users browsing this forum: No registered users and 8 guests