Click here to Skip to main content
15,899,026 members
Articles / Productivity Apps and Services / Sharepoint / SharePoint 2013
Tip/Trick

Unique Entries Based on Multiple Columns in Sharepoint List

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
11 Sep 2017CPOL1 min read 19.1K   3
Populate list data from Sharepoint service and then check user input when save button submitted

Introduction

By default, Sharepoint list only allows unique entries from one column. The traditional way is we query data from database to check the user input, throw a message when condition matches. In Sharepoint, we can populate list data from Sharepoint web service and compare it with user input, when matching data is found, throw an error “Data already exists”.

Using the Code

I have made a list application with name Employees, I have added 3 columns - Name, Email, and Phone with data type text.

Image 1

Maybe you see my application looks different style because I have made custom CSS for my Sharepoint site. Below is new item form.

Image 2

Here it is my step to create unique validation, I will make validation from column Title, Name, and Email. So when user input already exists based on that 3 columns error message appear and data won’t be saved.

  1. Open Sharepoint designer and open your site. In this case, my site is http://bhpsp03srv/.
  2. Open List and Libraries > Employees > NewForm.aspx.

    Image 3

  3. Add jquery reference and our validation script under </Sharepoint:UIVersionedContent> and above </asp:Content>.

    Image 4

  4. This is our script:
    HTML
    <script type="text/javascript" 
    src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js" />
    
    <script type="text/javascript">
        // adding javascript to button onclick event to call checkExist() function
        var oc = $('td.ms-toolbar:nth-child(2) input').attr('onclick');
        $('td.ms-toolbar:nth-child(2) input').attr
           ('onclick','if (checkExist())
           {  alert("Employee Already Exist"); }else{'+ oc +'}');
    
        //variable to hold Ajax result
        var dataResults;
        
        //Ajax to populate data from Employees list
        $.ajax({
            url: "http://bhpsp03srv/_api/web/lists/getbytitle('Employees')/items",
            type: "GET",
            async: false,
            headers: { "Accept": "application/json;odata=verbose" },
            success: function(data) {
                dataResults = data.d;
            }    
        });
    
        //function to compare user input and existing data
        function checkExist(){
            var title = $('table.ms-formtable > tbody > tr:nth-child(1) > 
                            td.ms-formbody > span > input').val();    
            var name = $('table.ms-formtable > tbody > tr:nth-child(2) > 
                            td.ms-formbody > span > input').val();    
            var email = $('table.ms-formtable > tbody > tr:nth-child(3) > 
                            td.ms-formbody > span > input').val();    
            var c = null;
            
            $.each(dataResults, function(i, item){
                $.each(item, function(i, dt){
                    console.log(dt.Title + '  ' + dt.Name + '  ' + dt.Email);
                    if (dt.Title == title && dt.Name == name && dt.Email == email){
                        c = true;
                        return false;
                    }
                });
            });           
            return c;  
        }
        
    </script>
  5. Try to add new item that already exists, gotcha you’ve done:

    Image 5

  6. For edit item, you can do the same thing.

Points of Interest

Why does Sharepoint not make this feature, or maybe I can’t find this feature. Hope it helps you guys!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Indonesia Indonesia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionCould not get this to work Pin
Member 1405591013-May-20 10:01
Member 1405591013-May-20 10:01 
QuestionClever Pin
Member 1457120028-Aug-19 4:51
Member 1457120028-Aug-19 4:51 
GeneralMy vote of 4 Pin
Member 1236439013-Sep-17 21:12
Member 1236439013-Sep-17 21:12 
interesting simple article. how about one on using custom css in SharePoint - as you mention

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.