5

I have a SharePoint 2013 list and want to use 2 columns as sort of a natural key on the list, so when a user enters a new entry, if these 2 columns combined match an entry already in the list, it won't add it.

Can I do this with a validation formula, or will I need to wire this check into an event receiver? If I can do it with validation, can anyone offer help on the validation forumla? I'm not overly familiar with the syntax.

[UPDATE]: I'm going to mark sasisprite's answer as correct, as his directly answers my question, but I wanted to leave an update on how I did this, in case anyone else comes across it looking to the same thing.

The solution is actually pretty simple, as I already had an event receiver wired into the list, I just had to add a method to check for a duplicate and return an error if that method returned true. So it looks like this:

public bool IsDuplicateEntry(string date, string sitename)
    {
        string spUrl = "http://intranet";
        string spWeb = "myWeb";

        using (SPSite site = new SPSite(spUrl))
        using (SPWeb web = site.OpenWeb(spWeb))
        {
            SPList list = web.Lists["My List"];
            foreach(SPListItem item in list.Items)
            {
                string _name = item["Name"].ToString();
                string _date = DateTime.Parse(item["Date"].ToString()).ToString("d");
                if(_sitename == sitename && _date == date)
                {
                    return true;
                }
            }
            return false;
        }

So then it's just a matter of calling the method in my event receiver:

public override void ItemAdding(SPItemEventProperties properties)
        {
            base.ItemAdding(properties);

            string theName = properties.AfterProperties["Name"].ToString());
            //I want to extract just the date here
            string theDate = DateTime.Parse(properties.AfterProperties["Date"].ToString()).ToString("d")

            if(IsDuplicateEntry(theName, theDate)
            {
                  properties.Status = SPEventReceiverStatus.CancelWithError;
                  properties.ErrorMessage = "You have already made an entry for today");
            }
            else
            {
                //Do your stuff
            }
        }

Hope this helps somebody.

3

Even I have the same requirement. But Unfortunately we don't have any option like enforcing unique by combining 2 columns by default.

Even I have researched a lot. But we can achieve this by using only info-path form.

Hope the below link may help you

https://salnikan.wordpress.com/2011/07/26/prevention-of-duplicate-entries-based-on-two-or-more-columns-in-a-sharepoint-list/

  • Thanks for the answer. I was afraid of that. I'm already running an event receiver on the list, so I can add the check there. Would have saved me some time though to do it through a quick validation script. – gev125 Jan 26 '16 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.