如何在项目创建时以编程方式将批准者添加到工作流并启动它 [英] How to programmatically add approvers to a workflow at item creation and start it

查看:56
本文介绍了如何在项目创建时以编程方式将批准者添加到工作流并启动它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在工作流程中添加一些SPUsers作为批准者,但是我总是遇到一些麻烦.

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. While doing this, It just throws an SPException without any message. It says "SPException was caught". The workflow started and an email was sent, but the workflow ended right away. When i look in the item's workflow, i can see that there is no participant in the workflow.

提前致谢.

Thanks in advance.

这是我的代码.

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;
                                }
                            }
                        }

推荐答案

蒂蒂,

我不建议您将批准者添加到工作流之外.

I won't suggest you add the approver outside of the workflow.

相反,您可以创建一个将涉及每个用户及其管理员的列表,然后在工作流程中,您可以查找该列表以直接在工作流程中找到当前用户的管理员作为批准者,以下是视频,其中包含有关详细步骤的视频您的参考:

Instead, you can create a list which will involve each user and their manager, then in the workflow, you can lookup the list to find the manager of current user as approver in the workflow directly, here is video with detailed steps for your reference:

具有动态批准者的SharePoint 2013批准工作流

谢谢

最好的问候


这篇关于如何在项目创建时以编程方式将批准者添加到工作流并启动它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆