10/19/2009

SharePoint: Setting a People Picker field using the API

 

Funny how the obvious does not always work… For example, you want to create a new task in code and assign it to some one. You might try to set the Assigned To field to the user's name:

      SPItem itm6 = lst.Items.Add();
      itm6["Title"] = "test2";
      itm6["Assigned To"] = @"MAXSP2007\samc";               //fails!
      itm6.Update();

“Assigned To” is a “People Picker” field and only accepts data in certain ways, any but the obvious. If you read this field you will see a value like:  “8#;MAXSP2007\samc”, and using that value will work…

What the "People Picker” field wants is the ID of the user, the “8”.  The rest appears to be ignored. So, here’s several ways to get there:


itm1["Assigned To"] = web.AllUsers[@"MAXSP2007\samc"]; //works
- itm2["Assigned To"] = 8; //works
- itm3["Assigned To"] = web.AllUsers[@"MAXSP2007\samc"]; //works - SPMember member = web.AllUsers[@"MAXSP2007\samc"]; itm4["Assigned To"] = member.ID; //works - SPMember member = web.AllUsers[@"MAXSP2007\samc"]; itm5["Assigned To"] = member; //works

 

.

 

 

 

No comments:

Note to spammers!

Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.