locked
Retrieve SPFieldUser from username RRS feed

  • Question

  • Hi,

    I'm able to retrieve users and username (domain\person) from all the AD Users in a Person/Group Picker control in InfoPath.

    Then I need to add the different users in a field in a SharePointList.

    foreach (string str in users)
    {
    userValueCollection.Add(new SPFieldUserValue(workflowProperties.Web,
           workflowProperties.Web.AllUsers[str].ID,
           workflowProperties.Web.AllUsers[str].LoginName));
    }

    If the users have permissions to visit the SharePoint site (or are members of this site) it will work. If not, it will throw an exception User cannot be found.

    I have also tried with Web.SiteUsers instead of Web.AllUsers.

    Thursday, March 7, 2013 8:46 AM

Answers

  • but it does not assign any permissions, just adds the user to the UserInfo table in SQL.

    That's the only way to accomplish what you are trying. If you do this through the SharePoint UI it does the exact same thing in the backend.
    • Marked as answer by okdreamy Thursday, March 7, 2013 10:41 AM
    Thursday, March 7, 2013 10:40 AM

All replies

  • Hi,

    use, "SPWeb.EnsureUser" and input the domain name to retrieve the user object.

    This will then allow you to construst the SPFieldUser object to input in the people field.

    Regards
    Pieter
    Thursday, March 7, 2013 9:01 AM
  • Hi,

    foreach(string str in users) 

    {

    SPUser user = workflowProperties.Web.EnsureUser(str); userValueCollection.Add(new SPFieldUserValue(workflowProperties.Web, workflowProperties.Web.SiteUsers[str].ID, workflowProperties.Web.SiteUsers[str].LoginName));

    }

    I declare a SPUser and assign it, and it will add the user to the website if he does not exist yet.

    It works, but is it the correct way to make it? it seems strange to me...

    • Edited by okdreamy Thursday, March 7, 2013 10:25 AM
    Thursday, March 7, 2013 10:23 AM
  • nonono, don't add the user to the site.

    You just Ensure the user to retrieve his details from AD, then you can add him/her to the sharepoint list like follows.

    SPUser user = workflowProperties.Web.EnsureUser(str);
    userValueCollection.Add(new SPFieldUserValue(workflowProperties.Web, user.ID, user.LoginName);
    Thursday, March 7, 2013 10:30 AM
  • are you sure Ensure method does not add user to the site?

    because -checks whether the specified login name belongs to a valid user of the web site, and if the login name does not already exist, adds it to the web site.

    Thursday, March 7, 2013 10:37 AM
  • but it does not assign any permissions, just adds the user to the UserInfo table in SQL.

    That's the only way to accomplish what you are trying. If you do this through the SharePoint UI it does the exact same thing in the backend.
    • Marked as answer by okdreamy Thursday, March 7, 2013 10:41 AM
    Thursday, March 7, 2013 10:40 AM
  • Thank you Pieter, have a nice day !
    Thursday, March 7, 2013 10:42 AM