[Designer(typeof(MyData.Activities.AddItemDesigner))]
  public sealed class SharePointAddItem : SharePointBaseActivity
  {
    // Define an activity input arguments
    public InArgument<Dictionary<string, object>> FieldValues { get; set; }
    
    //CodeActivityContext
    protected override void Execute(NativeActivityContext context)
    {
      // Obtain the runtime value of the input arguments
      string url = context.GetValue(this.Url);
      string listTitle = context.GetValue(this.ListTitle);
      Dictionary<string, object> fieldValues = context.GetValue(this.FieldValues);

      if (this.NetworkCredential.Get(context) != null)
      {
        using (ClientContext clientContext = new ClientContext(url))
        {
          clientContext.Credentials = NetworkCredential.Get(context);

          Microsoft.SharePoint.Client.List list = clientContext.Web.Lists.GetByTitle(listTitle);
          ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
          Microsoft.SharePoint.Client.ListItem listItem;

          listItem = list.AddItem(itemCreateInfo);

          foreach (KeyValuePair<string, object> v in fieldValues)
          {
            listItem[v.Key] = v.Value.ToString();
          }

          listItem.Update();

          //Batch Update
          try
          {
            clientContext.ExecuteQuery();
          }
          catch (Exception ex)
          {
            //Production: Log Exception
          }
        }
      }
      else
      {
        //NOTE: Throw specific exception in a production application
        throw new Exception("NetworkCredentials");
      }
    }
  }
XAML
<sap:ActivityDesigner  x:Class="MyData.Activities.AddItemDesigner"
    xmlns:s="clr-namespace:System;assembly=mscorlib"            
    xmlns:net="clr-namespace:System.Net;assembly=System" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sap="clr-namespace:System.Activities.Presentation;assembly=System.Activities.Presentation"               
    xmlns:sapv="clr-namespace:System.Activities.Presentation.View;assembly=System.Activities.Presentation"
    xmlns:sapc="clr-namespace:System.Activities.Presentation.Converters;assembly=System.Activities.Presentation" Collapsible="True">

    <sap:ActivityDesigner.Resources>
        <sapc:ArgumentToExpressionConverter 
                x:Key="ArgumentToExpressionConverter" />

        <DataTemplate x:Key="ShowAsCollapsed">
            <TextBlock>Expand to edit properties</TextBlock>
        </DataTemplate>
        <DataTemplate x:Key="ShowAsExpanded">
            <Grid>
                
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>

                <TextBlock Text="Credentials" Grid.Row ="0" Grid.Column="1"
            HorizontalAlignment="Left" VerticalAlignment="Center" />

                <sapv:ExpressionTextBox HintText="Credential" 
                Grid.Row ="0" Grid.Column="2" MaxWidth="150" MinWidth="150" Margin="5"
                OwnerActivity="{Binding Path=ModelItem}"
                Expression= {Binding Path=ModelItem.NetworkCredential, Mode=TwoWay, 
                Converter={StaticResource ArgumentToExpressionConverter}, 
                ConverterParameter=In }"
                ExpressionType="net:ICredentials" GotFocus="ExpressionTextBox_GotFocus" />
                

                <TextBlock Text="Url" Grid.Row ="1" Grid.Column="1"
            HorizontalAlignment="Left" VerticalAlignment="Center" />
                
                <sapv:ExpressionTextBox HintText="SharePont 2010 Url" 
                Grid.Row ="1" Grid.Column="2" MaxWidth="150" MinWidth="150" Margin="5"
                OwnerActivity="{Binding Path=ModelItem}"
                Expression="{Binding Path=ModelItem.Url, Mode=TwoWay, 
                Converter={StaticResource ArgumentToExpressionConverter}, 
                ConverterParameter=In }"
                ExpressionType="s:String" GotFocus="ExpressionTextBox_GotFocus" />

                <TextBlock Text="List Title" Grid.Row ="2" Grid.Column="1"
            HorizontalAlignment="Left" VerticalAlignment="Center" />
                
                <sapv:ExpressionTextBox HintText="SharePoint 2010 List Title" 
                Grid.Row ="2" Grid.Column="2" MaxWidth="150" MinWidth="150" Margin="5"
                OwnerActivity="{Binding Path=ModelItem}"
                Expression="{Binding Path=ModelItem.ListTitle, Mode=TwoWay, 
                Converter={StaticResource ArgumentToExpressionConverter}, 
                ConverterParameter=In }"
                ExpressionType="s:String" GotFocus="ExpressionTextBox_GotFocus" />


                <TextBlock Text="Values" Grid.Row ="3" Grid.Column="1"
            HorizontalAlignment="Left" VerticalAlignment="Center" />
                <sapv:ExpressionTextBox HintText="List Item Values" 
                Grid.Row ="3" Grid.Column="2" MaxWidth="150" MinWidth="150" Margin="5"
                OwnerActivity="{Binding Path=ModelItem}"
                Expression="{Binding Path=ModelItem.FieldValues, Mode=TwoWay, 
                Converter={StaticResource ArgumentToExpressionConverter}, 
                ConverterParameter=In }"
                ExpressionType="s:Object" GotFocus="ExpressionTextBox_GotFocus" Loaded="DictionaryExpressionTextBox_Loaded" />
                
            </Grid>
        </DataTemplate>

        <Style x:Key="StyleWithCollapse" TargetType="{x:Type ContentPresenter}">
            <Setter Property="ContentTemplate" 
                Value="{DynamicResource ShowAsExpanded}"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=ShowExpanded}" Value="False">
                    <Setter Property="ContentTemplate" 
                        Value="{DynamicResource ShowAsCollapsed }"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </sap:ActivityDesigner.Resources>
      
    <sap:ActivityDesigner.Icon>
        <DrawingBrush>
            <DrawingBrush.Drawing>
                <ImageDrawing>
                    <ImageDrawing.Rect>
                        <Rect Location="0,0"  Size="16,16" />
                    </ImageDrawing.Rect>
                    <ImageDrawing.ImageSource>

                        <BitmapImage UriSource="SharePointAddItem.png"/>
                    </ImageDrawing.ImageSource>
                </ImageDrawing>
            </DrawingBrush.Drawing>
        </DrawingBrush>
    </sap:ActivityDesigner.Icon>

    <Grid>
        <ContentPresenter Style="{DynamicResource StyleWithCollapse}" 
            Content="{Binding}" />
    </Grid>
</sap:ActivityDesigner>
Result
image

Last edited Oct 10, 2011 at 3:50 AM by dvana, version 10

Comments

No comments yet.