locked
how to assign sharepoint task to multiple users programatically RRS feed

  • Question

  • Hi all,

    I am trying to assign workflow task to multiple users using the following code 

    SPFieldUserValueCollection UserValueCollection = new SPFieldUserValueCollection(workflowProperties.Web, TempListitem["Technician"].ToString());

     createTaskWithContentType1_TaskProperties1.AssignedTo = UserValueCollection.ToString();

    If I assign the task to the single user it works fine but I am not able to assign task to the multiple users..

     

    Can anyone assist me regarding this issue please..

           

     

    Thursday, November 10, 2011 5:56 AM

All replies

  • Hi,

    Kindly check once, the Task list you created is the default task list or the custom Task list ?

    The AssignedTo field should be marked as "Multi" so that it allows multiple values.

    Let me know if this could be the issue.

     

    Ali

    Thursday, November 10, 2011 7:03 AM
  • I am using the default task list and I have also set the option to allow multiple values..
    Thursday, November 10, 2011 7:08 AM
  • Hi 

    Try using this approach.

    First hide the assignedTo columns in Task List.

    Create a New Column with type PeoplePicker with Multiple users.

    once new columns is created then 

    1. Fetch the current TaskID.

    2. Retrieve the current list item using the above taskID from Task List.

    3. Then assign multiple users to newly created columns SEPERATED by semi colon.

    Try this approach. Let me know if you need any code based help. I'll try to provide a sample code.

     

    Regards,

    Ali


    Thanks, Ali Yasir
    Thursday, November 10, 2011 7:33 AM
  • You can try the following code for reference

     

    SPList taskList = workflowProperties.TaskList;
                SPListItem taskItem = taskList.GetItemById(PreApprovalTaskChanged_AfterProperties.TaskItemId);
                if (RequestFields.Persons != null && RequestFields.Persons.Length > 0)
                {

                    SPUser user;
                    SPFieldUserValueCollection fuvc = new SPFieldUserValueCollection();
                    foreach (Contact person in pRequestFields.Persons)
                    {
                        user = workflowProperties.Web.EnsureUser(person.LoginName);
                        fuvc.Add(new SPFieldUserValue(workflowProperties.Web, user.ID, user.Name));
                    }

                    taskItem["ApprovingPersons"] = fuvc;
                    // note this causes onChanged event called
                    taskItem.SystemUpdate(false);
                }
     


    Thanks, Ali Yasir
    Thursday, November 10, 2011 7:33 AM
  • If above code still not works then it means 

    you need to use SPBuildInFieldId. Follow the below code.

    SPGroup group = item.Web.Groups["<GroupName>"];

                    SPUser user;

                    userValueCollection = new SPFieldUserValueCollection();

                    foreach (SPUser person in group.Users)

                    {

                        user = item.Web.EnsureUser(person.LoginName);

                        userValueCollection.Add(new SPFieldUserValue(item.Web, user.ID, user.Name));

     

                    }

     

                    item[SPBuiltInFieldId.AssignedTo] = userValueCollection;

                    item.Update();


    Thanks, Ali Yasir
    Thursday, November 10, 2011 7:43 AM
  • Hi ,,

    I am trying to assign task using create task activity in statemachine workflow using taskproperties..

    I tried different approaches to assign task to multiple users as bellow,,

    => SPFieldUserValueCollection UserValueCollection = new SPFieldUserValueCollection(workflowProperties.Web, TempListitem["Technician"].ToString());

     createTaskWithContentType1_TaskProperties1.AssignedTo = UserValueCollection.tostring();

     

    =>createTaskWithContentType1_TaskProperties1.AssignedTo = @"testdomain\spadmin;testdomain\testuser";

     

     But still it is not working..

    Thursday, November 10, 2011 7:56 AM
  • Hi,

    As suggested earlier, try creating a new custom column similar to  "AssignedTo" Field with type PeoplePicker with Multiple users.

    Then try to hard code the values. Please let me know if problem still occurs.

    are you getting desired value on "createTaskWithContentType1_TaskProperties1.AssignedTo" when you put a BreakPoint ?


    Thanks, Ali Yasir
    Thursday, November 10, 2011 8:47 AM
  • I created the new field and used the following code in ontaskcreated activity

    using (SPSite spSite = new SPSite("http://moss-suppor:83/HelpDesk/"))

                {

                    using (SPWeb spWeb = spSite.RootWeb)

                    {

                        SPList list = workflowProperties.TaskList;

                        var listItem = list.GetItemById(OnTechnicianTaskCreated.AfterProperties.TaskItemId);

     

                        listItem["Technicians"] = UserValueCollection;

                        listItem.Update();

      

                    }

                }

     

    But I am getting error as "Object reference not set instance of an object".

    Thursday, November 10, 2011 8:53 AM
  • Can you please try the below code :

     

    SPListItem item = list.Items.GetItemById(OnTechnicianTaskCreated.AfterProperties.TaskItemId);

    ..

    item[SPBuiltInFieldId.AssignedTo] = userValueCollection;

    item[SPBuiltInFieldId.WorkflowVersion] = 1;

    item.Update();

     

     

     


    Thanks, Ali Yasir
    Thursday, November 10, 2011 9:22 AM
  • I tried this code but still I am getting the same error "Object reference not set to an instance of an object."

     

    But if I get the item by index as below it works fine..

     

     

                        var listItem = list.Items[0];

                        listItem["Technicians"] = UserValueCollection;

                        listItem.Update();

     

    I think it is because of custom task content type.. when I try to update task with default content type it works fine but when I try to update the task with custom content type it is throwing error as object reference not set to instance of an object..

     


    • Edited by MNRSPDev Thursday, November 10, 2011 10:08 AM
    Thursday, November 10, 2011 9:44 AM
  • Instead of OnTaskCreated activity please try to implement the functionality as suggested by Ali inside a Code Activity placed next to the CreateTask activity.
    Thursday, November 10, 2011 11:52 PM
  • I tried with code activity also but still I am getting the same error..
    Friday, November 11, 2011 6:44 AM
  • Apply a breakpoint and see at what point do you get the null reference.

    Can you please paste the code snippet that you wrote inside code activity

    Friday, November 11, 2011 10:03 AM