0

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";
        newTask.Update();
    }
}
2
  • maybe because you didn't write any metadata in the other columns? Apr 18, 2013 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, 2013 at 14:33

2 Answers 2

2

you should use SPUser field type in this situation.

      newTask["AssignedTo"] = web.EnsureUser("Domain\Username");
2
  • As Raymond said, use SPUser type as input to AssignedTo, or send in the Users ID to the field by calling: yourUser.ID Apr 18, 2013 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, 2013 at 14:49
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.