none
how to get a value from people/group field and use it in visual studio workflow

    Question

  • hai. im new in sharepoint. i want to get value from person/group column in task list and use it in visual studio state machine workflow and assign it to the selected user. a tutorial or sample coding will be much appreciated. thank you
    Tuesday, October 30, 2012 9:55 AM

Answers

All replies

  •   Hi this is a sample code how to get List<SPUsers> and List<SPGroup> from people/group field in workflow you can use workflowProperties.Item and code:

    SPFieldCollection fields = workflowProperties.Item.Fields;
                          foreach (SPField field in fields)
                          {
                              if (field.Type == SPFieldType.User)
                              {
                                  List<SPUser> users = new List<SPUser>();
                                  users = Permission.GetSPUsers(item, field.Title);
                                  foreach (SPUser userField in users)
                                  {
                                   // TO DO USERS
                                  }
                                  List<SPGroup> groups = new List<SPGroup>();
                                  groups = Permission.GetSPGroups(item, field.Title);
                                  foreach (SPGroup group in groups)
                                  {
                                     TO DO GROUPS
                                  }
                              }
                          }

      public static  List<SPUser> GetSPUsers(SPListItem item, string key)
            {
                List<SPUser> users = new List<SPUser>();
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPFieldUser field = item.Fields[key] as SPFieldUser;
                    if (field != null)
                    {
                        if (item[key] != null)
                            if (item[key].ToString() != String.Empty)
                            {
                                SPFieldUserValueCollection objUserFieldValueCol = new SPFieldUserValueCollection(item.Web, item[key].ToString());
                                for (int i = 0; i < objUserFieldValueCol.Count; i++)
                                {
                                    SPFieldUserValue singlevalue = objUserFieldValueCol[i];
                                    if (singlevalue.User == null)
                                    {
                                        //then single value is group
                                    }
                                    else
                                    {
                                        users.Add(singlevalue.User);
                                    }
                                }
                            }
                    }
                });
                return users;
            }

            public static List<SPGroup> GetSPGroups(SPListItem item, string key)
            {
                List<SPGroup> groups = new List<SPGroup>();
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {      
                SPFieldUser field = item.Fields[key] as SPFieldUser;
                if (field != null)
                {
                    if (item[key] != null)
                        if (item[key].ToString() != String.Empty)
                        {
                            SPFieldUserValueCollection objUserFieldValueCol = new SPFieldUserValueCollection(item.Web, item[key].ToString());
                            for (int i = 0; i < objUserFieldValueCol.Count; i++)
                            {
                                SPFieldUserValue singlevalue = objUserFieldValueCol[i];
                                if (singlevalue.User == null)
                                {
                                    // SP Groups
                                    SPGroup group = item.Web.SiteGroups.GetByID(singlevalue.LookupId);
                                    groups.Add(group);
                                }                 
                            }
                        }
                }
                });
                return groups;
            }



    So you can assign a task to first user from user list in properties AssignedTo in createTask method of workflow.

    createApprovalTask.TaskProperties.AssignedTo = users[0].LoginName:


     private void createApprovalTask_MethodInvoking(object sender, EventArgs e)
            {
                createApprovalTask.TaskId = Guid.NewGuid();
                createApprovalTask.TaskProperties.AssignedTo =  users[0].LoginName
                createApprovalTask.TaskProperties.DueDate = DateTime.Now.AddDays(7);
                createApprovalTask.TaskProperties.Title = "Title";
                createApprovalTask.TaskProperties.SendEmailNotification = true;
            }



    • Proposed as answer by Jacues87 Tuesday, October 30, 2012 11:40 AM
    • Edited by Jacues87 Tuesday, October 30, 2012 11:44 AM
    Tuesday, October 30, 2012 11:40 AM
  • thanks for reply. this is basically what i want to do. using AssignedTo property in create task. but im not quite get the coding. can you pls give more details
    Wednesday, October 31, 2012 1:22 AM
  • Hi MRu,

    Here is also a sample about this, please check out it:
    http://mstechsharing.blogspot.com/2012/03/sharepoint-2010-state-machine-workflows_25.html

    Thanks,
    Lhan Han

    Thanks, Lhan Han TechNet Subscriber Support in forum If you have any feedback on our support, please contact tnmff@microsoft.com.

    Monday, November 05, 2012 3:11 AM