Quantcast
Channel: SharePoint 2010 - General Discussions and Questions forum
Viewing all articles
Browse latest Browse all 17574

Adding SharePoint User to SharePoint Group programmatically returns null value

$
0
0

Hi,

I have a problem when adding user to SharePoint group programmatically using SharePoint people picker control. If I try to add the user manually inside the group, the user will be accepted and if I add the user to the website first, then I can add the user programmatically. The problem exists when the user is not exist in the website. Below is my code for further reference.

PickerEntity pe = (PickerEntity)upTenderOwner.ResolvedEntities[i];
                            userCollTO.Add(new SPFieldUserValue(SPContext.Current.Web, Convert.ToInt32(pe.EntityData["SPUserID"]), pe.Key));

                            foreach (SPFieldUserValue spfuv in userCollTO)
                            {
                                SPUser user = spfuv.User;

                                web.AllowUnsafeUpdates = true;

                                SPUser spUsr = web.EnsureUser(user.LoginName);
                                if (spUsr != null)
                                {
                                    SPGroup spGrp = web.Groups[grpTO.Name];
                                    if (spGrp != null)
                                    { spGrp.AddUser(spUsr); }
                                }

                                web.AllowUnsafeUpdates = false;
Any help is much appreciated. Thank you.


wawa87


Viewing all articles
Browse latest Browse all 17574

Trending Articles



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