DocumentParts can be used to LINQ query ContentParts from Word and Excel.  After dragging a DocumentPart activity into a Flowchart, a user can select a Word or Excel content reference. See ReferenceItemsProvider for a Word content controls LINQ query that provides ModelItem.Reference ExpressionBuilder list values. Figure 1 shows an example DocumentParts Flowchart.

Figure 1 – Excel and Word DocumentParts Flowchart

image

 ModelItem.Reference ExpressionBuilder

 

image

After running the workflow from Word, the DocumentParts activity uses a SharePoint document context along with one or more ContentParts to insert content into a Word document. Figure 2 shows how you can diagnose workflow activates after running a workflow. The Workflow Activities Treeview is an example of a client host subscribing to the workflow ActivityStateTracked event.

Figure 3 – Workflow Diagnostics Task Pane

image

 Code Behind

FillWordParts() adds ContentParts from Word ContentControls.

    private void FillWordParts(MemoryStream memoryStream, string namedRange, string reference)
    {
      string value = string.Empty;

      using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(memoryStream, false))
      {
        //Add ContentParts dropped onto DocumentParts
        foreach (Activity a in this.Activities)
        {
          ContentPart part = (ContentPart)a;

          if (part.NamedRange != null)
          {
            namedRange = part.NamedRange.Expression.ToString();
          }

          reference = part.Reference.Expression.ToString();
        
          var contentControls =
              wordDocument.MainDocumentPart.Document.Body.Descendants()
              .Where(c => (c is SdtBlock || c is SdtRun || c is SdtCell) && c.Descendants<Tag>().FirstOrDefault().Val.ToString() == reference)
              .Select(c => c.InnerText);
          if (contentControls.Count() > 0)
            value = contentControls.First().ToString();         

          contentParts.Add(part.Reference.Expression.ToString(), value);
        }
      }

    }
ReferenceItemsProvider
A ReferenceItemsProvider provides Word content control tag names to set a ContentPart.Reference model item expression.
  [ExpressionBuilderMetadata(Name = "ReferenceItemsProvider", Description = "", ImagePath = "")]
  public class ReferenceItems : 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 documentName = baseActivity.Expression("DocumentName").ToString();

      List<ListItem> list = null;
      
      if (ownerActivity != null)
      {
        if (Path.GetExtension(documentName) == ".docx")
        {
          var contentControls = MyData.Activities.Common.ContentControls
            (baseActivity.Expression("Url").ToString(),
            documentName,
            baseActivity.Expression("LibraryTitle").ToString());
          
          list = contentControls.Where(c => c.IsEmbedded == false).Where(c => c.HasTable == false)
              .Select(c => new ListItem { Name = c.Name, Value = c }).ToList<ListItem>();

        }
      }

      return list;
    }
  }

Example DocumentParts activity containing multiple Word ContentParts.

image

Last edited Oct 18, 2011 at 5:13 AM by dvana, version 4

Comments

No comments yet.