0

I need to get users within SPFieldUserValueCollection and set them inside SPUserCollection, but I have the following build error:

The type 'Microsoft.SharePoint.SPUserCollection' has no constructors defined

 SPFieldUserValueCollection employees = item["employees"] as SPFieldUserValueCollection;
            SPUserCollection users = new SPUserCollection();

            if (values != null)
            {
                foreach (SPFieldUserValue userValue in employees)
                {
                    users.Add(userValue.User.LoginName, userValue.User.Email, userValue.User.Name, userValue.User.Notes);
                }

                return users;
            }

How I can initialize SPUserCollection?

0

Use instead:

List<SPUser> users = new List<SPUser>(); 
//SPUser user = web.EnsureUser(userValue.User.LoginName);
users.add(user);

So... your code:

SPWeb web = SPContext.Current.Web;
SPFieldUserValueCollection employees = item["employees"] as SPFieldUserValueCollection;
List<SPUser> users = new List<SPUser>();

if (values != null)
{
    foreach (SPFieldUserValue userValue in employees)
    {
        SPUser user = web.EnsureUser(userValue.User.LoginName);
        users.Add(user);
    }

    return users;
}
  • Thank you @domsen, my question for understand how work with SP code, not for solve specific issue. and the question is "How I can initialize SPUserCollection?" – jobin Apr 1 '15 at 15:01
  • I think you cant realy initialzie a "new" UserCollection, but im not realy sure... you can initialize an existing one by doing something like spusercollection users = web.AllUsers or something else... I came across this issue, too - a lot of times.... so I decided to do this with this way :) – domsen123 Apr 1 '15 at 15:37
  • thank you, I'll reflect the code and check how they do it – jobin Apr 1 '15 at 16:02

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.