Two "person or group" columns cannot have the same user SharePoint 2013

Cherry P 21 Reputation points
2020-08-24T21:18:36.493+00:00

Hi All,

I have a SharePoint custom list where two "person or group" columns. I need to validate those two columns to make sure they doesn't have the same user. One column is set to allow multiple users and other column is set to allow single user only. I know that we cannot do it with the calculated column.

Can anybody share how do I achieve this? I appreciate all your help!

Thanks,

SharePoint Server
SharePoint Server
A family of Microsoft on-premises document management and storage systems.
2,339 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,608 questions
0 comments No comments
{count} votes

Accepted answer
  1. Amos Wu-MSFT 4,051 Reputation points
    2020-08-25T03:08:11.147+00:00

    You could add a event receiver(item adding) at your custom list.
    My test code for your reference:

     public override void ItemAdding(SPItemEventProperties properties)
                {
                    base.ItemAdding(properties);
    
                object peopleColumn = properties.AfterProperties["SingleUserColumn"];
                SPWeb web = properties.Web;
                SPFieldUserValueCollection objUserFieldValueCol = new SPFieldUserValueCollection(web, properties.AfterProperties["MutipleUserColumn"].ToString());
                for (int i = 0; i < objUserFieldValueCol.Count; i++)
                {
                    SPFieldUserValue singlevalue = objUserFieldValueCol[i];
    
                        if (singlevalue.ToString().Equals(peopleColumn.ToString()))
                        {
                            properties.Cancel = true;
                            properties.ErrorMessage = "Contain two same user";
                            properties.Status = SPEventReceiverStatus.CancelWithError;
                        }
    
                }
    
            }
    

    Demo on how to create event receiver for your reference:
    https://www.c-sharpcorner.com/article/steps-to-create-item-event-receivers-in-sharepoint-2013-using-c-sharp-server-object-m/


    If the response is helpful, please click "Accept Answer" and upvote it.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Cherry P 21 Reputation points
    2020-08-25T12:36:30.157+00:00

    Thank you for the response @Amos Wu-MSFT Appreciate it.

    I am trying with this code on my dev server and got below error. Can you please help?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.