Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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();
    }
}
share|improve this question
    
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? –  LCountee Apr 18 '13 at 14:33
add comment

2 Answers

up vote 2 down vote accepted

you should use SPUser field type in this situation.

      newTask["AssignedTo"] = web.EnsureUser("Domain\Username");
share|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". –  LCountee Apr 18 '13 at 14:49
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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