更新: 如何创建一个out变量?有谁可以帮我找出我在代码中做错了什么。我收到了错误...... 依赖项属性“ListItem”的ErrorType“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'>NS@de.com</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>
谢谢,
答案 0 :(得分:1)
此代码错误:
SPSite _site = new SPSite(__Context.Site.Url);
SPWeb _web = (SPWeb)(__Context.Web);
在高架环境中操作时,必须重新创建站点和Web。您重新创建了网站,但没有重新创建网站。因此,Web可能在错误的上下文中引用了一个站点。
我通常只是这样做(确保你在使用中处理对象)
SPSite _site = new SPSite(__Context.Site.Id);
SPWeb _web = _site.OpenWeb(__Context.Web.Id);
答案 1 :(得分:0)
来自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参数的工作流活动示例。