Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11571

how to identify is it a user or a user group from string sharepoint

$
0
0

Guys, i have a discussion list where i have field type People or Group with Multiple Selection On. its my Participants field.

admin or any other user can enter user or user group in that field, and from C# i am getting that Participants field as string it may contain values something like

domain/UserName, domain/GroupName, domain/UserName and so on..........

My Question is How to identify from this type of string either it is a user or it is a group ?

below is my current code to get user from string param Participant, if this param contains Group Name then error occurs in the below line where i mention comment.

 private bool IsUserExists(SPListItem item, string Participants, SPWeb CurrentWeb)
        {
            bool IsUserExists = false;

            if (!string.IsNullOrEmpty(Participants))
            {
                string AllUsers = string.Empty;
                SPFieldUserValueCollection UserColl = new SPFieldUserValueCollection(item.Web, Participants);

                foreach (SPFieldUserValue Value in UserColl)
                {   
                    AllUsers = AllUsers + Value.User.Name+ ","; //ERROR occurs if it is group
                }

                if (AllUsers.Length > 0)
                {
                    AllUsers = AllUsers.Substring(0, AllUsers.Length - 1);

                    string[] array = AllUsers.Split(',');
                    if (array.Length > 0)
                    {
                        for (int i = 0; i < array.Length; i++)
                        {
                            SPUser user = CurrentWeb.EnsureUser(array[i]);
                            if (user != null)
                            {
                                if (user.LoginName == SPContext.Current.Web.CurrentUser.LoginName) //user.LoginName --> contains user name with domain
                                    IsUserExists = true; //SPContext.Current.Web.CurrentUser.LoginName --> contains user name with domain
                            }
                        }
                    }
                }
            return IsUserExists;
        }


Viewing all articles
Browse latest Browse all 11571

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>