The MenuItemsBuilder inherits from ExpressionBuilderBase :  IExpressionBuilder which is the base class for all ModelItem Expression Builders. An ExpressionBuilderBase uses an IExpressionItemsProvider to provide a List<ListItem> that an expression builder can use to create the ModelItem expression. The MenuItemsBuilder uses the SharePointListItems provider as the menu ItemsSource. SharePointListItems returns SharePoint list titles. An ExpressionBuilderBase  uses an IExpressionTypeConverter to convert a List<ListItem> into a ModelItem expression string. The MenuItemsBuilder converts the selected menu item to a quoted string. See NameValueFormBuilder for an example of converting a List<ListItem> to a VisualBasicValue<Dictionary<string, object>> used for a SharePointAddItem value list.

An Action<T> onUpdated delegate is used to pass metadata back to the plug-in caller.

  [ExpressionBuilderMetadata(Name = "MenuItemsBuilder", Description = "My Test Builder", ImagePath = "Images/TestBuilder.jpg")]
  public class MenuItemsBuilder : ExpressionBuilderBase
  {
    Action<ObservableCollection<ListItem>> _onUpdated;
    private ObservableCollection<ListItem> _result = new ObservableCollection<ListItem>();

    private System.Windows.Controls.MenuItem _contextMenu = null;

    protected override IEnumerable<ListItem> RunExtension(IEnumerable<ListItem> metadata)
    {
      _contextMenu = base.ItemsControl as System.Windows.Controls.MenuItem;

      if (this.ItemsProvider != null)
      {
        this.ItemsProvider.MetaData = metadata;

        foreach (ListItem item in this.ItemsProvider.Items)
        {
          _contextMenu.Items.Add(AddMenuItem(item.Name, item.Value.ToString()));
        }
      }

      _contextMenu.IsSubmenuOpen = true;

      return metadata;

    }

    private System.Windows.Controls.MenuItem AddMenuItem(string name, string header)
    {
      System.Windows.Controls.MenuItem menuItem = new System.Windows.Controls.MenuItem();
      menuItem.Name = name;
      menuItem.Header = header;
      menuItem.Click += new System.Windows.RoutedEventHandler(menuItem_Click);

      return menuItem;
    }

    void menuItem_Click(object sender, System.Windows.RoutedEventArgs e)
    {
      string header = (sender as System.Windows.Controls.MenuItem).Header.ToString();
      object convertedValue = null;
      if (this.Converter != null)
      {
        convertedValue = this.Converter.CommitedText(new List<ListItem>{new ListItem{Name = header, Value = header}});
      }
      base.OnUpdated(convertedValue);
    }
  }

ModelItem.ListTitle Example

<ModelItem path="ModelItem.ListTitle" 
builderType="MenuItemsBuilder" 
itemsProviderType="SharePointListItems" 
converterType="QuotedValueExpressionTypeConverter"/>

SharePointListItems : ExpressionBuilderItemsProvider

Provides list item titles for expression menu. An OwnerActivity returns the url used to get list items; therefore, the list provider is dependent on a qualified url.

  [ExpressionBuilderMetadata(Name = "SharePointListItems", Description = "", ImagePath = "")]
  public class SharePointListItems : ExpressionBuilderItemsProviderBase
  {
    protected override List<ListItem> RunExtension(IEnumerable<ListItem> metadata)
    {
      ModelItem ownerActivity = (from m in metadata where m.Name == "OwnerActivity" select m.Value).First() as ModelItem;
      ModelItem baseActivity = ownerActivity.BaseActivity("SharePointBaseActivity");
      string url = string.Empty;
      if (ownerActivity != null)
      {
        url = baseActivity.Expression("Url").ToString();
      }

      string path = (from m in metadata select m).ToList()[1].Value as string;  
      SharePointItems spItems = new SharePointItems(url, path);
      return spItems.Items;
    }
  }

SharePoint List Titles

    private List<MyData.ExpressionBuilder.Model.ListItem> ListTitles()
    {
      List<MyData.ExpressionBuilder.Model.ListItem> nameValues = new List<MyData.ExpressionBuilder.Model.ListItem>();

      //GenericList is the default for this version
      BaseType baseType = BaseType.GenericList;

      if (_expressionPath == "ModelItem.LibraryTitle")
        baseType = BaseType.DocumentLibrary;

      using (ClientContext clientContext = new ClientContext(_url))
      {
        ListCollection lists = clientContext.Web.Lists;
        clientContext.Load(lists);

        try
        {
          System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
          clientContext.ExecuteQuery();
        }
        finally
        {
          System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
        }

        var items = from l in lists.ToList() where !l.Hidden && l.BaseType == baseType orderby l.Title select l;

        foreach (var l in items)
        {
          nameValues.Add(new MyData.ExpressionBuilder.Model.ListItem(l.Title.Replace(" ", string.Empty), l.Title));
        }
      }

      return nameValues;
    }

Last edited Oct 10, 2011 at 4:16 AM by dvana, version 6

Comments

No comments yet.