When using a custom workflow activity with elevated privileges to create an item in a list. It creates two instead of one.
What I am doing is that when a user create an item in the list . It displays the created by field as the system name instead of the users name. I have done that already. But when I create an item it keeps the one with the users name and creates one with the system name.
It should update the item instead of creating a new one.
#region [ Workflow Context Properties ]
public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof(CreatedByWorkFlowAction));
[ValidationOption(ValidationOption.Required)]
public WorkflowContext __Context
{
get
{
return ((WorkflowContext)(base.GetValue(__ContextProperty)));
}
set
{
base.SetValue(__ContextProperty, value);
}
}
#endregion [ Workflow Context Properties ]
#region Custom Workflow Properties
//List ID
public static DependencyProperty ListIdProperty = DependencyProperty.Register("ListId", typeof(string), typeof(CreatedByWorkFlowAction));
[ValidationOption(ValidationOption.Required)]
public string ListId
{
get
{
return ((string)(base.GetValue(CreatedByWorkFlowAction.ListIdProperty)));
}
set
{
base.SetValue(CreatedByWorkFlowAction.ListIdProperty, value);
}
}
//Item Properties
public static DependencyProperty ItemPropertiesProperty = DependencyProperty.Register("ItemProperties", typeof(Hashtable), typeof(CreatedByWorkFlowAction));
[ValidationOption(ValidationOption.Optional)]
public Hashtable ItemProperties
{
get
{
return ((Hashtable)(base.GetValue(CreatedByWorkFlowAction.ItemPropertiesProperty)));
}
set
{
base.SetValue(CreatedByWorkFlowAction.ItemPropertiesProperty, value);
}
}
//Overwrite
public static DependencyProperty OverwriteProperty = DependencyProperty.Register("Overwrite", typeof(bool), typeof(CreatedByWorkFlowAction));
[ValidationOption(ValidationOption.Required)]
public bool Overwrite
{
get
{
return ((bool)(base.GetValue(CreatedByWorkFlowAction.OverwriteProperty)));
}
set
{
base.SetValue(CreatedByWorkFlowAction.OverwriteProperty, value);
}
}
//HTMLBody
public static DependencyProperty NewItemIdProperty = DependencyProperty.Register("NewItemId", typeof(SPItemKey), typeof(CreatedByWorkFlowAction));
[ValidationOption(ValidationOption.Required)]
public SPItemKey NewItemId
{
get
{
return ((SPItemKey)(base.GetValue(CreatedByWorkFlowAction.NewItemIdProperty)));
}
set
{
base.SetValue(CreatedByWorkFlowAction.NewItemIdProperty, value);
}
}
#endregion
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(__Context.Web.Url))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists[new Guid(ListId)];
SPListItem newItem = list.AddItem();
foreach (DictionaryEntry entry in ItemProperties)
{
newItem[entry.Key.ToString()] = entry.Value;
}
newItem.Update();
this.NewItemId = new SPItemKey(newItem.ID);
}
}
});
the created.actions page
<?xml version="1.0" encoding="utf-8" ?>
<WorkflowInfo>
<Actions Sequential="then" Parallel="and">
<Action Name="Display created by as system name"
ClassName="WorkflowActivity.CreatedByWorkFlowAction"
Assembly="WorkflowActivity, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d78956eaba5cabfc"
AppliesTo="all"
CreatesInList="UpdateList"
Category="OMPLC-GI"
UsesCurrentItem="true">
<RuleDesigner Sentence="Create item in %1 (Output to %2) as anonymous">
<FieldBind Field="ListId,ItemProperties"
Text="this list"
Id="1"
DesignerType="CreateListItem" />
<FieldBind Field="NewItemId"
DesignerType="ParameterNames"
Text="create"
Id="2"/>
</RuleDesigner>
<Parameters>
<Parameter Name="__Context"
Type="Microsoft.SharePoint.WorkflowActions.WorkflowContext"
Direction="In"
DesignerType="Hide" />
<Parameter Name="ListId"
Type="System.String, mscorlib"
Direction="In"
DesignerType="ListNames"
Description="Canonical form of the list GUID used by this action." />
<Parameter Name="ItemProperties"
Type="System.Collections.Hashtable, mscorlib"
DesignerType="ItemProperties"
Direction="In"
Description="Collection of values used to update list item." />
<Parameter Name="Overwrite"
Type="System.Boolean, mscorlib"
Direction="In"
InitialValue="false"
Description="Overwrite any list item with this ID if it exists." />
<Parameter Name="NewItemId"
Type="Microsoft.SharePoint.Workflow.SPItemKey, Microsoft.SharePoint"
Direction="Out"
DesignerType="ParameterNames"
Description="ID of the list item created by this action." />
</Parameters>
</Action>
</Actions>