1

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>

1
  • The item that is actioned with elevated privileges will be executed under the web applications App pool account in IIS, which means that the modified user of the document/list item will most likely display as SYSTEM ACCOUNT Commented Oct 22, 2014 at 13:02

1 Answer 1

1

If I understood your question, you workflow is run after the item has been created - the workflow is actually reacting to the item creation.

If that is the case, then the workflow doesn't need to create an item. It should obtain a reference to the existing one and update that instead.

In your code you get the list that contains the item using the .Lists collection and the Id of the list gotten from a workflow parameter. Then , the next instruction

SPListItem newItem = list.AddItem();

creates a new item, copy the current item properties to it (so the new item will have the same values as the original one) and then saves the new item.

If you plan to update the original item instead, you need to modify the aforementioned instruction to get the item instead of creating a new instance. Create (if you don't have it already) a workflow parameter for the current item id, then use the GetItemById method of the list to get the item instance and work up from there.

1
  • 1
    Could you elaborate on the Create workflow parameter for current id , would it be something like this <Parameter Name="__ListItem" Type="System.Int32, mscorlib" Direction="In" />
    – HeliosAsra
    Commented Oct 22, 2014 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.