locked
How to assign task programmatically in C#? RRS feed

  • Question

  • I am trying to assign a task programmatically, but it kept saying the field is read only:

    SPListItem oTaskItem = oTaskList.Items.Add();
    oTaskItem["Title"] = "Review for " + document.Name;
    oTaskItem["Priority"] = "(2) Normal";
    oTaskItem["Assigned To"] = uReviewer;
    oTaskItem["Description"] = "Review for document: " + document.Name;

    Can someone help?
    Friday, February 5, 2010 4:19 AM

Answers

  • Use oTaskItem[SPBuiltInFieldId.AssignedTo] = uReviewer; rather than "AssignedTo"

    SPBuiltInFieldId.AssignedTo;
    SPBuiltInFieldId.Priority;
    and so on
    Friday, February 5, 2010 8:51 AM
  • HI

    I am trying to assign a task programmatically, but it kept saying the field is read only:

    SPListItem oTaskItem = oTaskList.Items.Add();
    oTaskItem["Title"] = "Review for " + document.Name;
    oTaskItem["Priority"] = "(2) Normal";
    oTaskItem["Assigned To"] = uReviewer;
    oTaskItem["Description"] = "Review for document: " + document.Name;

    Can someone help?

    This might be the problem where  bolded the text here  Assigned to field is of type user and group.

    So you have to give uReviewer of type SPuser.
    To get Spuser 

    SPFieldUserValue val = new SPFieldUserValue(Web, list.Items[0]["Reviewer"] as string);
    string superviser= val.LookupValue;
    SPUser spReviewer= val.User;
     
    Hope this will help you.


    Thanks Shrivallabh Kulkarni
    Friday, February 5, 2010 8:35 AM

All replies

  • Your code should work. Can you eloborate on which line exactly you are getting the message? Hope, uReviewer is of type SPUser.

    Friday, February 5, 2010 5:50 AM
  • That should work, but if I remember correctly the internal name of Assigned To is "AssignedTo" (without spaces)
    You can check the internal names of fields (and their contents + if a field is read only) with a tool like SPM2007 (sharepoint manager 2007), you can find it here: http://www.codeplex.com/spm


    Kind regards,
    Jeroen
    My blog on WSS / MOSS development is found at http://jebass.blogspot.com
    Friday, February 5, 2010 7:21 AM
  • HI

    I am trying to assign a task programmatically, but it kept saying the field is read only:

    SPListItem oTaskItem = oTaskList.Items.Add();
    oTaskItem["Title"] = "Review for " + document.Name;
    oTaskItem["Priority"] = "(2) Normal";
    oTaskItem["Assigned To"] = uReviewer;
    oTaskItem["Description"] = "Review for document: " + document.Name;

    Can someone help?

    This might be the problem where  bolded the text here  Assigned to field is of type user and group.

    So you have to give uReviewer of type SPuser.
    To get Spuser 

    SPFieldUserValue val = new SPFieldUserValue(Web, list.Items[0]["Reviewer"] as string);
    string superviser= val.LookupValue;
    SPUser spReviewer= val.User;
     
    Hope this will help you.


    Thanks Shrivallabh Kulkarni
    Friday, February 5, 2010 8:35 AM
  • Use oTaskItem[SPBuiltInFieldId.AssignedTo] = uReviewer; rather than "AssignedTo"

    SPBuiltInFieldId.AssignedTo;
    SPBuiltInFieldId.Priority;
    and so on
    Friday, February 5, 2010 8:51 AM