A SharePointItems activity encapsulates update and delete operations and allows a simple CAMLWhere activity. A future version will support a more complex CAML activity including logical operators and a CAML expression builder. For example, a SharePointItems activity can participate in a workflow to delete all Announcements that begin with “Word”.

image

  public enum SharePointOperationEnum
  {
    GetItems, Update, Delete 
  }

  [Designer(typeof(MyData.Activities.Designers.SharePointItemsDesigner))]
  public sealed class SharePointItems : SharePointBaseActivity
  {
    [Browsable(false)]
    public Collection<Activity> Activities { get; set; }

    //Fields are used for updates and limit lists
    public InArgument<Dictionary<string, object>> FieldValues { get; set; }


    public OutArgument<CamlQuery> CamlQuery { get; set; }
    public OutArgument<ListItemCollection> ListItems { get; set; }

    public SharePointOperationEnum Operation { get; set; }
    
    private string camlWhere;

    public SharePointItems()
    {
      Activities = new Collection<Activity>();
    }

    //Cache composite activity metadata
    protected override void CacheMetadata(NativeActivityMetadata metadata)
    {
      metadata.SetChildrenCollection(Activities);
     
      base.CacheMetadata(metadata);
    }

    protected override void Execute(NativeActivityContext context)
    {

      Dictionary<string, object> fieldValues = context.GetValue(this.FieldValues);

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

          List list = clientContext.Web.Lists.GetByTitle(this.ListTitle.Get(context));
          CamlQuery camlQuery = new CamlQuery();

          //Current, this only supports ComparisonOperator.Equal, ValueType.String and does not support LogicalOperator 
          //Build Caml Where elements
          VisualBasicValue<Object> valueResult;
          foreach (CamlWhere a in this.Activities)
          {
            valueResult = a.Value.Expression as VisualBasicValue<Object>;

            camlWhere += CamlElements
              .WhereElement(a.ComparisonOperator.ToString(), a.FieldRef.Expression.ToString(),
              a.ValueType.ToString(), valueResult.ExpressionText.Replace("\"", string.Empty));
          }

          camlQuery.ViewXml =
              @"<View><Query>" + camlWhere + "</Query><RowLimit>100</RowLimit></View>";


          ListItemCollection listItems = list.GetItems(camlQuery);

          //Note: Current version does not support limiting the list to reduce payload size.
          //For example:
          //clientContext.Load(
          //      listItems,
          //      items => items.Include(
          //          item => item["Title"]));

          //Current version returns the entire schema. Limiting the schema is more efficient since the payload size is reduced
          clientContext.Load(listItems);
          clientContext.ExecuteQuery();

          ListItems.Set(context, listItems);
          CamlQuery.Set(context, camlQuery);

          //This version only supports a Delete operation
          //See SharePointInsert activity for an insert example
          if (Operation == SharePointOperationEnum.Delete)
          {
            foreach (ListItem item in listItems.ToList())
              item.DeleteObject();
          
          } else if (Operation == SharePointOperationEnum.Update)
          {
            foreach (ListItem item in listItems.ToList())
            {
              foreach (KeyValuePair<string, object> v in fieldValues)
              {
                item[v.Key] = v.Value.ToString();
              }

              //Update this list item
              item.Update();
            }
          }

          clientContext.ExecuteQuery();
        }
      }
      else
      {
        //NOTE: Throw specific exception
      }
    }
  }

Last edited Oct 14, 2011 at 2:54 AM by dvana, version 6

Comments

No comments yet.