I want to add an item to a SharePoint task list, with data in the Title, AssignedTo, and Status columns. The code below adds the item with data in the Title column, but no data in the other columns. What do I need to change for this to work?

SPWebApplication webApp = SPWebApplication.Lookup(new Uri("http://mysharepointserver"));
string strSite = "http://mysharepointserver/sites/testsite/default.aspx";
using (SPSite siteCollection = new SPSite(strSite))
    using (SPWeb web = siteCollection.OpenWeb("testsite/mysubsite"))
        SPList taskList = web.Lists["Tasks"];
        SPListItem newTask = taskList.Items.Add();
        newTask["Title"] = DateTime.Now.ToString();
        newTask["AssignedTo"] = @"mydomain\jdoe";
        newTask["Status"] = "In Progress";
  • maybe because you didn't write any metadata in the other columns? – Salvatore Di Fazio Apr 18 '13 at 14:29
  • I think that I added metadata to the Title, AssignedTo, and Status columns. Are you saying that the above code does not do that? If so, can you provide a code example of how to do it? – LFurness Apr 18 '13 at 14:33

you should use SPUser field type in this situation.

      newTask["AssignedTo"] = web.EnsureUser("Domain\Username");
| improve this answer | |
  • As Raymond said, use SPUser type as input to AssignedTo, or send in the Users ID to the field by calling: yourUser.ID – Robert Lindgren Apr 18 '13 at 14:42
  • Thanks. This worked. The only tweak I had to make was escaping the backslash, so it was "Domain\\Username". – LFurness Apr 18 '13 at 14:49

This is how you can assing a task to SOMEBODY

   var somebody = new SPFieldValueCollection();
   somebody.Add(new SPFieldUserValue(web, user.ID, user.Name));
   myTask["AssignedTo"] = somebody;

the status column should be ok

| improve this answer | |

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.