3

I want to add some SPUsers as approvers in a workflow but i always get some trouble with it. What I want is that when a user creates a list item, I take the user's manager, make him as an approver/participant of the workflow attached to that list, and start the workflow. Thanks in advance. Here is some code where i get an Exception without any explanation.

IEnumerable<SPContentType> childrenCT = from SPContentType childCT in list.ContentTypes
                                                                where childCT.Id.IsChildOf((SPContentTypeId)item["ContentTypeId"])
                                                                select childCT;
                        foreach (SPContentType ct in childrenCT)
                        {
                            foreach (SPWorkflowAssociation association in ct.WorkflowAssociations)
                            {
                                if (association.Name == "Validation Workflow")
                                {
                                        SPServiceContext serverContext = SPServiceContext.GetContext(site);
                                        UserProfileManager myUserProfile = new UserProfileManager(serverContext);
                                        UserProfile currentUserProfile = myUserProfile.GetUserProfile(login);
                                        string managerName = (string)currentUserProfile["manager"].Value;
                                        SPUser managerUser = web.EnsureUser(managerName);
                                        SPUser authorUser = web.EnsureUser(login);

                                        String associationData = association.AssociationData;

                                        string un = string.Format(@"<d:Assignment>
                                                                        <d:Assignee>
                                                                            <pc:Person>
                                                                                <pc:DisplayName>{0}</pc:DisplayName>
                                                                                <pc:AccountId>{1}</pc:AccountId>
                                                                                <pc:AccountType>User</pc:AccountType>
                                                                            </pc:Person>
                                                                        </d:Assignee>
                                                                        <d:Stage xsi:nil='true' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />
                                                                        <d:AssignmentType>Serial</d:AssignmentType>
                                                                    </d:Assignment>",
                                                                   managerUser.Name, managerUser.ID);

                                        string deux = string.Format(@"<d:Assignment>
                                                                        <d:Assignee>
                                                                            <pc:Person>
                                                                                <pc:DisplayName>{0}</pc:DisplayName>
                                                                                <pc:AccountId>{1}</pc:AccountId>
                                                                                <pc:AccountType>User</pc:AccountType>
                                                                            </pc:Person>
                                                                        </d:Assignee>
                                                                        <d:Stage xsi:nil='true' />
                                                                        <d:AssignmentType>Serial</d:AssignmentType>
                                                                    </d:Assignment>", 
                                                                    authorUser.Name, authorUser.ID);
                                        string approversInfo = un + deux;
                                        int start = associationData.IndexOf("<d:Assignment>");

                                        int end = associationData.IndexOf("</d:Approvers>");

                                        int repLength = end - start;
                                        association.Enabled = true;
                                        string finalAssociationData = associationData.Replace(associationData.Substring(start, repLength).Trim(), approversInfo);

                                    web.AllowUnsafeUpdates = true;
                                    site.WorkflowManager.StartWorkflow(item, association, finalAssociationData); // <== this is where i got an exception
                                    break;
                                }
                            }
                        }

Here is the value of finalAssociationData after it is set:

<dfs:myFields xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:dms="http://schemas.microsoft.com/office/2009/documentManagement/types" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:q="http://schemas.microsoft.com/office/infopath/2009/WSSList/queryFields" xmlns:d="http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields" xmlns:ma="http://schemas.microsoft.com/office/2009/metadata/properties/metaAttributes" xmlns:pc="http://schemas.microsoft.com/office/infopath/2007/PartnerControls" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><dfs:queryFields></dfs:queryFields><dfs:dataFields><d:SharePointListItem_RW><d:Approvers><d:Assignment>
                                                                        <d:Assignee>
                                                                            <pc:Person>
                                                                                <pc:DisplayName>Stéphanie Rabehaja</pc:DisplayName>
                                                                                <pc:AccountId>17</pc:AccountId>
                                                                                <pc:AccountType>User</pc:AccountType>
                                                                            </pc:Person>
                                                                        </d:Assignee>
                                                                        <d:Stage xsi:nil='true' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />
                                                                        <d:AssignmentType>Serial</d:AssignmentType>
                                                                    </d:Assignment></d:Approvers><d:ExpandGroups>true</d:ExpandGroups><d:NotificationMessage /><d:DueDateforAllTasks xsi:nil="true" /><d:DurationforSerialTasks xsi:nil="true" /><d:DurationUnits>Day</d:DurationUnits><d:CC /><d:CancelonRejection>true</d:CancelonRejection><d:CancelonChange>true</d:CancelonChange><d:EnableContentApproval>true</d:EnableContentApproval></d:SharePointListItem_RW></dfs:dataFields></dfs:myFields>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.