在自定义工作流程活动中使用 OUT 参数时出错

发布于 2024-10-14 09:58:24 字数 8850 浏览 3 评论 0原文

更新: 如何创建 out 变量?任何人都可以帮我找出我的代码中做错了什么。我收到错误... 依赖属性“ListItem”的错误类型“System.Int32”与值类型“System.String”不匹配。 参数名称:值

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.SharePoint.WorkflowActions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using System.Diagnostics;


namespace TestEmailDistributionActivity
{
    public partial class Activity1: SequenceActivity
    {
        EventLog _log = new EventLog("Email Distribution");
        SPList _list;
        SPFieldUserValueCollection objUserFieldValueCol;
        string semailsettingKeyword1;
        string semailsettingKeyword2;
        string sender;
        string semailsubject;
        string semailfrom;
        string toField;

        public Activity1()
        {
            InitializeComponent();
        }
        public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof(Activity1));
        [DescriptionAttribute("__Context")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public WorkflowContext __Context
        {
            get
            {
                return ((WorkflowContext)(base.GetValue(Activity1.__ContextProperty)));
            }
            set
            {
                base.SetValue(Activity1.__ContextProperty, value);
            }
        }
        public static DependencyProperty ListIdProperty = DependencyProperty.Register("ListId", typeof(string), typeof(Activity1));
        [DescriptionAttribute("ListId")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string ListId 
        { 
            get 
            {
                return ((string)(base.GetValue(Activity1.ListIdProperty))); 
            } 
            set 
            {
                base.SetValue(Activity1.ListIdProperty, value); 
            } 
        }
        public static DependencyProperty ListItemProperty = DependencyProperty.Register("ListItem", typeof(int), typeof(Activity1));
        [DescriptionAttribute("ListItem")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public int ListItem 
        {
            get 
            {
                return ((int)(base.GetValue(Activity1.ListItemProperty))); 
            } 
            set 
            {
                base.SetValue(Activity1.ListItemProperty, value);
            } 
        }
        public static DependencyProperty RecipientsProperty = DependencyProperty.Register("Recipients", typeof(string), typeof(Activity1));
        [DescriptionAttribute("Recipients")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string Recipients
        {
            get
            {
                return ((string)(base.GetValue(Activity1.RecipientsProperty)));
            }
            set
            {
                base.SetValue(Activity1.ListItemProperty, value);
            }
        } 
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {

            _log.Source = "Share Point Workflows";  
           // _log.WriteEntry("Code entered into Excute Method");
            try        
            {                
                //Execute method as a elevated method  
                SPSecurity.CodeToRunElevated elevatedExecuteMethod = new SPSecurity.CodeToRunElevated(ExecuteMethod); 
                SPSecurity.RunWithElevatedPrivileges(elevatedExecuteMethod);
            }          
            catch (Exception ex)     
            {           
                _log.WriteEntry("Error" + ex.Message.ToString(), EventLogEntryType.Error);   
            } 
            return ActivityExecutionStatus.Closed;  
        }
        private void ExecuteMethod()
        {
           // _log.WriteEntry("Code entered into ExcuteMethod()");
            //retrieveing the Site object    
            SPSite _site = new SPSite(__Context.Site.ID); 
            //retrieveing the Web object             
            //SPWeb _web = (SPWeb)(__Context.Web);  
            SPWeb _web = _site.OpenWeb(__Context.Web.ID);
            //retrieveing the list object        
            _list = _web.Lists[new Guid(this.ListId)];  
            //retrieveing the list item object         
            SPListItem _listItem = _list.GetItemById(this.ListItem);
            try
            {
                using (SPSite mysite = new SPSite("http://dlglobaltest.dl.com/Admin/IT/Application%20Development%20Group/TestEmailDistribution"))
                {
                    using (SPWeb myweb = mysite.OpenWeb())
                    {
                        _log.WriteEntry("message from activity");
                        semailsubject = _listItem["E-Mail Subject"].ToString();
                        semailfrom = _listItem["emalfrom"].ToString();
                        SPList settingsList = myweb.Lists["Settings"];
                        //SPListItem _settingslistItem = settingsList.GetItemById(this.ListItem);
                        //string sender = _settingslistItem["Sender"].ToString();
                        SPQuery oQuery = new SPQuery();
                        oQuery.Query = "<Where><Contains><FieldRef Name='Sender' /><Value Type='Text'>[email protected]</Value></Contains></Where>";
                        SPListItemCollection ColListItems = settingsList.GetItems(oQuery);
                        foreach (SPListItem oListItem in ColListItems)
                        {
                            semailsettingKeyword1 = oListItem["Keyword1"].ToString();
                            semailsettingKeyword2 = oListItem["Keyword2"].ToString();
                            sender = oListItem["Sender"].ToString();
                            //SPFieldUserValue objUserFieldValue = new SPFieldUserValue(myweb, oListItem["Recipients"].ToString());

                            if ((semailsubject.Contains(semailsettingKeyword1)) || (semailsubject.Contains(semailsettingKeyword2)))
                            {
                                objUserFieldValueCol = new SPFieldUserValueCollection(myweb, oListItem["Recipients"].ToString());
                                for (int i = 0; i < objUserFieldValueCol.Count; i++)
                                {
                                    toField = objUserFieldValueCol[i].User.Email;
                                    this.Recipients = toField.ToString();//**getting the error here.**

                                }
                                _log.WriteEntry(toField);

                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _log.WriteEntry("Error" + ex.Message.ToString(), EventLogEntryType.Error);   
            }

        }
    }
}

操作文件:

<Action Name="get Recipients" 
            ClassName="TestEmailDistributionActivity.Activity1"
            Assembly="TestEmailDistributionActivity, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ef7ee5e33fadc78e" 
            AppliesTo="all" Category="Custom">
        <RuleDesigner Sentence="get all column values from %1 and %3 from Settings list"> 
            <FieldBind Field="ListId,ListItem" Text="this list" Id="1" DesignerType="ChooseListItem" />
            <FieldBind Field="Recipients" Text="get Recipients" Id="1" DesignerType="ParameterNames" />
        </RuleDesigner>   
        <Parameters> 
            <Parameter Name="__Context" Type="Microsoft.SharePoint.WorkflowActions.WorkflowContext" Direction="In" /> 
            <Parameter Name="ListId" Type="System.String, mscorlib" Direction="In" /> 
            <Parameter Name="ListItem" Type="System.Int32, mscorlib" Direction="In" /> 
            <Parameter Name="Recipients" Type="System.String, mscorlib" Direction="Out" /> 
        </Parameters> 
    </Action>  

谢谢,

Update:
How to create a out variable?Can anyone please help me find out what am I doing wrong in my code. I am getting the error...
ErrorType 'System.Int32' for dependency property 'ListItem' does not match with value type 'System.String'.
Parameter name: value

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.SharePoint.WorkflowActions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using System.Diagnostics;


namespace TestEmailDistributionActivity
{
    public partial class Activity1: SequenceActivity
    {
        EventLog _log = new EventLog("Email Distribution");
        SPList _list;
        SPFieldUserValueCollection objUserFieldValueCol;
        string semailsettingKeyword1;
        string semailsettingKeyword2;
        string sender;
        string semailsubject;
        string semailfrom;
        string toField;

        public Activity1()
        {
            InitializeComponent();
        }
        public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof(Activity1));
        [DescriptionAttribute("__Context")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public WorkflowContext __Context
        {
            get
            {
                return ((WorkflowContext)(base.GetValue(Activity1.__ContextProperty)));
            }
            set
            {
                base.SetValue(Activity1.__ContextProperty, value);
            }
        }
        public static DependencyProperty ListIdProperty = DependencyProperty.Register("ListId", typeof(string), typeof(Activity1));
        [DescriptionAttribute("ListId")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string ListId 
        { 
            get 
            {
                return ((string)(base.GetValue(Activity1.ListIdProperty))); 
            } 
            set 
            {
                base.SetValue(Activity1.ListIdProperty, value); 
            } 
        }
        public static DependencyProperty ListItemProperty = DependencyProperty.Register("ListItem", typeof(int), typeof(Activity1));
        [DescriptionAttribute("ListItem")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public int ListItem 
        {
            get 
            {
                return ((int)(base.GetValue(Activity1.ListItemProperty))); 
            } 
            set 
            {
                base.SetValue(Activity1.ListItemProperty, value);
            } 
        }
        public static DependencyProperty RecipientsProperty = DependencyProperty.Register("Recipients", typeof(string), typeof(Activity1));
        [DescriptionAttribute("Recipients")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string Recipients
        {
            get
            {
                return ((string)(base.GetValue(Activity1.RecipientsProperty)));
            }
            set
            {
                base.SetValue(Activity1.ListItemProperty, value);
            }
        } 
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {

            _log.Source = "Share Point Workflows";  
           // _log.WriteEntry("Code entered into Excute Method");
            try        
            {                
                //Execute method as a elevated method  
                SPSecurity.CodeToRunElevated elevatedExecuteMethod = new SPSecurity.CodeToRunElevated(ExecuteMethod); 
                SPSecurity.RunWithElevatedPrivileges(elevatedExecuteMethod);
            }          
            catch (Exception ex)     
            {           
                _log.WriteEntry("Error" + ex.Message.ToString(), EventLogEntryType.Error);   
            } 
            return ActivityExecutionStatus.Closed;  
        }
        private void ExecuteMethod()
        {
           // _log.WriteEntry("Code entered into ExcuteMethod()");
            //retrieveing the Site object    
            SPSite _site = new SPSite(__Context.Site.ID); 
            //retrieveing the Web object             
            //SPWeb _web = (SPWeb)(__Context.Web);  
            SPWeb _web = _site.OpenWeb(__Context.Web.ID);
            //retrieveing the list object        
            _list = _web.Lists[new Guid(this.ListId)];  
            //retrieveing the list item object         
            SPListItem _listItem = _list.GetItemById(this.ListItem);
            try
            {
                using (SPSite mysite = new SPSite("http://dlglobaltest.dl.com/Admin/IT/Application%20Development%20Group/TestEmailDistribution"))
                {
                    using (SPWeb myweb = mysite.OpenWeb())
                    {
                        _log.WriteEntry("message from activity");
                        semailsubject = _listItem["E-Mail Subject"].ToString();
                        semailfrom = _listItem["emalfrom"].ToString();
                        SPList settingsList = myweb.Lists["Settings"];
                        //SPListItem _settingslistItem = settingsList.GetItemById(this.ListItem);
                        //string sender = _settingslistItem["Sender"].ToString();
                        SPQuery oQuery = new SPQuery();
                        oQuery.Query = "<Where><Contains><FieldRef Name='Sender' /><Value Type='Text'>[email protected]</Value></Contains></Where>";
                        SPListItemCollection ColListItems = settingsList.GetItems(oQuery);
                        foreach (SPListItem oListItem in ColListItems)
                        {
                            semailsettingKeyword1 = oListItem["Keyword1"].ToString();
                            semailsettingKeyword2 = oListItem["Keyword2"].ToString();
                            sender = oListItem["Sender"].ToString();
                            //SPFieldUserValue objUserFieldValue = new SPFieldUserValue(myweb, oListItem["Recipients"].ToString());

                            if ((semailsubject.Contains(semailsettingKeyword1)) || (semailsubject.Contains(semailsettingKeyword2)))
                            {
                                objUserFieldValueCol = new SPFieldUserValueCollection(myweb, oListItem["Recipients"].ToString());
                                for (int i = 0; i < objUserFieldValueCol.Count; i++)
                                {
                                    toField = objUserFieldValueCol[i].User.Email;
                                    this.Recipients = toField.ToString();//**getting the error here.**

                                }
                                _log.WriteEntry(toField);

                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _log.WriteEntry("Error" + ex.Message.ToString(), EventLogEntryType.Error);   
            }

        }
    }
}

Actions file:

<Action Name="get Recipients" 
            ClassName="TestEmailDistributionActivity.Activity1"
            Assembly="TestEmailDistributionActivity, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ef7ee5e33fadc78e" 
            AppliesTo="all" Category="Custom">
        <RuleDesigner Sentence="get all column values from %1 and %3 from Settings list"> 
            <FieldBind Field="ListId,ListItem" Text="this list" Id="1" DesignerType="ChooseListItem" />
            <FieldBind Field="Recipients" Text="get Recipients" Id="1" DesignerType="ParameterNames" />
        </RuleDesigner>   
        <Parameters> 
            <Parameter Name="__Context" Type="Microsoft.SharePoint.WorkflowActions.WorkflowContext" Direction="In" /> 
            <Parameter Name="ListId" Type="System.String, mscorlib" Direction="In" /> 
            <Parameter Name="ListItem" Type="System.Int32, mscorlib" Direction="In" /> 
            <Parameter Name="Recipients" Type="System.String, mscorlib" Direction="Out" /> 
        </Parameters> 
    </Action>  

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

有深☉意 2024-10-21 09:58:24

此代码是错误的:

SPSite _site = new SPSite(__Context.Site.Url); 
SPWeb _web = (SPWeb)(__Context.Web); 

在提升的上下文中操作时,您必须重新创建站点和 Web。您重新创建了该站点,但没有重新创建网络。因此,网络可能在错误的上下文中引用了某个网站。

我通常只是这样做(确保你在使用中处置对象)

SPSite _site = new SPSite(__Context.Site.Id); 
SPWeb _web = _site.OpenWeb(__Context.Web.Id); 

This code is wrong:

SPSite _site = new SPSite(__Context.Site.Url); 
SPWeb _web = (SPWeb)(__Context.Web); 

When operating in an elevated context, you must re-create the site and web. You have re-created the site, but not the web. So the web is probably referencing a site in the wrong context.

I normally just do (make sure you Dispose the objects in a using)

SPSite _site = new SPSite(__Context.Site.Id); 
SPWeb _web = _site.OpenWeb(__Context.Web.Id); 
眼眸印温柔 2024-10-21 09:58:24

user346514 的代码几乎可以工作,只需注意:

public string Recipients
        {
            get
            {
                return ((string)(base.GetValue(Activity1.RecipientsProperty)));
            }
            set
            {
                base.SetValue(Activity1.ListItemProperty, value);
            }
        } 

必须是:

public string Recipients
        {
            get
            {
                return ((string)(base.GetValue(Activity1.RecipientsProperty)));
            }
            set
            {
                base.SetValue(Activity1.RecipientsProperty, value);
            }
        } 

另外,在 ACTIONS 文件上:

<RuleDesigner Sentence="get all column values from %1 and %3 from Settings list"> 
            <FieldBind Field="ListId,ListItem" Text="this list" Id="1" DesignerType="ChooseListItem" />
            <FieldBind Field="Recipients" Text="get Recipients" Id="1" DesignerType="ParameterNames" />
        </RuleDesigner>

必须类似于:

    <RuleDesigner Sentence="get all column values from %1 and %2 from Settings list. Store result on %3."> 
                <FieldBind Field="ListId,ListItem" Text="this list" Id="1" DesignerType="ChooseListItem" />
<FieldBind Field="ListId2,ListItem2" Text="this list" Id="2" DesignerType="ChooseListItem" />
 <FieldBind Field="Recipients" Text="get Recipients" Id="3" DesignerType="ParameterNames" />
            </RuleDesigner>

%1 , %2, %3 应该匹配并且顺序与 相同。
这些字段也必须定义,但不需要相同的顺序。

我知道这是一篇旧帖子,但以防万一有人需要带有 OUT 参数的工作流活动示例。

The code from user346514 almost work, just pay attention to:

public string Recipients
        {
            get
            {
                return ((string)(base.GetValue(Activity1.RecipientsProperty)));
            }
            set
            {
                base.SetValue(Activity1.ListItemProperty, value);
            }
        } 

Must be:

public string Recipients
        {
            get
            {
                return ((string)(base.GetValue(Activity1.RecipientsProperty)));
            }
            set
            {
                base.SetValue(Activity1.RecipientsProperty, value);
            }
        } 

Also, on the ACTIONS file:

<RuleDesigner Sentence="get all column values from %1 and %3 from Settings list"> 
            <FieldBind Field="ListId,ListItem" Text="this list" Id="1" DesignerType="ChooseListItem" />
            <FieldBind Field="Recipients" Text="get Recipients" Id="1" DesignerType="ParameterNames" />
        </RuleDesigner>

Must be something like:

    <RuleDesigner Sentence="get all column values from %1 and %2 from Settings list. Store result on %3."> 
                <FieldBind Field="ListId,ListItem" Text="this list" Id="1" DesignerType="ChooseListItem" />
<FieldBind Field="ListId2,ListItem2" Text="this list" Id="2" DesignerType="ChooseListItem" />
 <FieldBind Field="Recipients" Text="get Recipients" Id="3" DesignerType="ParameterNames" />
            </RuleDesigner>

The %1 , %2, %3 Should match and in the same order as .
These fields must be defined also on but don't need the same order.

I know it's an old post, but just in case someone needs a example of workflow activities with OUT parameters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文