Home > MOSS 2007 > Notes Field Not Getting Populated by SPUserCollection.Add Method

Notes Field Not Getting Populated by SPUserCollection.Add Method

February 23rd, 2009 Leave a comment Go to comments

So here is the deal:

You add a user to the users collection of a site collection by calling into the SPUserCollection.Add method passing login name, email, name and notes as parameters, but notes field doesn’t get populated as you expected:

[CSharp]
using (SPSite site = new SPSite(url))
{
using (SPWeb sweb = site.OpenWeb())
{
//Ommitted Code for brevity
sweb.SiteUsers.Add(“foo\\JohnDoe”, “JohnDoe@foo.com”, “John Doe”, “Is it John Doe or John Dough?”);
//Ommitted Code for brevity
}
}
[/CSharp]

Although, using the code snippet above, the user is added correctly to the user collection of the site but notes field is always left blank. Note that in the above code, I am just adding the user to the users collection and I don’t bother adding him to any specific group at this point (for example by calling into SPWeb.Groups[groupName].AddUser). Let’s suppose that my requirement only dictates to add the user to this collection at this point and that’s all for now.

kept at each site collection level, there is this hidden list called ‘User Information List’ that stores some metadata about every and each user has ever browsed, authenticated or added to a group in that site collection . This design decision along with many other things kept at the site collection level gives countless developers a good amount of grief and frustration as you may be aware of . When a user is granted access to a site, a new list item will be created in that list. The User Information List can be accessed via the browser by navigating to /_catalogs/users/simple.aspx from your site or via Object model , only if you or your code runs under sufficient privileges. So, we can use that list for troubleshooting above issue by updating the ‘Notes’ field after the user is added. The following example shows how to interact with this list for setting the required user properties.

[CSharp]
using (SPSite site = new SPSite(url))
{
using (SPWeb sweb = site.OpenWeb())
{
//Ommitted Code for brevity
sweb.SiteUsers.Add(“foo\\JohnDoe”, “JohnDoe@foo.com”, “John Doe”,string.Empty);
user = sweb.AllUsers[“foo\\JohnDoe”];
SPList list = sweb.Lists[“User Information List”];
SPListItem userItem = list.Items.GetItemById(user.ID);
userItem[“Notes”] = “Is it John Doe or John Dough?”;
userItem.Update();
//Ommitted Code for brevity
}
}
[/CSharp]

This way you pass an empty string to the calling method (SiteUsers.Add) and update the Notes field in a separate call.

Categories: MOSS 2007 Tags:
  1. No comments yet.