Introduction to QueryFeed - Microsoft Web viewRunning a workflow from within a Microsoft Excel 2010...

17
Introducing the OData QueryFeed workflow activity About The OData QueryFeed sample activity shows how to create a workflow activity that consumes an OData resource, and renders entity properties in a Microsoft Excel 2010 worksheet or Microsoft Word 2010 document. Using the sample QueryFeed activity, you can consume any OData resource. The sample activity uses LINQ to project OData metadata into activity designer expression items. By setting activity expressions, a fully qualified OData query string is constructed consisting of Resource, Filter, OrderBy, and Select parameters. Executing the activity returns OData entity properties. The sample represents an end to end data movement application utilizing a number of technologies including Using LINQ to project OData entity XML to entity classes. Creating custom workflow activities. Creating custom workflow designers containing OData schema aware expression editors and WorkflowItemsPresenters. A WorkflowItemsPresenter is used to drop OData Filter activities that format a fully qualified OData filter parameter. WPF ComboBox items hosting a button and checkbox. Hosting the Windows Workflow designer within Microsoft Excel 2010. Running a workflow from within a Microsoft Excel 2010 and Microsoft Word 2010 Addin. Using a workflow Tracking Participant to subscribe to activity states. Creating ModelItem and Office extension methods. Open XML 2.0 that renders OData entity properties as a Word 2010 table. By providing feedback on this blog, or sending me an email, you can help define any part of the sample. My email address is [email protected] .

Transcript of Introduction to QueryFeed - Microsoft Web viewRunning a workflow from within a Microsoft Excel 2010...

Page 1: Introduction to QueryFeed - Microsoft Web viewRunning a workflow from within a Microsoft Excel 2010 and Microsoft Word 2010 Addin. ... From Microsoft SQL Server Management Studio,

Introducing the OData QueryFeed workflow activityAboutThe OData QueryFeed sample activity shows how to create a workflow activity that consumes an OData resource, and renders entity properties in a Microsoft Excel 2010 worksheet or Microsoft Word 2010 document. Using the sample QueryFeed activity, you can consume any OData resource. The sample activity uses LINQ to project OData metadata into activity designer expression items. By setting activity expressions, a fully qualified OData query string is constructed consisting of Resource, Filter, OrderBy, and Select parameters. Executing the activity returns OData entity properties.

The sample represents an end to end data movement application utilizing a number of technologies including

Using LINQ to project OData entity XML to entity classes. Creating custom workflow activities. Creating custom workflow designers containing OData schema aware expression editors and

WorkflowItemsPresenters. A WorkflowItemsPresenter is used to drop OData Filter activities that format a fully qualified OData filter parameter.

WPF ComboBox items hosting a button and checkbox. Hosting the Windows Workflow designer within Microsoft Excel 2010. Running a workflow from within a Microsoft Excel 2010 and Microsoft Word 2010 Addin. Using a workflow Tracking Participant to subscribe to activity states. Creating ModelItem and Office extension methods. Open XML 2.0 that renders OData entity properties as a Word 2010 table.

By providing feedback on this blog, or sending me an email, you can help define any part of the sample. My email address is [email protected].

Source Codehttp://msftdbprodsamples.codeplex.com/releases/view/94486

User StoryThe sample addresses the following user story:

As a developer, I want to create an OData feed activity so that an IT Analyst can consume any AdventureWorks OData feed within a business workflow.

Requirements1) Visual Studio .NET 2010 SP1

Page 2: Introduction to QueryFeed - Microsoft Web viewRunning a workflow from within a Microsoft Excel 2010 and Microsoft Word 2010 Addin. ... From Microsoft SQL Server Management Studio,

2) .NET Framework 4.03) WCF Data Services 5.0 for OData V3 located at

http://www.microsoft.com/en-us/download/details.aspx?id=293064) Open XML SDK located at http://www.microsoft.com/en-us/download/details.aspx?id=51245) Microsoft Excel 2010 or above6) Microsoft Word 2010 or above (optional)

How to install the sample1) Install WCF Data Services 5.0 for OData V3 (WcfDataServices.exe ) located at

http://www.microsoft.com/en-us/download/details.aspx?id=29306.2) Install OpenXMLSDKv2.msi located at http://www.microsoft.com/en-us/download/details.aspx?

id=5124.3) Open \Microsoft.Samples.SqlServer.OData\Microsoft.Samples.SqlServer.Workflows.OData.sln in

Visual Studio 2012.4) Build the solution.5) For this iteration of the sample, you will need to install the activity designer assembly into the

GAC.

How to install the activity designer assemblies into the GAC

From the Visual Studio Command Prompt, enter:

gacutil /i {FullPath}\Microsoft.Samples.SqlServer.Activities.dll

For Example:

gacutil /i C:\Projects\Microsoft.Samples.SqlServer.OData\Common\Activities\bin\Debug\Microsoft.Samples.SqlServer.Activities.dll

How to remove Microsoft.Samples.SqlServer.Activities from the GAC

From the Visual Studio Command Prompt, enter:

gacutil /u Microsoft.Samples.SqlServer.Activities

Note

If you get the Visual Studio error below, then you will need to install Microsoft.Samples.SqlServer.Activities into the GAC. A future release should resolve this issue.

Page 3: Introduction to QueryFeed - Microsoft Web viewRunning a workflow from within a Microsoft Excel 2010 and Microsoft Word 2010 Addin. ... From Microsoft SQL Server Management Studio,

Example QueryFeed workflowThe sample includes an example workflow that consumes the AdventureWorks CompanySales resource, filters company sales ordered by ProductSubCategory and Sales, and selects ProductSubCategory and Sales.

How to run the sampleTo run the sample, you will need to start the sample AdventureWorks OData feed. After the AdventureWorks OData service starts, you can run the ExcelAddin project or open Microsoft Excel 2010.

To run the service on a local ASP.NET Development Server

1) Attach the AdventureWorks2012 database. The AdventureWorks2012 database can be downloaded from http://msftdbprodsamples.codeplex.com/releases/view/93587

2) From Microsoft SQL Server Management Studio, run \Microsoft.Samples.SqlServer.OData\AdventureWorks.OData.Service\SQL Scripts\Views.sql to create the OData feed SQL views.

3) Open \Microsoft.Samples.SqlServer.OData\AdventureWorks.OData.Service\AdventureWorks.OData.Service.sln in Visual Studio 2012.

4) In Solution Explorer, select AdventureWorks.svc.5) Press F5 to run the service on http://localhost:1234/AdventureWorks.svc/.

To run the ExcelAddin project

1) Open \Microsoft.Samples.SqlServer.OData\Microsoft.Samples.SqlServer.Workflows.OData.sln in Visual Studio 2012.

2) Right click the Microsoft.Samples.SqlServer.ExcelAddIn project.3) Click Set as Startup Project.4) Press F5 to run the sample ExcelAddin.5) Click the Developer ribbon tab.

Page 4: Introduction to QueryFeed - Microsoft Web viewRunning a workflow from within a Microsoft Excel 2010 and Microsoft Word 2010 Addin. ... From Microsoft SQL Server Management Studio,

6) Click the Configuration button.7) Select the example QueryFeed workflow by clicking the (…) button next to the Workflow

property. Click the Close button.

NoteThe example QueryFeed workflow is located at \Microsoft.Samples.SqlServer.OData\Configuration\Activity Example.xaml

8) Select a cell.9) Click the Run ribbon button.

The example QueryFeed workflow renders entity properties as an Excel ListObject table.

Page 5: Introduction to QueryFeed - Microsoft Web viewRunning a workflow from within a Microsoft Excel 2010 and Microsoft Word 2010 Addin. ... From Microsoft SQL Server Management Studio,

Overview of the QueryFeed activityThe example QueryFeed Execute method constructs the following fully qualified OData query string and sets an EntityProperties OutArgument.

ServiceQueryString=http://localhost:1234/AdventureWorks.svc/CompanySales?$filter=OrderYear ne 2006 and Sales lt 10000&$top=25&$orderby=ProductSubCategory asc,Sales desc&$select=ProductSubCategory,Sales

For the complete QueryFeed sample activity, see the QueryFeed activity within the source code.

public sealed class QueryFeed : NativeActivity { public string Url { get; set; } public string Resource { get; set; }

//Filter public InArgument<int> Top { get; set; } public InArgument<int> Skip { get; set; } [Browsable(false)] public Collection<Activity> FilterActivities { get; set; }

public List<string> OrderBy { get; set; } public List<string> SelectProperties { get; set; }

Page 6: Introduction to QueryFeed - Microsoft Web viewRunning a workflow from within a Microsoft Excel 2010 and Microsoft Word 2010 Addin. ... From Microsoft SQL Server Management Studio,

public OutArgument<string> ServiceQueryString { get; set; } public OutArgument<IEnumerable<IEnumerable<EntityProperty>>> EntityProperties { get; set; }

protected override void Execute(NativeActivityContext context) { IEnumerable<IEnumerable<EntityProperty>> properties = null; XNamespace mxmlns = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"; string serviceQuery = this.QualifiedQueryString (this.Url.RemoveQuotes(), this.Resource.RemoveQuotes(), context.GetValue<int>(this.Top), context.GetValue<int>(this.Skip), this.SelectProperties, this.OrderBy);

try { properties = from item in XElement.Load(serviceQuery).Descendants(mxmlns.GetName("properties")) select from e in item.Descendants() select new EntityProperty { Name = e.Name.LocalName, Value = e.Value }; } catch { //Catch exception in a production application }

ServiceQueryString.Set(context, serviceQuery); EntityProperties.Set(context, properties); }

Page 7: Introduction to QueryFeed - Microsoft Web viewRunning a workflow from within a Microsoft Excel 2010 and Microsoft Word 2010 Addin. ... From Microsoft SQL Server Management Studio,

Using LINQ to build activity expression itemsThe sample activity uses LINQ to project OData metadata into activity designer expression items.

Resource

Query string part

/CompanySales

Page 8: Introduction to QueryFeed - Microsoft Web viewRunning a workflow from within a Microsoft Excel 2010 and Microsoft Word 2010 Addin. ... From Microsoft SQL Server Management Studio,

EntitySets LINQ for Resource Items

public IEnumerable<EntitySet> EntitySets{ get { //Arrange: A url to a WCF 5.0 service is given as InArgument_Url XNamespace xmlns = "http://schemas.microsoft.com/ado/2008/09/edm";

//Act: A LINQ to XML query is constructed that projects EntitySet XML to an EntitySet class IEnumerable<EntitySet> entitySets = from x in metadata .Descendants(xmlns.GetName("EntitySet")) select new EntitySet { Name = x.Attributes("Name").Single().Value, Namespace = x.Attributes("EntityType").Single().Value.Split(new char[] { '.' })[0], EntityType = x.Attributes("EntityType").Single().Value.Split(new char[] { '.' })[1] };

//Return: return entitySets; }}

Page 9: Introduction to QueryFeed - Microsoft Web viewRunning a workflow from within a Microsoft Excel 2010 and Microsoft Word 2010 Addin. ... From Microsoft SQL Server Management Studio,

Filter

Query string part

$filter=OrderYear ne 2006 and Sales lt 10000

EntitySetSchema LINQ for Filter Items

public IEnumerable<EntityPropertySchema> EntitySetSchema(string resource){ IEnumerable<EntityPropertySchema> entitySchema = null; if (resource != string.Empty) { XNamespace xmlns = "http://schemas.microsoft.com/ado/2008/09/edm"; entitySchema = from p in metadata .Descendants(xmlns.GetName("EntityType")).Descendants(xmlns.GetName("Property")) where p.Parent.Attribute("Name").Value == (from e in this.EntitySets where e.Name == resource select e.EntityType).Single<string>() select new EntityPropertySchema { Parent = p.Parent, Name = p.Attribute("Name").Value, Type = p.Attribute("Type").Value, MaxLength = p.Attribute("MaxLength") == null ? "NaN" : p.Attribute("MaxLength").Value

Page 10: Introduction to QueryFeed - Microsoft Web viewRunning a workflow from within a Microsoft Excel 2010 and Microsoft Word 2010 Addin. ... From Microsoft SQL Server Management Studio,

}; }

return entitySchema;}

Page 11: Introduction to QueryFeed - Microsoft Web viewRunning a workflow from within a Microsoft Excel 2010 and Microsoft Word 2010 Addin. ... From Microsoft SQL Server Management Studio,

OrderBy

Query string part

$orderby=ProductSubCategory asc,Sales desc

EntitySetSchema LINQ for Filter Items

See Filter

Page 12: Introduction to QueryFeed - Microsoft Web viewRunning a workflow from within a Microsoft Excel 2010 and Microsoft Word 2010 Addin. ... From Microsoft SQL Server Management Studio,

Select

Query string part

$select=ProductSubCategory,Sales

EntitySetSchema LINQ for Filter Items

See Filter

Rendering QueryFeed TablePartsThe example QueryFeed workflow renders entity properties in any client that supports .NET Framework 4.0. The sample source code at http://msftdbprodsamples.codeplex.com/releases/view/94486 shows rendering an EntityProperties TablePart in Microsoft Excel 2010 and Microsoft Word 2010. The sample source code shows how to use an extension method to extend a Microsoft Excel or Microsoft Word range.

Excel Example

Page 13: Introduction to QueryFeed - Microsoft Web viewRunning a workflow from within a Microsoft Excel 2010 and Microsoft Word 2010 Addin. ... From Microsoft SQL Server Management Studio,

OData Service

Excel Range Extension Method

public static void InsertEntityTable(this Range activeCell, IEnumerable<IEnumerable<EntityProperty>> entityProperties, string styleName) { int currentColumn = 0; int currentRow = 1; Range range;

List<string> propertyNames = (from item in entityProperties select item).First<IEnumerable<EntityProperty>>().Select(n => n.Name).ToList<string>(); int columnCount = propertyNames.Count(); int rowCount = entityProperties.Count();

Globals.ThisAddIn.Application.ScreenUpdating = false;

//Data Columns foreach (string name in propertyNames)

Page 14: Introduction to QueryFeed - Microsoft Web viewRunning a workflow from within a Microsoft Excel 2010 and Microsoft Word 2010 Addin. ... From Microsoft SQL Server Management Studio,

{ range = activeCell.get_Offset(1, currentColumn); range.FormulaR1C1 = name; currentColumn++; } currentColumn = 0;

//Data Values foreach (IEnumerable<EntityProperty> items in entityProperties) { //row = new TableRow(); currentRow++; foreach (EntityProperty item in items) { range = activeCell.get_Offset(currentRow, currentColumn); range.FormulaR1C1 = item.Value; currentColumn++; } currentColumn = 0; } Worksheet activeSheet = Globals.ThisAddIn.Application.ActiveSheet; Range styleRange = activeCell.Range[activeSheet.Cells[2, 1], activeSheet.Cells[rowCount + 2, columnCount]]; string listObjectName = String.Format("Table{0}", activeSheet.ListObjects.Count);

try { activeSheet.ListObjects.AddEx(XlListObjectSourceType.xlSrcRange, styleRange, Type.Missing, XlYesNoGuess.xlYes).Name = listObjectName; activeSheet.ListObjects[listObjectName].TableStyle = styleName; } catch { //Handle exception in a production application }

styleRange.Columns.AutoFit();

Globals.ThisAddIn.Application.ScreenUpdating = true; }

Page 15: Introduction to QueryFeed - Microsoft Web viewRunning a workflow from within a Microsoft Excel 2010 and Microsoft Word 2010 Addin. ... From Microsoft SQL Server Management Studio,

Word ExampleThe same QueryFeed workflow in Word.

Next PostThe next post will show how to edit a QueryFeed workflow using a hosted Windows Workflow designer. A custom designer is included in the source code at http://msftdbprodsamples.codeplex.com/releases/view/94486.