Fabian Williams SharePoint Blog

Solving problems with SharePoint day and night

Why it pays to have Smart Friends to Encourage you

Why write this blog post?

Sure its late and YES I could be rotting my brain away on the Idiot Box, but what started off as a Taunt by Ram turned out to consume my life for 2 days straight but with useful results, challenges that were overcome, and a better appreciation for not just Cloud, but the integration of Cloud and On-Prem using Windows Azure.  Let me tell you what I did, and how easy it was… in the end, plus the real implications of it.

First a little history – Pre Taunt

So, as I prepare for my sessions at #SPLive in Orlando in 3 weeks, I wanted to knock out a few Demos and make sure that I could showcase just about anything anyone asked of me based on the Session Abstract/Topic. In furtherance of that, I decided to do the following, build out solutions employing

SharePoint Designer 2013

  • External Content Type using a Native SQL Data Connection
  • External Content Type using an On-Prem (i.e. Hosted on one of my Dev Rig VMs) Windows Communications Foundation [WCF] Data Connection – JSON enabled
  • External Content Type using a .NET Assembly (built on my Visual Studio 2012, Deployed as a Farm Solution to my On-Prem SP2013) Data Connection
  • Workflow using the same On-Prem WCF above in my On-Prem Farm

Visual Studio 2012

  • A Locally IIS hosted WCF Service Library that exposes Northwind data from a Local SQL Server 2012 Box
  • A Locally IIS hosted OData Service Endpoint that exposes Northwind data from a Local SQL Server 2012 Box
  • External Content Type built as a .NET Assembly to be deployed to my ON-Prem SharePoint 2013 Farm
  • External Content Type AND External List SharePoint 2013 App built as an OData Sourced Application deployed to my ON-Prem SharePoint 2013 Farm

For every Instance of the WCF and the App Model External Content Type, I also targeted my Office 365 Tennant to surface the Data as well. I was pretty satisfied with myself until my good friend sent this tweet, and I know it was in jest…

image

but… I took it as a challenge and, I rarely back down from a challenge 🙂 so, since I have a MSDN subscription, there was “really” nothing holding me back besides my inexperience creating Azure Web Roles, which is what you need to do in order to have the requisite Data Access Points (URI) to create External Content Types and potentially Workflows. 

What Next you say…

So, with a little research on Google scratch that, I mean Bing, I found a few MSDN, and TechNet blogs on how to create Web Roles, although not many of them “accurately” showed you how to use Visual Studio to DEPLOY your solution back to Azure. Anyway, it was surprisingly easy, although initially it was somewhat intimidating and i did get snagged on a GOTCHA which i will blog about later when doing a ASP.NET Web Role where it requires you to lower your version of the System.Data.Client assembly from 5.0 to 4.something in order for it work in Azure, i say that because it worked perfectly when I debugged it locally on my Visual Studio IIS. I also had to create an Azure SQL Instance and used a script I downloaded from CodePlex to restore a Northwind Database to my Azure SQL Instance and then I set up firewall rules to allow me to access it over the internet.

But with quick turn around and because i could just Refactor my original On-Prem code logic, I was able to create the following

Visual Studio 2012

  • Azure ASP.NET Web Role that published as a Cloud Service which used OData via ADO.NET Entity Framework to get a Cloud based URI that i can do CRUD operations on
  • Azure WCF Web Role that published as a Cloud Service which used WCF via ADO.NET Entity Framework to get a Cloud based URI that i can do CRUD operations on as well

    image

Now I can really Mix and Match, I can

  1. Use Azure (Cloud) Hosted Data via an ON-Prem WCF or OData End Point and surface that information both on my ON-Prem SharePoint or my Office 365 Instance
  2. Use Azure (Cloud) Hosted Data via an Azure Cloud Service WCF or OData End Point and surface that information both on my ON-Prem SharePoint or my Office 365 Instance
  3. Plus everything I had when I started off

The takeaway here is that with very little investments in time and effort I extended my solution to a “MODERN” approach. I had Cloud in the sentence 🙂

 

Other Positive Implications

So, what made this so easy for me was I signed up for TFS Online here http://tfs.visualstudio.com/en-us/tfs-welcome.aspx and EVEN WITHOUT a MSDN Subscription you can sign up for FREE and get 5 accounts in one instance.

image

What I think is GOLD is…

  • You have a place to store all your CODE/ Work and before you ask, it supports different formats (see image below)
    image
  • Your Code/Work is accessible from Anywhere you have an Internet Connection now
  • You can reliably share your Code/Work with anyone now, rather than Zipping it up and Email it
  • Need Help with some of you work… Invite people as smart or smarter than you to review our code, many hands make light load 🙂
  • Most beneficial of all is that you can configure Continual Integration. I know my good friend Jeremy Thake always talk about Application Lifecycle Management (ALM) and you can employ some of those techniques from Azure through TFS Online and your Visual Studio with multiple Team Member… and guess what… FOR FREE
    image

So that’s all I have to say, I guess we really Paid It Forward RAM, you encouraged me to do this, I in turn, documented my efforts and hope that someone else can take it from here.

 

Cheers all, have a great night. Oh yeah, VS 2013 is out now. GO get it.

Advertisements

October 17, 2013 Posted by | Azure, JSON, OData, REST, SharePoint 2013, SharePoint 2013 Workflows, SharePoint Designer 2013, SharePoint Development, SQL Server, Visual Studio 2012, WCF | , , | Leave a comment

Part 2 of 3-Blog Update on SharePoint BCS with full CRUD

Consistently two of my most heavily viewed/visited post based on WordPress Metrics is the one highlighted below, see the image for the top 5 all

  1. How To: Create, Configure, Consume SharePoint 2010 Secure Store in Business Connectivity Services
  2. Creating a SharePoint 2010 External Content Type with CRUD Methods using LINQ and a SQL LOB System

image

Since I have already done this in SharePoint 2010 using Secure Store and a SQL Server Native Line of Business (LOB) System, as well as a Visual Studio 2010 .Net Assembly, I figured I can offer value by updating this post to include the following Scenarios

There are tons of examples (the #1 viewed blog above uses that as an example) on how to use SharePoint Designer to do a SQL Native Connection, so i wont bother beating that horse to death, and I would like to also point out to this excellent post by my good friend Chris Givens on how he extended the third bullet point example above to include Notification and Subscriptions Alert, so i will not be doing that either.

Approach

In this Part 2 of 3 Update I am targeting a second post cited above [Creating a SharePoint 2010 External Content Type with CRUD Methods using LINQ and a SQL LOB System]; again, I am specifically leaving out the precursor activities that are needed before you begin to create the External Content Type (ECT), I will just pick up at the point of Creating the ECT and as for cases where I introduce new methodologies, I will ofcourse showcase those aspects.

In this blog, specifically I will be showing the following:

  1. Point you to an old post by Scott Guthrie “The Gu” post on using Linq to SQL, I am going to skip that part in this post but demonstrate in my code how I use my DataContext that is created by the Linq to SQL addition to my Project Solution in Visual Studio 2012
  2. I will certainly show you how to Create your BDC Entity and the full CRUD StereoTyped Methods necessary to carry out those functions
  3. I will illustrate the chronological steps you NEED to take, otherwise you will spend Days Troubleshooting why your crap doesn’t work 🙂 and all the Gotchas that are hidden as well
  4. Deploy the Solution to the Farm & Create an External List from It

Lets Begin – Create a New Empty SharePoint 2013 Visual Studio Project

This is going to be a Farm based solution that will connect using “Linq to SQL” to our SQL Sever Database and create a DataContext class that we will use to communicate from our BCS entity to the underlying Database. 

image

this will be a Farm Solution

image

Once your project comes to life, you will need to establish a connection to your SQL Server or whatever Database you are using.  You do this by going to “Tools > Connect to Database > Fill out the Add Connection Fields similar to what i have below

image

and you should end up with a ORD Designer and in your “Server Explorer” you should expand that Database Connection that is now there and drag your Table/View etc to the Object Relational Designer (ORD). Now in my case I am using SQL Auth so I will get a warning, Im ok with that, this is Demoware

image

When I click YES, i get my Northwind Employee Entity in the context of the DataContext Class and my Project now looks like this below

image

Next we begin our work on the BCS piece. This does it for Data Access.

Create the BCS Data Model Entity

So, now you will need to add a new item to your project. you will add a BDCM as seen below Business Data Connectivity Model. Now, if you have read my part 1 you will know that I vehemently hate when Visual Studio gives me helper/sample implementations, well, this happens here after you name and add your item to your project.

image

as you can see below, you get a Entity1.cs and an Entity1Servcie.cs which ties to the GUI entity you see in the designer. We will get rid of these as we did previously in out WCF part 1 blog post and create our own entity based on the NorthWindEntity we got with the Linq To SQL DataContext we created earlier. so for now just select the two files and delete them, alternatively, you can delete the item out of the designer and “I think” it deletes the file along with it, perhaps it leaves the service file, but long and short, kill em all.

image

What you will after you delete those is, from the toolbar, drag a new entity onto the design surface like so and rename the entity in the Properties Window to something like “Employee” you will also notice that it creates a Serivce class for you as well, this one is called [EntityName]Service.cs

image

Next you will add an Identifier, by right clicking on the New Employee Identity, click Add, then click Identifier. When the Identifier appears, in the Properties Window again, change the name to EmployeeID and set the Type Name  to System.Int32

GOTCHA ALERT

The next piece is Arguably the place MOST people will Run-A-Muck because they will either FORGET to change the TypeName of the Specific Finder to reflect the Employee Class created by the Data Context and leave it as the ‘Generic” that it is originally set to, or they will fail to set the Identifier property in this method. The reason folks mess this up is because THIS IS THE ONLY METHOD THAT REQUIRES THIS, every method thereafter inherits from this. The next things folks mess up on is the Update Method but I will go into that in detail in a few.

END GOTCHA ALERT

Add a Specific Finder Method (Read Item)

  1. Click on the Employee entity in the BDC Designer
  2. You should see in the pane at the bottom of the Visual Studio IDE a window called “BDC Method Details”, inside there click
  3. Add a Method from the dropdown list and select “Create a Specific Finder Method”
  4. A few things will be added for you by default but what I want you to concern yourself with is under “Type Descriptor” heading in the same window you will see “Employee”, click on that then click “Edit”

This should open up the BDC Explorer and a Hierarchical View should appear of the Model. The properties window should also be open as well,

  1. Locate TypeName in the Properties Window, click on it, then click “CURRENT PROJECT” tab, then select Employee which should be under the DataContext Class (it should only be 1 in there anyway)
  2. Inside the BDC Explorer, right click on Employee, and click “Add Type Descriptor” – Later on you will repeat this for every Field in the Entity(Data Store) that you want to surface paying attention to the data type of the field
  3. When the new TypeDescriptor is created, in the Properties window change the name to EmployeeID and set the TypeName to Int32, also
  4. Click the dropdown list next to Identifier and select EmployeeID

You will then repeat Step 2 for all the Fields in your Entity, refer back to my Image above that has the Employee Entity, remember to set the TypeName to the correct Field Type (Int32, String, DateTime, etc). Once that is complete, you can either (1) Double Click or (2) Right Click and select ‘View Code’’ on the ReadItem Method that is in the Design Area. This will take you inside EmployeeService.cs or whatever name gave your entity appended with Service.cs. It will be subbed out but I want you to replace what is there with

  1. public static Employee ReadItem(int employeeID)
  2.         {
  3.             NorthWindDataContext dataContext = new NorthWindDataContext
  4.             ("Data Source=Farm1Server1ADSQL;Initial Catalog=Northwind;uid=BCSUser1;pwd=P@ssword1");
  5.  
  6.             Employee Employee =
  7.             (from employees in dataContext.Employees.AsEnumerable().Take(20)
  8.              where employees.EmployeeID == employeeID
  9.              select employees).Single();
  10.             return Employee;
  11.         }

 

Add a Finder Method (Read List)

  1. Click on the Employee entity in the BDC Designer
  2. You should see in the pane at the bottom of the Visual Studio IDE a window called “BDC Method Details”, inside there click
  3. Add a Method from the dropdown list and select “Create Finder Method”

Once that is complete, you can either (1) Double Click or (2) Right Click and select ‘View Code’’ on the ReadList Method that is in the Design Area. This will take you inside EmployeeService.cs or whatever name gave your entity appended with Service.cs. It will be subbed out but I want you to replace what is there with

  1. public static IEnumerable<Employee> ReadList()
  2.       {
  3.           NorthWindDataContext dataContext = new NorthWindDataContext
  4.           ("Data Source=Farm1Server1ADSQL;Initial Catalog=Northwind;uid=BCSUser1;pwd=P@ssword1");
  5.  
  6.  
  7.           IEnumerable<Employee> Employees =
  8.               from employees in dataContext.Employees
  9.               select employees;
  10.           return Employees;
  11.       }

 

Add a Creator Method

  1. Click on the Employee entity in the BDC Designer
  2. You should see in the pane at the bottom of the Visual Studio IDE a window called “BDC Method Details”, inside there click
  3. Add a Method from the dropdown list and select “Create Creator Method”

Once that is complete, you can either (1) Double Click or (2) Right Click and select ‘View Code’’ on the Create Method that is in the Design Area. This will take you inside EmployeeService.cs or whatever name gave your entity appended with Service.cs. It will be subbed out but I want you to replace what is there with

  1. public static Employee Create(Employee newEmployee)
  2.   {
  3.       NorthWindDataContext dataContext = new NorthWindDataContext
  4.       ("Data Source=Farm1Server1ADSQL;Initial Catalog=Northwind;uid=BCSUser1;pwd=P@ssword1");
  5.  
  6.  
  7.       Employee emp = new Employee();
  8.  
  9.       emp.FirstName = newEmployee.FirstName;
  10.       emp.LastName = newEmployee.LastName;
  11.       emp.Title = newEmployee.Title;
  12.       emp.TitleOfCourtesy = newEmployee.TitleOfCourtesy;
  13.       emp.BirthDate = newEmployee.BirthDate;
  14.       emp.HireDate = newEmployee.HireDate;
  15.       emp.Address = newEmployee.Address;
  16.       emp.City = newEmployee.City;
  17.       emp.Region = newEmployee.Region;
  18.       emp.PostalCode = newEmployee.PostalCode;
  19.       emp.Country = newEmployee.Country;
  20.       emp.HomePhone = newEmployee.HomePhone;
  21.       emp.Extension = newEmployee.Extension;
  22.       emp.Notes = newEmployee.Notes;
  23.  
  24.       dataContext.Employees.InsertOnSubmit(emp);
  25.       dataContext.SubmitChanges();
  26.       return emp;
  27.   }

 

Add a Updater Method

  1. Click on the Employee entity in the BDC Designer
  2. You should see in the pane at the bottom of the Visual Studio IDE a window called “BDC Method Details”, inside there click
  3. Add a Method from the dropdown list and select “Create Updater Method”

GOTCHA ALERT

Now based on YOUR particular Data Source/Store you may have a Primary Key that either (1) AutoUpdates or (2) Doesnt – If it DOES NOT Auto Update you MUST click on the Employee Type Descriptor in the BDC Method Details Window and in the Properties Window you MUST set he “Pre-Updater Field” to True

BUT!!!!

If YOUR Primary Key actually DOES Auto Increment, then what “I” have found to work for me is to Add another Type Descriptor to the Updater Method (see Image below), name it appropriately and SET its Pre-Updater property to TRUE

image

This means that the Updater Method will take in two Parameters now, not one as most MSDN and TechNet articles will say, but I am only using my EmployeeID Input Parameter to locate the Specific Item needing to update and the employee parameter to set the Fields of what will be committed back to the Data Source. All attempts to do it otherwise GAVE AN ERROR about the EmployeeID being a Read Only Field and needed the PreUpdate Field set to True when using an External List to Update an Item.

END GOTCHA ALERT

Once that is complete, you can either (1) Double Click or (2) Right Click and select ‘View Code’’ on the Update Method that is in the Design Area. This will take you inside EmployeeService.cs or whatever name gave your entity appended with Service.cs. It will be subbed out but I want you to replace what is there with

  1. public static void Update(Employee employee, int parameter)
  2.     {
  3.         NorthWindDataContext dataContext = new NorthWindDataContext
  4.         ("Data Source=Farm1Server1ADSQL;Initial Catalog=Northwind;uid=BCSUser1;pwd=P@ssword1");
  5.  
  6.         var employeeToUpdate = (from employees in dataContext.Employees
  7.                                where employees.EmployeeID == parameter
  8.                                select employees).Single();
  9.  
  10.         employeeToUpdate.FirstName = employee.FirstName;
  11.         employeeToUpdate.LastName = employee.LastName;
  12.         employeeToUpdate.Title = employee.Title;
  13.         employeeToUpdate.TitleOfCourtesy = employee.TitleOfCourtesy;
  14.         employeeToUpdate.BirthDate = employee.BirthDate;
  15.         employeeToUpdate.HireDate = employee.HireDate;
  16.         employeeToUpdate.Address = employee.Address;
  17.         employeeToUpdate.City = employee.City;
  18.         employeeToUpdate.Region = employee.Region;
  19.         employeeToUpdate.PostalCode = employee.PostalCode;
  20.         employeeToUpdate.Country = employee.Country;
  21.         employeeToUpdate.HomePhone = employee.HomePhone;
  22.         employeeToUpdate.Extension = employee.Extension;
  23.         employeeToUpdate.Notes = employee.Notes;
  24.         dataContext.SubmitChanges();
  25.     }

 

Add a Deleter Method

  1. Click on the Employee entity in the BDC Designer
  2. You should see in the pane at the bottom of the Visual Studio IDE a window called “BDC Method Details”, inside there click
  3. Add a Method from the dropdown list and select “Create a Deleter Method”

Once that is complete, you can either (1) Double Click or (2) Right Click and select ‘View Code’’ on the Delete Method that is in the Design Area. This will take you inside EmployeeService.cs or whatever name gave your entity appended with Service.cs. It will be subbed out but I want you to replace what is there with

 

  1.  
  2. public static void Delete(int employeeID)
  3. {
  4.     NorthWindDataContext dataContext = new NorthWindDataContext
  5.     ("Data Source=Farm1Server1ADSQL;Initial Catalog=Northwind;uid=BCSUser1;pwd=P@ssword1");
  6.  
  7.     Employee Employee =
  8.     (from employees in dataContext.Employees.AsEnumerable().Take(20)
  9.      where employees.EmployeeID == employeeID
  10.      select employees).Single();
  11.  
  12.  
  13.     dataContext.Employees.DeleteOnSubmit(Employee);
  14.     dataContext.SubmitChanges();
  15.  
  16. }

 

All in all the full code should look like below when you put it all together.

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using NorthWindEmployees;
  6.  
  7. namespace NorthWindEmployees.NWindFarmECTEmployees
  8. {
  9.     public partial class EmployeeService
  10.     {
  11.         public static Employee ReadItem(int employeeID)
  12.         {
  13.             NorthWindDataContext dataContext = new NorthWindDataContext
  14.             ("Data Source=Farm1Server1ADSQL;Initial Catalog=Northwind;uid=BCSUser1;pwd=P@ssword1");
  15.  
  16.             Employee Employee =
  17.             (from employees in dataContext.Employees.AsEnumerable().Take(20)
  18.              where employees.EmployeeID == employeeID
  19.              select employees).Single();
  20.             return Employee;
  21.         }
  22.  
  23.         public static IEnumerable<Employee> ReadList()
  24.         {
  25.             NorthWindDataContext dataContext = new NorthWindDataContext
  26.             ("Data Source=Farm1Server1ADSQL;Initial Catalog=Northwind;uid=BCSUser1;pwd=P@ssword1");
  27.  
  28.  
  29.             IEnumerable<Employee> Employees =
  30.                 from employees in dataContext.Employees
  31.                 select employees;
  32.             return Employees;
  33.         }
  34.  
  35.         public static Employee Create(Employee newEmployee)
  36.         {
  37.             NorthWindDataContext dataContext = new NorthWindDataContext
  38.             ("Data Source=Farm1Server1ADSQL;Initial Catalog=Northwind;uid=BCSUser1;pwd=P@ssword1");
  39.  
  40.  
  41.             Employee emp = new Employee();
  42.  
  43.             emp.FirstName = newEmployee.FirstName;
  44.             emp.LastName = newEmployee.LastName;
  45.             emp.Title = newEmployee.Title;
  46.             emp.TitleOfCourtesy = newEmployee.TitleOfCourtesy;
  47.             emp.BirthDate = newEmployee.BirthDate;
  48.             emp.HireDate = newEmployee.HireDate;
  49.             emp.Address = newEmployee.Address;
  50.             emp.City = newEmployee.City;
  51.             emp.Region = newEmployee.Region;
  52.             emp.PostalCode = newEmployee.PostalCode;
  53.             emp.Country = newEmployee.Country;
  54.             emp.HomePhone = newEmployee.HomePhone;
  55.             emp.Extension = newEmployee.Extension;
  56.             emp.Notes = newEmployee.Notes;
  57.  
  58.             dataContext.Employees.InsertOnSubmit(emp);
  59.             dataContext.SubmitChanges();
  60.             return emp;
  61.         }
  62.  
  63.         public static void Update(Employee employee, int parameter)
  64.         {
  65.             NorthWindDataContext dataContext = new NorthWindDataContext
  66.             ("Data Source=Farm1Server1ADSQL;Initial Catalog=Northwind;uid=BCSUser1;pwd=P@ssword1");
  67.  
  68.             var employeeToUpdate = (from employees in dataContext.Employees
  69.                                    where employees.EmployeeID == parameter
  70.                                    select employees).Single();
  71.  
  72.             employeeToUpdate.FirstName = employee.FirstName;
  73.             employeeToUpdate.LastName = employee.LastName;
  74.             employeeToUpdate.Title = employee.Title;
  75.             employeeToUpdate.TitleOfCourtesy = employee.TitleOfCourtesy;
  76.             employeeToUpdate.BirthDate = employee.BirthDate;
  77.             employeeToUpdate.HireDate = employee.HireDate;
  78.             employeeToUpdate.Address = employee.Address;
  79.             employeeToUpdate.City = employee.City;
  80.             employeeToUpdate.Region = employee.Region;
  81.             employeeToUpdate.PostalCode = employee.PostalCode;
  82.             employeeToUpdate.Country = employee.Country;
  83.             employeeToUpdate.HomePhone = employee.HomePhone;
  84.             employeeToUpdate.Extension = employee.Extension;
  85.             employeeToUpdate.Notes = employee.Notes;
  86.             dataContext.SubmitChanges();
  87.         }
  88.  
  89.         public static void Delete(int employeeID)
  90.         {
  91.             NorthWindDataContext dataContext = new NorthWindDataContext
  92.             ("Data Source=Farm1Server1ADSQL;Initial Catalog=Northwind;uid=BCSUser1;pwd=P@ssword1");
  93.  
  94.             Employee Employee =
  95.             (from employees in dataContext.Employees.AsEnumerable().Take(20)
  96.              where employees.EmployeeID == employeeID
  97.              select employees).Single();
  98.  
  99.  
  100.             dataContext.Employees.DeleteOnSubmit(Employee);
  101.             dataContext.SubmitChanges();
  102.  
  103.         }
  104.     }
  105. }

 

In a nutshell you are done, and you can just “F5” to debug and test it, or just “Deploy” from here, BUT!!! you can also copy that BDCM file out. Its what was previously known as your Application Definition File or now your Model File and you can Import that INTO

  • SharePoint Designer or
  • Central Administration

Why would you do that? Maybe your SDLC calls for separation, and your coders need to and off here. This is just an XML file, i have a snippet below

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Model xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/windows/2007/BusinessDataCatalog" Name="NWindFarmECTEmployees">
  3.   <LobSystems>
  4.     <LobSystem Name="NWindFarmECTEmployees" Type="DotNetAssembly">
  5.       <LobSystemInstances>
  6.         <LobSystemInstance Name="NWindFarmECTEmployees" />
  7.       </LobSystemInstances>
  8.       <Entities>
  9.         <Entity Name="Employee" Namespace="NorthWindEmployees.NWindFarmECTEmployees" Version="1.0.0.71">
  10.           <Properties>
  11.             <Property Name="Class" Type="System.String">NorthWindEmployees.NWindFarmECTEmployees.EmployeeService, NWindFarmECTEmployees</Property>
  12.           </Properties>
  13.           <Identifiers>
  14.             <Identifier Name="EmployeeID" TypeName="System.Int32" />
  15.           </Identifiers>
  16.           <Methods>
  17.             <Method Name="ReadItem">
  18.               <Parameters>
  19.                 <Parameter Name="employee" Direction="Return">
  20.                   <TypeDescriptor Name="Employee" TypeName="NorthWindEmployees.Employee, NWindFarmECTEmployees" IsCollection="false" PreUpdaterField="false">
  21.                     <TypeDescriptors>
  22.                       <TypeDescriptor Name="EmployeeID" TypeName="System.Int32" IsCollection="false" IdentifierName="EmployeeID" />
  23.                       <TypeDescriptor Name="LastName" TypeName="System.String" />
  24.                       <TypeDescriptor Name="FirstName" TypeName="System.String" />
  25.                       <TypeDescriptor Name="Title" TypeName="System.String" />
  26.                       <TypeDescriptor Name="TitleOfCourtesy" TypeName="System.String" />
  27.                       <TypeDescriptor Name="BirthDate" TypeName="System.DateTime" IsCollection="false" />
  28.                       <TypeDescriptor Name="HireDate" TypeName="System.DateTime" IsCollection="false" />
  29.                       <TypeDescriptor Name="Address" TypeName="System.String" />
  30.                       <TypeDescriptor Name="City" TypeName="System.String" />
  31.                       <TypeDescriptor Name="Region" TypeName="System.String" />
  32.                       <TypeDescriptor Name="PostalCode" TypeName="System.String" />
  33.                       <TypeDescriptor Name="Country" TypeName="System.String" />
  34.                       <TypeDescriptor Name="HomePhone" TypeName="System.String" />
  35.                       <TypeDescriptor Name="Extension" TypeName="System.String" />
  36.                       <TypeDescriptor Name="Notes" TypeName="System.String" /></TypeDescriptors></TypeDescriptor></Parameter>
  37.                 <Parameter Name="employeeID" Direction="In">
  38.                   <TypeDescriptor Name="EmployeeID" TypeName="System.Int32" IdentifierEntityName="Employee" IdentifierEntityNamespace="NorthWindEmployees.NWindFarmECTEmployees" IdentifierName="EmployeeID" PreUpdaterField="false" /></Parameter>
  39.               </Parameters>
  40.               <MethodInstances>
  41.                 <MethodInstance Name="ReadItem" Type="SpecificFinder" ReturnParameterName="employee" ReturnTypeDescriptorPath="Employee" />
  42.               </MethodInstances></Method>
  43.             <Method Name="ReadList">
  44.               <Parameters>
  45.                 <Parameter Name="employeeList" Direction="Return">
  46.                   <TypeDescriptor Name="EmployeeList" TypeName="System.Collections.Generic.IEnumerable`1[[NorthWindEmployees.Employee, NWindFarmECTEmployees]]" IsCollection="true">
  47.                     <TypeDescriptors>
  48.                       <TypeDescriptor Name="Employee" IsCollection="false" TypeName="NorthWindEmployees.Employee, NWindFarmECTEmployees">
  49.                         <TypeDescriptors>
  50.                           <TypeDescriptor Name="EmployeeID" IdentifierName="EmployeeID" IsCollection="false" TypeName="System.Int32" />
  51.                           <TypeDescriptor Name="LastName" TypeName="System.String" />
  52.                           <TypeDescriptor Name="FirstName" TypeName="System.String" />
  53.                           <TypeDescriptor Name="Title" TypeName="System.String" />
  54.                           <TypeDescriptor Name="TitleOfCourtesy" TypeName="System.String" />
  55.                           <TypeDescriptor Name="BirthDate" IsCollection="false" TypeName="System.DateTime" />
  56.                           <TypeDescriptor Name="HireDate" IsCollection="false" TypeName="System.DateTime" />
  57.                           <TypeDescriptor Name="Address" TypeName="System.String" />
  58.                           <TypeDescriptor Name="City" TypeName="System.String" />
  59.                           <TypeDescriptor Name="Region" TypeName="System.String" />
  60.                           <TypeDescriptor Name="PostalCode" TypeName="System.String" />
  61.                           <TypeDescriptor Name="Country" TypeName="System.String" />
  62.                           <TypeDescriptor Name="HomePhone" TypeName="System.String" />
  63.                           <TypeDescriptor Name="Extension" TypeName="System.String" />
  64.                           <TypeDescriptor Name="Notes" TypeName="System.String" /></TypeDescriptors></TypeDescriptor></TypeDescriptors></TypeDescriptor></Parameter>
  65.               </Parameters>
  66.               <MethodInstances>
  67.                 <MethodInstance Name="ReadList" Type="Finder" ReturnParameterName="employeeList" ReturnTypeDescriptorPath="EmployeeList" />
  68.               </MethodInstances></Method>
  69.             <Method Name="Create">
  70.               <Parameters>
  71.                 <Parameter Name="returnEmployee" Direction="Return">
  72.                   <TypeDescriptor Name="ReturnEmployee" IsCollection="false" TypeName="NorthWindEmployees.Employee, NWindFarmECTEmployees">
  73.                     <TypeDescriptors>
  74.                       <TypeDescriptor Name="EmployeeID" IdentifierName="EmployeeID" IsCollection="false" TypeName="System.Int32" />
  75.                       <TypeDescriptor Name="LastName" TypeName="System.String" />
  76.                       <TypeDescriptor Name="FirstName" TypeName="System.String" />
  77.                       <TypeDescriptor Name="Title" TypeName="System.String" />
  78.                       <TypeDescriptor Name="TitleOfCourtesy" TypeName="System.String" />
  79.                       <TypeDescriptor Name="BirthDate" IsCollection="false" TypeName="System.DateTime" />
  80.                       <TypeDescriptor Name="HireDate" IsCollection="false" TypeName="System.DateTime" />
  81.                       <TypeDescriptor Name="Address" TypeName="System.String" />
  82.                       <TypeDescriptor Name="City" TypeName="System.String" />
  83.                       <TypeDescriptor Name="Region" TypeName="System.String" />
  84.                       <TypeDescriptor Name="PostalCode" TypeName="System.String" />
  85.                       <TypeDescriptor Name="Country" TypeName="System.String" />
  86.                       <TypeDescriptor Name="HomePhone" TypeName="System.String" />
  87.                       <TypeDescriptor Name="Extension" TypeName="System.String" />
  88.                       <TypeDescriptor Name="Notes" TypeName="System.String" /></TypeDescriptors></TypeDescriptor></Parameter>
  89.                 <Parameter Name="newEmployee" Direction="In">
  90.                   <TypeDescriptor Name="NewEmployee" IsCollection="false" TypeName="NorthWindEmployees.Employee, NWindFarmECTEmployees">
  91.                     <TypeDescriptors>
  92.                       <TypeDescriptor Name="EmployeeID" IdentifierName="EmployeeID" IsCollection="false" TypeName="System.Int32" CreatorField="true" />
  93.                       <TypeDescriptor Name="LastName" TypeName="System.String" CreatorField="true" />
  94.                       <TypeDescriptor Name="FirstName" TypeName="System.String" CreatorField="true" />
  95.                       <TypeDescriptor Name="Title" TypeName="System.String" CreatorField="true" />
  96.                       <TypeDescriptor Name="TitleOfCourtesy" TypeName="System.String" CreatorField="true" />
  97.                       <TypeDescriptor Name="BirthDate" IsCollection="false" TypeName="System.DateTime" CreatorField="true" />
  98.                       <TypeDescriptor Name="HireDate" IsCollection="false" TypeName="System.DateTime" CreatorField="true" />
  99.                       <TypeDescriptor Name="Address" TypeName="System.String" CreatorField="true" />
  100.                       <TypeDescriptor Name="City" TypeName="System.String" CreatorField="true" />
  101.                       <TypeDescriptor Name="Region" TypeName="System.String" CreatorField="true" />
  102.                       <TypeDescriptor Name="PostalCode" TypeName="System.String" CreatorField="true" />
  103.                       <TypeDescriptor Name="Country" TypeName="System.String" CreatorField="true" />
  104.                       <TypeDescriptor Name="HomePhone" TypeName="System.String" CreatorField="true" />
  105.                       <TypeDescriptor Name="Extension" TypeName="System.String" CreatorField="true" />
  106.                       <TypeDescriptor Name="Notes" TypeName="System.String" CreatorField="true" /></TypeDescriptors></TypeDescriptor></Parameter>
  107.               </Parameters>
  108.               <MethodInstances>
  109.                 <MethodInstance Name="Create" Type="Creator" ReturnParameterName="returnEmployee" ReturnTypeDescriptorPath="ReturnEmployee" />
  110.               </MethodInstances></Method>
  111.             <Method Name="Update">
  112.               <Parameters>
  113.                 <Parameter Name="employee" Direction="In">
  114.                   <TypeDescriptor Name="Employee" IsCollection="false" TypeName="NorthWindEmployees.Employee, NWindFarmECTEmployees">
  115.                     <TypeDescriptors>
  116.                       <TypeDescriptor Name="EmployeeID" IdentifierName="EmployeeID" IsCollection="false" TypeName="System.Int32" UpdaterField="true" />
  117.                       <TypeDescriptor Name="LastName" TypeName="System.String" UpdaterField="true" />
  118.                       <TypeDescriptor Name="FirstName" TypeName="System.String" UpdaterField="true" />
  119.                       <TypeDescriptor Name="Title" TypeName="System.String" UpdaterField="true" />
  120.                       <TypeDescriptor Name="TitleOfCourtesy" TypeName="System.String" UpdaterField="true" />
  121.                       <TypeDescriptor Name="BirthDate" IsCollection="false" TypeName="System.DateTime" UpdaterField="true" />
  122.                       <TypeDescriptor Name="HireDate" IsCollection="false" TypeName="System.DateTime" UpdaterField="true" />
  123.                       <TypeDescriptor Name="Address" TypeName="System.String" UpdaterField="true" />
  124.                       <TypeDescriptor Name="City" TypeName="System.String" UpdaterField="true" />
  125.                       <TypeDescriptor Name="Region" TypeName="System.String" UpdaterField="true" />
  126.                       <TypeDescriptor Name="PostalCode" TypeName="System.String" UpdaterField="true" />
  127.                       <TypeDescriptor Name="Country" TypeName="System.String" UpdaterField="true" />
  128.                       <TypeDescriptor Name="HomePhone" TypeName="System.String" UpdaterField="true" />
  129.                       <TypeDescriptor Name="Extension" TypeName="System.String" UpdaterField="true" />
  130.                       <TypeDescriptor Name="Notes" TypeName="System.String" UpdaterField="true" /></TypeDescriptors></TypeDescriptor></Parameter>
  131.                 <Parameter Name="parameter" Direction="In">
  132.                   <TypeDescriptor Name="EmployeeID" TypeName="System.Int32" IsCollection="false" IdentifierName="EmployeeID" PreUpdaterField="true" /></Parameter>
  133.               </Parameters>
  134.               <MethodInstances>
  135.                 <MethodInstance Name="Update" Type="Updater" />
  136.               </MethodInstances></Method>
  137.             <Method Name="Delete">
  138.               <Parameters>
  139.                 <Parameter Name="employeeID" Direction="In">
  140.                   <TypeDescriptor Name="EmployeeID" TypeName="System.Int32" IdentifierEntityName="Employee" IdentifierEntityNamespace="NorthWindEmployees.NWindFarmECTEmployees" IdentifierName="EmployeeID" /></Parameter>
  141.               </Parameters>
  142.               <MethodInstances>
  143.                 <MethodInstance Name="Delete" Type="Deleter" />
  144.               </MethodInstances></Method>
  145.           </Methods></Entity>
  146.       </Entities>
  147.     </LobSystem>
  148.   </LobSystems>
  149. </Model>

 

For US howevever we will just Deploy it. What we expect to see then in Central Admin under the BDC Service Application is the following

  • A brand new External Content Type
  • A new Model and
  • A new External System

image

As in my original post, make sure you also do the following as it relates to the ECT

  1. Set the Metadata Store Permissions
  2. Set at a Minimum the “Execute” Permissions on the ECT so that people can use it. You may consider giving the Search Account permissions if you intend to use this ECT as a Content Source in Search

The Finale

Now we create our External List and we should have FULL CRUD Capabilities. Here you can see the Methods exposed though their actions. To see and learn more come see my VS Live 360 Session that i spoke about on my blog here.

image

October 14, 2013 Posted by | Business Connectivity Services, Secure Store, SharePoint 2013, SharePoint Development, SharePoint How-To, Visual Studio 2012 | , , , , | 2 Comments

Blog Update on SharePoint BCS with full CRUD Part 1 of 3

Précis

Consistently two of my most heavily viewed/visited post based on WordPress Metrics is the one highlighted below, see the image for the top 5 all

  1. How To: Create, Configure, Consume SharePoint 2010 Secure Store in Business Connectivity Services
  2. Creating a SharePoint 2010 External Content Type with CRUD Methods using LINQ and a SQL LOB System

image

Since I have already done this in SharePoint 2010 using Secure Store and a SQL Server Native Line of Business (LOB) System, as well as a Visual Studio 2010 .Net Assembly, I figured I can offer value by updating this post to include the following Scenarios

  • BCS Full CRUD using SharePoint Designer with a Windows Communications Foundation (WCF) Data Connection [I will actually show you how to build the WCF, otherwise why even bother :-)] using the Users Identity as the Auth (YOU ARE HERE!)
  • BCS Full CRUD with Visual Studio 2012 .Net Assembly using Secure Store [I will put in Code Snippets this time rather than Images :-)]
  • BCS Full CRUD with Visual Studio 2012 OData LOB System Type SharePoint App [Yes App Model] to include the External List Instantiation

There are tons of examples (the #1 viewed blog above uses that as an example) on how to use SharePoint Designer to do a SQL Native Connection, so i wont bother beating that horse to death, and I would like to also point out to this excellent post by my good friend Chris Givens on how he extended the third bullet point example above to include Notification and Subscriptions Alert, so i will not be doing that either.

Approach

As this is an update, I am specifically leaving out the precursor activities that are needed before you begin to create the External Content Type (ECT), I will just pick up at the point of Creating the ECT and as for cases where I introduce new methodologies, I will ofcourse showcase those aspects.

In this blog, specifically I will be showing the following

  1. Creating a WCF Source
    1. Basically, creating the WCF Service Library
    2. Moving the Service App DLL & Hosting the WCF in IIS (since automatically it is hosted in the local virtual Visual Studio IIS app)
    3. Using SharePoint Designer 2013 and that WCF Data Connection to do Full CRUD Operations, CRUD-Q infact

Lets Begin – Creating the WCF Service Library

My thought process for this since I am going to be exposing data in an underlying SQL Server is to use Visual Studio Entity Framework 5 to create my access to the Data Store. I will then (I don’t have to but because I want to extrapolate the Entity and manipulate it as my own object) I will create my own class(object) to represent the Northwind Employee and then create my Visual Studio Interface and then my Service Contract.  If all of this sounds like gibberish to you, here is a beginners tutorial on how to create a WCF Service Library (its too involved for this post and will take away from my objective) NOW… I am not going to implement it totally the way the post describes it, as i said above, but I WILL show you the code of my

  • Connecting to the Northwind Database
  • My Northwind Employee Entity (Data Contract)
  • Service Contract (Interface)
  • Service Itself(Implementation of the Interface)

New Project & Connecting to the Database

First you will start Visual Studio as Administrator and create a new Project as such

image

Project > New Project > Dub CF > WCF Service Library – Then name it appropriately

Now by default you will get a Service Contract called (IService1.cs) and a Service Implementation (SErvice1.cs) – I don’t like using the OOB templates that are provided although hey say you can Rename/Refactor it; i just never trust it. So, you can go ahead and delete those, because we will create our own classes for our Data Contract, Service Contract and Service Implementation.

image

Once you have deleted those two files, then next we will do is add an ADO.NET Entity Data Model item to our project, name it appropriately for the Data you are modeling after and click Add.

image

Follow the Wizard by doing “Generate from Database”, make a new connection or select from one already there, because I am using SQL Server Authentication and I want to have this persisted in my project I also select the option for “Yes, include the sensitive data in the connection string” which is basically your Uname and PW along with Data Source and Initial Catalogue. Under Tables I am selecting my Northwind Employee Table, and following the rest of the wizard to the end. When you are done, you should have an entity as i do below as well as a “NameYouChose”.edmx file which shows you the below

image

Once you have done that, now I am actually going to create my own Data Contact class which abstracts from the ADO.NET Employee entity.  You do that by adding a new class to your project. I am calling mine “Emp.cs” as seen below in the code snippet

  1.  
  2. namespace NorthwindServiceLib
  3. {
  4.     [DataContract]
  5.     public class Emp
  6.     {
  7.         [DataMember]
  8.         public Int32 EmployeeID;
  9.         [DataMember]
  10.         public string LastName;
  11.         [DataMember]
  12.         public string FirstName;
  13.         [DataMember]
  14.         public string Title;
  15.         [DataMember]
  16.         public string TitleOfCourtesy;
  17.         [DataMember]
  18.         public DateTime BirthDate;
  19.         [DataMember]
  20.         public DateTime HireDate;
  21.         [DataMember]
  22.         public string Address;
  23.         [DataMember]
  24.         public string City;
  25.         [DataMember]
  26.         public string Region;
  27.         [DataMember]
  28.         public string PostalCode;
  29.         [DataMember]
  30.         public string Country;
  31.         [DataMember]
  32.         public string HomePhone;
  33.         [DataMember]
  34.         public string Extension;
  35.         [DataMember]
  36.         public string Notes;
  37.         [DataMember]
  38.         public string ReportsTo;
  39.         [DataMember]
  40.         public string PhotoPath;
  41.  
  42.     }
  43. }

You will notice that i am decorating the class with the [DataContract] attribute and also setting the fields that i want as [DataMember] then next thing I do is create my Service Contract which as I said before is a Interface; I do that by adding another class called IEmpService.cs as seen in the code snippet below

  1.  
  2. namespace NorthwindServiceLib
  3. {
  4.     [ServiceContract]
  5.     public interface IEmpService
  6.     {
  7.         [OperationContract]
  8.         void CreateEmployee(Emp emp);
  9.         [OperationContract]
  10.         List<Emp> GetAllEmp();
  11.         [OperationContract]
  12.         List<Emp> GetSpecificEmp(Int32 empId);
  13.         [OperationContract]
  14.         void RemoveEmp(Int32 empId);
  15.         [OperationContract]
  16.         void UpdateEmp(Emp emp);
  17.     }
  18. }

Here we decorate the interface with the attribute [ServiceContract] and this is the fist part where we have an eye towards what SharePoint BCS needs. As you can see above we have a the following

  • Create Method called “CreateEmployee” which takes the data contract as a input parameter
  • Read List Method called “GetAllEmp” which returns a list of all the Employees
  • Read Item (Specific Finder) Method called “GetSpecificEmp” which takes the employee Id as an input parameter
  • Delete Method called “RemoveEmp” which also takes employee Id as an input parameter
  • Updater Method called “UpdateEmp” which takes the data contract as an input parameter

We also decorate these as Operation Contract what we MUST implement to use our Interface. Next we will create the service that will implment the Interface and this is done by creating another class called EmpService.cs

  1.  
  2. namespace NorthwindServiceLib
  3. {
  4.     [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  5.     public class EmpService : IEmpService
  6.     {
  7.         public void CreateEmployee(Emp emp)
  8.         {
  9.             using (var ctx = new NorthwindEntities())
  10.             {
  11.                 List<Employee> empnew = new List<Employee>();
  12.                 Employee e = new Employee();
  13.                 e.EmployeeID = emp.EmployeeID;
  14.                 e.FirstName = emp.FirstName;
  15.                 e.LastName = emp.LastName;
  16.                 e.Title = emp.Title;
  17.                 e.TitleOfCourtesy = emp.TitleOfCourtesy;
  18.                 e.BirthDate = DateTime.Parse(emp.BirthDate.ToString());
  19.                 e.HireDate = DateTime.Parse(emp.HireDate.ToString());
  20.                 e.Address = emp.Address;
  21.                 e.City = emp.City;
  22.                 e.Region = emp.Region;
  23.                 e.PostalCode = emp.PostalCode;
  24.                 e.Country = emp.Country;
  25.                 e.HomePhone = emp.HomePhone;
  26.                 e.Extension = emp.Extension;
  27.                 e.ReportsTo = int.Parse(emp.ReportsTo);
  28.                 e.PhotoPath = emp.PhotoPath;
  29.                 e.Notes = emp.Notes;
  30.                 empnew.Add(e);
  31.                 ctx.Employees.Add(e);
  32.                 ctx.SaveChanges();
  33.                 
  34.             }
  35.  
  36.         }
  37.  
  38.         public List<Emp> GetAllEmp()
  39.         {
  40.  
  41.             using (var ctx = new NorthwindEntities())
  42.             {
  43.                 var currEmp = (from c in ctx.Employees
  44.                             select c).ToList();
  45.  
  46.                 List<Emp> emp = new List<Emp>();
  47.  
  48.                 for (int i = 0; i < currEmp.Count(); i++)
  49.                 {
  50.                     {
  51.                         Emp e = new Emp();
  52.                         e.EmployeeID = currEmp.ElementAt(i).EmployeeID;
  53.                         e.FirstName = currEmp.ElementAt(i).FirstName;
  54.                         e.LastName = currEmp.ElementAt(i).LastName;
  55.                         e.Title = currEmp.ElementAt(i).Title;
  56.                         e.TitleOfCourtesy = currEmp.ElementAt(i).TitleOfCourtesy;
  57.                         e.BirthDate = DateTime.Parse(currEmp.ElementAt(i).BirthDate.ToString());
  58.                         e.HireDate = DateTime.Parse(currEmp.ElementAt(i).HireDate.ToString());
  59.                         e.Address = currEmp.ElementAt(i).Address;
  60.                         e.City = currEmp.ElementAt(i).City;
  61.                         e.Region = currEmp.ElementAt(i).Region;
  62.                         e.PostalCode = currEmp.ElementAt(i).PostalCode;
  63.                         e.Country = currEmp.ElementAt(i).Country;
  64.                         e.HomePhone = currEmp.ElementAt(i).HomePhone;
  65.                         e.Extension = currEmp.ElementAt(i).Extension;
  66.                         e.ReportsTo = currEmp.ElementAt(i).ReportsTo.ToString();
  67.                         e.PhotoPath = currEmp.ElementAt(i).PhotoPath;
  68.                         e.Notes = currEmp.ElementAt(i).Notes;
  69.                         emp.Add(e);
  70.  
  71.                     }
  72.                 }
  73.  
  74.                 return emp;
  75.  
  76.             }
  77.         }
  78.  
  79.         public List<Emp> GetSpecificEmp(int empId)
  80.         {
  81.             using (var ctx = new NorthwindEntities())
  82.             {
  83.                 var currEmp = (from c in ctx.Employees
  84.                                where c.EmployeeID == empId
  85.                                select c).ToList();
  86.  
  87.                 List<Emp> emp = new List<Emp>();
  88.  
  89.                 for (int i = 0; i < currEmp.Count(); i++)
  90.                 {
  91.                     {
  92.                         Emp e = new Emp();
  93.                         e.EmployeeID = currEmp.ElementAt(i).EmployeeID;
  94.                         e.FirstName = currEmp.ElementAt(i).FirstName;
  95.                         e.LastName = currEmp.ElementAt(i).LastName;
  96.                         e.Title = currEmp.ElementAt(i).Title;
  97.                         e.TitleOfCourtesy = currEmp.ElementAt(i).TitleOfCourtesy;
  98.                         e.BirthDate = DateTime.Parse(currEmp.ElementAt(i).BirthDate.ToString());
  99.                         e.HireDate = DateTime.Parse(currEmp.ElementAt(i).HireDate.ToString());
  100.                         e.Address = currEmp.ElementAt(i).Address;
  101.                         e.City = currEmp.ElementAt(i).City;
  102.                         e.Region = currEmp.ElementAt(i).Region;
  103.                         e.PostalCode = currEmp.ElementAt(i).PostalCode;
  104.                         e.Country = currEmp.ElementAt(i).Country;
  105.                         e.HomePhone = currEmp.ElementAt(i).HomePhone;
  106.                         e.Extension = currEmp.ElementAt(i).Extension;
  107.                         e.ReportsTo = currEmp.ElementAt(i).ReportsTo.ToString();
  108.                         e.PhotoPath = currEmp.ElementAt(i).PhotoPath;
  109.                         e.Notes = currEmp.ElementAt(i).Notes;
  110.                         emp.Add(e);
  111.  
  112.                     }
  113.                 }
  114.  
  115.                 return emp;
  116.  
  117.             }
  118.         }
  119.  
  120.         public void RemoveEmp(int empId)
  121.         {
  122.             using (var ctx = new NorthwindEntities())
  123.             {
  124.  
  125.                  Employee empToDel = ctx.Employees.Where(d => d.EmployeeID == empId).Single();
  126.  
  127.                 ctx.Employees.Remove(empToDel);
  128.                 ctx.SaveChanges();
  129.             }
  130.         }
  131.  
  132.         public void UpdateEmp(Emp emp)
  133.         {
  134.             using (var ctx = new NorthwindEntities())
  135.             {
  136.                 var currEmp = (from c in ctx.Employees
  137.                                where c.EmployeeID == emp.EmployeeID
  138.                                select c).Single();
  139.  
  140.                 currEmp.FirstName = emp.FirstName;
  141.                 currEmp.LastName = emp.LastName;
  142.                 currEmp.Title = emp.Title;
  143.                 currEmp.TitleOfCourtesy = emp.TitleOfCourtesy;
  144.                 currEmp.BirthDate = emp.BirthDate;
  145.                 currEmp.HireDate = emp.HireDate;
  146.                 currEmp.Address = emp.Address;
  147.                 currEmp.City = emp.City;
  148.                 currEmp.Region = emp.Region;
  149.                 currEmp.PostalCode = emp.PostalCode;
  150.                 currEmp.Country = emp.Country;
  151.                 currEmp.HomePhone = emp.HomePhone;
  152.                 currEmp.Extension = emp.Extension;
  153.                 currEmp.ReportsTo = int.Parse(emp.ReportsTo);
  154.                 currEmp.PhotoPath = emp.PhotoPath;
  155.                 currEmp.Notes = emp.Notes;
  156.                 ctx.SaveChanges();           
  157.  
  158.             }
  159.         }
  160.     }
  161. }

Thats a heap a code I know, and there are other ways to do this, Im sure, this is just “my way”, you will notice I am employing the Using Statement to encapsulate the Context of the Data Connection then Im either Creating, Updating, Deleting and as in the case of Reading I have one that returns everything and one that returns just one.

THATS IT for the Code part of this!!!

Once all of that is done, you will NEED, YES NEED, to modify the App.config file because it still thinks that the Service Contract is tied to IService1.cs that we deleted earlier. We can also create other endpoiints, yada yada, but thats anotehr story. So here is what you need to do for your App.Config. Right click on it, and click “Edit WCF Configuration”, when you do you will need to change the Service to your EmpService by browsing to the Bin Directory and clicking the Dll & you will also need to set the Contract to IEmpService by doing the same. You will notice I have two endpoints as well, one for the Service Contract endpoint and the other is MEX for the announcement of the Service.

image

This GUI is just and abstract of the .config file so here is the full code for that below

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <configuration>
  3.   <configSections>
  4.     <!– For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 –>
  5.     <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  6.   </configSections>
  7.   <appSettings>
  8.     <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  9.   </appSettings>
  10.   <system.web>
  11.     <compilation debug="true" />
  12.   </system.web>
  13.   <!– When deploying the service library project, the content of the config file must be added to the host's
  14.   app.config file. System.Configuration does not support config files for libraries. –>
  15.   <system.serviceModel>
  16.     <services>
  17.       <service name="NorthwindServiceLib.EmpService">
  18.         <endpoint address="" binding="wsHttpBinding" bindingConfiguration=""
  19.           contract="NorthwindServiceLib.IEmpService">
  20.           <identity>
  21.             <dns value="localhost" />
  22.           </identity>
  23.         </endpoint>
  24.         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  25.         <host>
  26.           <baseAddresses>
  27.             <add baseAddress="http://localhost:8733/Design_Time_Addresses/NorthwindServiceLib/Service1/" />
  28.           </baseAddresses>
  29.         </host>
  30.       </service>
  31.     </services>
  32.     <behaviors>
  33.       <serviceBehaviors>
  34.         <behavior>
  35.           <!– To avoid disclosing metadata information,
  36.           set the values below to false before deployment –>
  37.           <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
  38.           <!– To receive exception details in faults for debugging purposes,
  39.           set the value below to true.  Set to false before deployment
  40.           to avoid disclosing exception information –>
  41.           <serviceDebug includeExceptionDetailInFaults="False" />
  42.         </behavior>
  43.       </serviceBehaviors>
  44.     </behaviors>
  45.   </system.serviceModel>
  46.   <connectionStrings>
  47.     <add name="NorthwindEntities" connectionString="metadata=res://*/Northwind.csdl|res://*/Northwind.ssdl|res://*/Northwind.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=Farm1Server1ADSQL;initial catalog=Northwind;persist security info=True;user id=oDataSU1;password=P@ssword1;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  48.   </connectionStrings>
  49.   <entityFramework>
  50.     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  51.       <parameters>
  52.         <parameter value="v11.0" />
  53.       </parameters>
  54.     </defaultConnectionFactory>
  55.   </entityFramework>
  56. </configuration>

Test your Work

Now that you have done all that you can certainly test your work even before it goes into IIS to be hosted outside of Visual Studio. You can do that by using a nifty tool inside your Visual Studio Command Promt called “WCFTestClient.exe” and passing it the URI of your Service, but the easiest way is just Press F5 in Visual Studio and it will do all that for you LOL, we will use the tool later on when we move it to IIS. Just for giggles sake, I will just test the GetAllEmp for this blog. IF you want to see more come see my VS Live 360 Session that i spoke about on my blog here

image

As you can see above, we have the Service exposed and all the methods, and we invoked one them and we can see the results there.

Move this to IIS – Hosting this Service in IIS

Now unless you want to have to start up Visual Studio every time you need to use this WCF, what you need to do is host this in IIS or you can do TCP or NetBinding but We are all working in SharePoint for this blog so we need IIS. Its simple, just add a new Web Site Project to this Visual Studio Solution; for this we will use the WCF Service Template.  This too will add a sample implementation again in the App_Code folder which includes Service1.cs and ISerivce1.cs. Delete those again & and set a reference to the WCF Service Library DLL from the first project.  If you are asking why we deleted both those files again its because we will be using the files from our first project referenced in our .svc page. Your solution may look like this now.

image

You will also have a file called Service.svc, that we will rename to something more descriptive of our Service we are trying to expose, I name mine NWindEmployee.svc Once you have renamed it, you will need to set the Service to the Project Namespace and remove the codebehind attribute since we wont need it.

  1. <%@ ServiceHost Language="C#" Debug="true" Service="NorthwindServiceLib.EmpService" %>

Save your file, and now its time to modify the Web.config file this time to point to our Service Reference, this is JUST like we did for the App.config so I wont bother going into detail here, Ill just include the code snippet for the web.config

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <configuration>
  3.   <configSections>
  4.     <!– For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 –>
  5.     <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  6.   </configSections>
  7.   <appSettings>
  8.     <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  9.   </appSettings>
  10.   <system.web>
  11.     <compilation debug="true" />
  12.   </system.web>
  13.   <!– When deploying the service library project, the content of the config file must be added to the host's
  14.   app.config file. System.Configuration does not support config files for libraries. –>
  15.   <system.serviceModel>
  16.     <services>
  17.       <service name="NorthwindServiceLib.EmpService">
  18.         <endpoint address="" binding="wsHttpBinding" bindingConfiguration=""
  19.           contract="NorthwindServiceLib.IEmpService">
  20.           <identity>
  21.             <dns value="localhost" />
  22.           </identity>
  23.         </endpoint>
  24.         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  25.         <host>
  26.           <baseAddresses>
  27.             <add baseAddress="http://localhost:57757/Design_Time_Addresses/NorthwindServiceLib/Service1/" />
  28.           </baseAddresses>
  29.         </host>
  30.       </service>
  31.     </services>
  32.     <behaviors>
  33.       <serviceBehaviors>
  34.         <behavior>
  35.           <!– To avoid disclosing metadata information,
  36.           set the values below to false before deployment –>
  37.           <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
  38.           <!– To receive exception details in faults for debugging purposes,
  39.           set the value below to true.  Set to false before deployment
  40.           to avoid disclosing exception information –>
  41.           <serviceDebug includeExceptionDetailInFaults="False" />
  42.         </behavior>
  43.       </serviceBehaviors>
  44.     </behaviors>
  45.   </system.serviceModel>
  46.   <connectionStrings>
  47.     <add name="NorthwindEntities" connectionString="metadata=res://*/Northwind.csdl|res://*/Northwind.ssdl|res://*/Northwind.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=Farm1Server1ADSQL;initial catalog=Northwind;persist security info=True;user id=oDataSU1;password=P@ssword1;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  48.   </connectionStrings>
  49.   <entityFramework>
  50.     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  51.       <parameters>
  52.         <parameter value="v11.0" />
  53.       </parameters>
  54.     </defaultConnectionFactory>
  55.   </entityFramework>
  56. </configuration>

Make this available in IIS

Now you can test this guy again using the Test Client, but the hosting into IIS Is quite simple and Ill just show you that.  All you have to do is either (1) Publish the site into a new Directory for PROD consumption (2) just copy and paste the folder where you want on a box running IIS and create a new Web App / Site pointing to that location and setting a FQDN and as in my case a port number where it can be consumed.

image

As you can see from the image above, I have a site with my FQDN port 57758 where it is located. Navigating to that site will yield

image

and clicking on the end point will show the WSDL

image

The Finale

Now we will use SharePoint Designer to Create an External Content Type (ECT) that will consume this WCF.

Open up SPD2013 and create a NEW ECT, Name it apprpriately, as Ive done here calling it BlogPostWCFExample, Next click on “External SYtems”  link to discover, its the last link on imabe below

image

Once you have done that click Add Connecation

image

Then Select WCF from the Drop Down…and click OK then

image

As a reminder we are using User Identity but i wanted to point out we just as easlily could be using Secure Store, if we were we would use the Target App ID as seen here

image

but for my example you will see it is more like this

image

When all of that is complete you will be able to see all the methods exposed, see below

image

Next you can right click on them individually and create the respective Methods, this has been done many times so i wont belabor it. But once you are complete you should have an ECT like so

image

and you should be able to create a List like so…

image

Summary

So, that is it for my Part 1 of 3, I will tackle the next one later tonight or tomorrow. Cheers, hope this helps, let me know your thoughts, critiques, etc.

 

October 14, 2013 Posted by | Business Connectivity Services, Public Speaking, SharePoint 2013, SharePoint Designer 2013, SharePoint How-To, WCF | , , , , | 2 Comments

Using the Call HTTP Web Service REST Calls in SharePoint Online using SPD

Précis

So I’ve had folks say that they still cant make the leap and get my examples to work in Office 365 SharePoint Online, so this post is to Prove out that IT can be done, there is no smoke and mirrors, I just wanted folks to take what I have and use that to go to the next level. This is not like a live session where I have an interactive audience, so I am putting the info out there in his blog.

This post builds on a few out there that I have done explaining how to use SharePoint Designer 2013 with the Dictionary Object to data in JSON Format to be used inside Workflows. Now, I do admit that there are VERY FEW examples out there, if any, that show you, how to do what I am about to tackle in Office 365 SharePoint Online. So here goes.

 

Approach

As usual we have an Initialization Stage and a Workhorse Stage, in the Init stage we will set our variables and make our REST call, we will parse it and limit our dataset to what we need for our results. I think folks are getting tripped up on what is needed in the Headers, as what I am getting as feedback is that the Workflow Starts but it stalls with a message like “Retrying last request. Next attempt scheduled after…” with a correlation ID, it also gives you a Retry Now Link.

So the first thing i will show you is of course what is in my List NOW, I will show it to you via the browser and in Fiddler.

image

now here is the items in Fiddler, i do this so you can appreciate the actions I will take later on in SPD when parsing the JSON data.

image

Now lets look at how we construct the Header, in this post, i have explained why you need certain Headers, so i wont go into it here, the only difference is that I am extrapolating what I did in Fiddler and using it in SharePoint Designer… now for obvious reasons, I am NOT showing you all the Header Information since it is my Valid Token to O365.

image

Now with that done, I have constructed a Site Workflow that will loop through the items and Log it to History, what you do with it for your Use Case can be quite elaborate, you essentially can

  • Communicate with any Office 365 SharePoint Online Tennant you want from a Single Workflow
  • Cross Communicate between an On Prem Farm and a Cloud Farm in one Workflow
  • Create List, Libraries, List Items, and do full CRUD operations

If you go and look at the REST API endpoints from that link i gave you before by Kirk Evans, you can see that its quite a bunch of things that are available now.

So. back to the story.

First Part of the Workflow

image

Second Pare of the Workflow

image

Next lets fire off this Workflow and look at the results.

Proof

Next we go to Site Contents, Site Workflows and click the one we just created after publishing

We should expect to see the below in our History List

image

and upon closer inspection of the Workflow

image

 

Summary

Now what I will say because I have worked quite a bit with SharePoint Online Wave 15 is that it can get squirrely and can crap out on you for no good apparent reason, it gives you obscure feedback when it does and often times it just says “something bad has happened..” and if you Refresh the browser, its back to normal. I don’t pretend to understand why, I don’t have eyes into the Data Centre nor access to the ULS Logs, so I am as much as in the blind as you are. BUT this is the reason why i do Fiddler first then SPD after…visibility.

I hope this helps! Cheers.

 

September 4, 2013 Posted by | JSON, Office 365, REST, SharePoint 2013 Workflows, SharePoint Designer 2013, SharePoint How-To, SharePoint Online, Workflows | , , | 4 Comments

More on SharePoint 2013 REST API with Fiddler and SPD

Précis

So, as of lately when it comes to SharePoint 2013 Workflow, CSOM with Managed Code, REST API, I have been trying to see how far down the rabbit hole i can go, and talking about what I find.  As a result, I have been fielding quite a few questions on the topic; although there is quite a bit of information out there by Kirk (@kaevans) Evans here on MSDN and others, what i have find is that they discuss a lot of the Reading Data, usages in C# and JavaScript and they elude to debugging with Fiddler.  I on the other hand, like to test my work in Fiddler then take it into SPD (SharePoint Designer 2013) or Visual Studio 2012 for either work with Console Apps, Workflows, Event Receivers etc. It also gives me the opportunity to see what is happening under the layers of SPD, or obscured by Visual Studio Activities. Now there are times when that door will get slammed shut in you face especially around SharePoint Online Office365 and then i just rely on Wictor (@wictor )Wilens MSO Helper in a Console App to peruse around. This post will show you how to Add data into a SharePoint List (i.e. ListItem) using the REST API via Fiddler and How to Create a SharePoint LIST as i couldn’t find a decent example out there after suggesting to a work colleague that they use Fiddler to map out their work then do it in SPD. On the other hand, there is a great post here by Borislav Grgic on how to use Add an Item to a SharePoint List using the REST API and POST Method in SharePoint Designer 2013.

What Major Obstacles you will need to Overcome

This is what I found lacking when I did research on how to achieve the stated objectives above.  Most of the examples just told you to either “..type this” or “create a dictionary object to to that…”, and some just omitted major steps that you will get tripped up in and no way to circumvent it. So, taking this by the numbers, lets first assume that you want to get back some List data from SharePoint 2013, we will do this first On-Prem then In-Cloud (Office 365).

On Prem

  1. You will need to construct a Uri representing your target list such as :  //_api/lists/getListByTitle(‘’)/items">http://<site>/<web>/_api/lists/getListByTitle(‘<listNameHere>’)/items in a browser (preferably Chrome) or Fiddler
  2. You will need to set some Headers, specifically the (a) Accept and (b) Content-Type Headers to accept ‘application/json;odata=verbose’ inorder to work with the return items in JSON format which if you are using this in SPD, you MUST do in order to use the Dictionary Object, or if you want to read the data in some logical hierarchy.
  3. If you are ON Prem, and using Fiddler inside “Composer” under Options, i usually set the Automatically Authenticate Flag like so, you will see me talk about Decrypting HTTPS Traffic later on in the Cloud or HTTPS targets later on. In doing so your Cookie header will be automatically set.
    image
  4. Next ensure your Verb is set to GET and fire away.

In Da Cloud

  1. Well steps one (1) and two (2) are the same from the above, however step three (3) is a bit different.
  2. You will need to pass your Cookie Token for the Office 365 Site here, and simply put you will log into the Target Office 365 Site and using using Fiddler you set it to “Capture Traffic” after you log in. If/when you click on a List or Library as an example, inspect your Headers returned (assuming you have already gone under Tools / Fiddler Options and selected to Decrypt HTTPS Traffic) like so
    image
  3. Once you do and inspect the Headers you will find cookies for FedAuth and rtFa under the “Cookie” header. Go ahead and copy and paste that in the Request Headers section of Fiddler
  4. Next ensure your Verb is set to GET and fire away.

What if i want to POST (Add) Data to my SharePoint List?

Well, if you do, you have to do a few more things before you can actually POST that data.

  1. You will need to get the X-RequestDigest Header to send along with your POST, notice this is “not” a GET anymore. Now referencing the post again above by Kirk Evans if you look almost 3/4’s away down on the page or just Cntrl-F to find the word “contextinfo” you will find a description on how to get that Header Information in the d:FormDigestValue node.
  2. Essentially you will need to change your Fiddler VERB from a GET to a POST and send along an empty Request Body, you may need to set a “Content-Length” Request Header as well, and i usually do 104 for that length.
  3. You will get back the information that you will need to add to your Fiddler statement.
  4. In addition you will need to construct a Key/Value Pair (hence working in JSON) to pass along in the Request Content that you want to Add
  5. Now there is one more obscure piece of information that ususally trips people up that you will also need to do, and this information will form a part of your Request Content that holds your Data you are trying to add.
    1. When you did your GET for the site, there was a Node called (underscore undescore metadata) __metadata. You will need to take that value and pass it first ahead of any data you are trying to add. You will see this example below
  6. Once you do all the above, you ensure that your Verb is set to POST and fire away.

Lets look at a few Examples

So taking this from the beginning we will use a use case of a List i have called “AndyTaskListOnPrem” and we will first do some Gets, then a Post and then finally verify our work. Here is an example of the list before the exercise.

image

Next lets look at how you need to construct the GET to look at the data in JSON format in Fiddler

image

And our Outcome. Pay attention to the Metadata Node and the type = SP.Data.AndyTAskListOnPremListItem value. you will need this later on for the Adding of List Item.

image

continued

image

Next lets look at how to get the ContextData information.

Create a POST with the URI of /_api/contextinfo">http://<site>/_api/contextinfo as i have below

image

Ensure the Response Content is blank and execute that, your results should look like mine below. You will need to capture the “FormDigestValue” information for later on in your POST.

image

Next lets Create a List Item

Armed with that information we have enough to create a List Item in this SharePoint List using the
REST API and Fiddler and using the same information plug this into Dictionary Objects in SharePoint Designer along with the Make a HTTP Call to do the same, here’s how…

image

Paying attention to the POST Verb, the X-RequestDigest “Request Headers” and the __Metadata and Addition in the “Request Body” after execution the above you should get

image

a HTTP/1.1 201 Created response. and if we inspect the list we should expect to see

image

 

To round things off, lets assume our Target was Office 365 SharePoint Online, then we would expect to see our Headers contain our Auth Tokens.  I will take this to a next level by using an example someone just asked me about. I will Create a List in Office 365 “not list ITEM, an actual LIST” using the same methods described above. the KEY takeaway here is I need to Pass my Auth Tokens and ofcourse I am using a different REST API Call

Lets Create a SharePoint List using REST and Fiddler

So, in the spirit of openness, here is the REST call to get my Auth Token… well some of the token, I wasnt born yesterday 🙂

image

Here is the Context Info, key here is remember its not HTTPS and you need pass the Token as well.

image

Finally here is me creating A NEW LIST in the Web

image

and with all the confidence in the world [well this is O365 so we also cross our fingers] we expect to get a 201 Create Response back from HTTP Header

image

and visually in the browser we should expect to see

image

Summary

So, in this exercise we accomplished the following

  • Understanding of some of the SharePoint 2013 REST API
  • Usages of that API in Fiddler and the Browser
  • How to Add a SharePoint List Item using the REST API
  • How to create a new LIST using the REST API

This should translate VERY easily into SharePoint Designer 2013 and Visual Studio 2012, that is the point of the exercise. Thats why I am NOT showing how to do it there, I have other post that talk about the Dictionary Object and how to create Headers, just use what I have here and apply it to one of those post and you should be golden. This is me teaching to fish.. 🙂 Cheers.

 

September 3, 2013 Posted by | JSON, Office 365, REST, SharePoint Designer 2013 | , , | 16 Comments

SharePoint Saturday New York 2013 Wrap-up

Praises

Year after year the organizers and volunteers that put on SharePoint Saturday New York City #spsnyc do an amazing job and the intensity and excellence keeps getting better year after year.  Fantastic Job Rebecca “Becky” I, Greg G, Jason G, Tasha S, Tom D, Brandon B, Casey S, and the rest of the crew too many to name.

This year it felt like on all level it came together even with the late lunch 🙂 The audience was exceptionally knowledgeable and engaging, we had fun while learning; i guess it helps that I actually have been seeing them for many years know and we treat each other like extended family.  Even though next year we will be in a new Venue, I will miss the Microsoft facility across form the Hilton where the Halal GUys have their truck.

Regarding my Preso, Deck, Session Information

So, as noted in my presentation, and for those of you that know the way I deliver sessions, I do a lot of demos, hardly any slides. This time however, my session was based on a 2 part blog post I did, I requested that my session attendees not take notes because the blog post will be more information that you had in the session, so please pick it up here

Finally a SharePoint Designer that Developers and BAs will Love – This is the Part 1 of what would have come before the session you sat in and

Part 2 of Finally a SharePoint Designer that Developer and BAs will Love – This is reflective of the session you had

Thank you all for attending, hope to see you again next year.

Cheers.

July 29, 2013 Posted by | JSON, Public Speaking, REST, SharePoint 2013, SharePoint 2013 Workflows, SharePoint Designer 2013, Where is Fabian | , , , | Leave a comment

Addenda to Finally a SharePoint Designer every BA and Dev will Love

Alas, it was getting a bit busy on the blog post to cram so much Information so this Post follows on the 2 that were already done.

  1. Part 1 – Stages, Steps, Loops, and Visio Professional 2013
  2. Part 2 – Put it all together with HTTP Service Calls via REST and the Dictionary Object/ Variable

This post will look specifically at making those REST API Calls in SharePoint Designer 2013 (SPD2013) on Web Sites “other” than SharePoint i.e. those FREE API’s that are out there. This one will use the Free Weather Online API. You can get an account here and get a Key Just for you 🙂

So..What’s our Story

Well we will endeavor to create a Site Workflow that will run every 24 hours and 5 minutes and display using a Promoted List in SharePoint 2013 the Weather Forecast for the specific Office Location in our Fictitious company. Now, I’ve done a presentation at a SharePoint Saturday or two that discussed creating a Custom Action that would take a parameter (text box field) holding Zip Code so one could customize/personalize this Workflow.

We begin by creating a new Site Workflow

As all good post go, we tell you how to start, so Fire up your SharePoint Designer 2013 and connect to your SharePoint 2013 site, this can be On Prem or in Da Cloud. Click on Workflow then New Site Workflow

SNAGHTML1ffc4d8b

Next you Stub out your Workflow, this time you should know all of my bag of tricks now to make life easier so I am including as much as possible here below, if you don’t know or haven’t been following the series, take a look at the other two links in the top of this post.

image

I now do my magic for the HTTP Call, get the Sub-Set of Data I need and then Iterate through the values

 

SNAGHTML201d158b

Above is the Fiddler Call I use to inspect my results so I know what to put in my SPD2013 Get Item From Dictionary Action, once you have that bit of information, then the rest, you have seen be do before. So…

image

Basically we make our HTTP call, put it in our data Sub-Set, and Get each individual element inside our loop, the ONLY thing different in this example is that I am doing a Create List inside the loop to add the current record to my Promoted Link List and you will see that later on below.

image

Once you invoke the workflow it does its job and remains in a Paused state for 24 hours and 5 minutes then runs again. To the user they will see below if all goes well when they view the list or we can have it as a Web Part or App on a page

image

Summary

So there you go, you are now knighted as SPD2013 Workflow professionals, go thee forth and conquer!

So, my good buddy Chris @givenscj Givens, a really brilliant fellow said that doing this series in Visual Studio would be a benefit. I will take a few days to do that, but I promise I will, need to go back to billable work now 🙂

 

April 30, 2013 Posted by | REST, SharePoint 2013, SharePoint Designer 2013, SharePoint How-To | , , | Leave a comment

Part 2 of Finally a SharePoint Designer that Developer and BAs will Love

Purpose of this Blog Post & Primer for this Post

This post follows up on a previous post below where we discussed Workflows in SharePoint 2013 through the eyes of SharePoint Designer 2013 and Visio Professional 2013. Part 1 is linked below, this is Part 2

Part 1 – Stages, Steps, Loops, and Visio Professional 2013

In the above post (this one) I will go about showing you the tooling and various simplistic but apropos examples designed solely to drive the usage home, in Part 2 we will use everything in a practical REAL WORLD sense

Part 2 – Put it all together with HTTP Service Calls via REST and the Dictionary Object/ Variable

In this post I will take a Publicly Available REST API probably the Weather API or Twitter API and use the HTTP Web Request along with the Dictionary Variable in SPD 2013 to surface it in a SharePoint Promoted Links App (List)

What is the Dictionary Object/ Variable

Simply put a Dictionary Variable is an Array i.e. a collection of related items that is indexed/indexable but in more detail it is a container designed to hold a collection of other variables which can be of a different data types. Specifically we have the following actions out of Dictionary

  • Build Dictionary
  • Count Items in a Dictionary
  • Get an Item from a Dictionary

image

Now before we get too deep into The Dictionary Variable/Object, lets take each of these actions one at a time in a simple scheme and see what they do exactly; kick the tires so to speak, in our real world example we will NOT be creating the Dictionary Object as much as consuming JSON data and dumping the JSON data inside a dictionary object. You get it right? JSON data usually comes across as a JSON Array or JSON object, in our case we want that JSON array, even if it is one item, the Dictionary Object/Variable EXPECTS to see an array, I actually wrote a blog post https://fabiangwilliams.wordpress.com/2012/12/31/limitations-when-using-sharepoint-2013-workflows-to-return-json-data/ explaining that issue, which i think is a BUG that should be handled by the SharePoint Team.  But lets leave that on one side for the moment and get back into our intended post.

It is ALL explained Here

As mentioned earlier, a Dictionary Object may be considered a collection of related items. Now to drive this point home lets use what we have at our disposal, in fact we will use two (2) examples, the first of which is to simply build on what we did in Part 1 using the Bank of Fabian Example.  You see, there are ALOT of post out there that do a good job of explaining to you how to get Weather Data, Twitter Data, ITunes Data, you know all the public REST JSON available stuff, and granted I will do one here as well in example number two in the spirit of being complete. However, what you don’t often see is showing how to use SharePoint own REST OData examples, so I will do one here. Lets set it up for you.

Our Use Case

Now I’m a big believer in Use/Business cases when doing Demos’ and not doing parlor tricks for the sake of “Hey! Look at this cool new stuff I can do”, so The idea here is that

1. You can consume data from ANY SharePoint List as long as you have an account that has permission or an App that does, and then act on or manipulate data from inside your Site Collection [assuming a Site Workflow here]

2. You want to expose data/information to an audience that doesn’t have access to certain data or hosted elsewhere

Web Site:  http://adotob.sharepoint.com (this is my Office 365 Dev Site)
List Instance: https://adotob.sharepoint.com/_layouts/15/start.aspx#/Bank%20of%20Fabian

image

So from the above we can tell that we have a list called …/bank%20of%20fabian and it has currently 5 items inside it yes? Ok, good. Now in order for us to get to that data we can call on our SharePoint oData service located here

https://adotob.sharepoint.com/_api/web/lists/getbytitle(‘Bank%20of%20Fabian’)/items

Now you should understand that SharePoint 2013 REST APS doesn’t understand the $json options so, it will render its result in XML, however there are quite a few tools out there that can convert your XML to JSON. In fact if you adjust your request header on your call yourself in Fiddler or in browser extension such as Chrome or FireFox, you can get the visual effect you are looking for as well.  So lets see what happens when you click on the link here for the REST API call

image

Now that is NOT going to be useful to us, so I will use this Chrome extension called ChangeHTTPRequest and after putting in my header i need which is

Accept: application/json;odata=verbose

I get the below

image

Now admittedly that is also difficult to read, so you should invest in the FREE JSON Viewer I always use and its Waaayyy cleaner, just cut and past the entire content and you can see what I mean.

image

So now that we have established some “Trust but Verify” credibility, next lets look at what we need to do in SharePoint Designer 2013 (SPD2013) to move this forward. We will be Demonstrating the following Capabilities in this Example

  1. How to use Dictionary Variable/Object
    1. We will build a Dictionary Variable to hold our HTTP Header information so we can get JSON Data
    2. We will Count Items in a Dictionary Variable so we can get the “Count” value of all items in there to assist us in our “Loop” that we may iterate through all the items in the Dictionary
    3. Get Items from the Dictionary that we may use to (as in our case) Write to the Log History so we can see what we are consuming, but in our Part 2 Example coming up using PUBLIC REST JSON data we will use that data to load into a ListItem
  2. Call a HTTP Web Service
    1. We will call that webservice we discussed above to get oData back
    2. We will discuss the other Dictionary Variables we will need in Support of this Call such as our ResponseContent and our subset of Data we want to inspect
  3. Do a Loop
    1. We will use the total items count that we got from our “Count Items in a Dictionary” Action as our loop counter max value in our Loop construct

Lets Build out our Workflow

The first thing we will do is create a Site Workflow

SNAGHTML1c311bfc

So in keeping with “Fabian’s Best Better Practice” we will stub out our Stages and Get some basic Logic out of the way

image

Once that is done the first thing we will do is build a Dictionary Variable to hold two items we need in order to get JSON data to be returned; we need to set the Accept and Content-Type HTTP header so the browser knows what we expect. We click Action then under Core Actions you will find Build Dictionary, because I use it a lot it is in my Recent Actions also

image

Once you click on ‘Build Dictionary’ you will get…

image

1 – You will then click on ‘this’ which will open up the dialog in [2] and in turn you will click the Add button. In doing so you will enter two Variables into this Dictionary Variable of String Data Type as aforementioned, in [3] I am showing you how i do it for the Accept header attribute.

image

When you are done, click the Ok Button. AND NOW YOU HAVE SEEN HOW TO CREATE A DICTIONARY VARIABLE and an acceptable use of it.

Next we will “Call the HTTP Service” by again clicking Actions in the Ribbon and then…

image

So again by clicking [1] ‘this’ we get the dialog box to enter the information about our HTTP REST Service which in this case we click on the ellipse [2] and put our workflow variable we created earlier that holds our URI, then we click [3] the OK Button.

Oh… i forgot something, but we can do that now… so you see in the above figure the second line has the (Output to Variable:dictionary) right? so I like to tidy up my Variable name, so what I do is click on the word ‘dictionary’[1] and create a NEW variable called RequestHeaders [2]

image

Now that that is done, we want to further configure the Service Request Call so we will ‘Right Click’ on our ‘Call’ and then Click on Properties..

image

Then I want to set the “RequestHeaders” Property to my Dictionary Variable “RequestHeaders”

image

and then click the OK Button.

Next we will need to ALSO create another Dictionary Variable to actually HOLD the data that’s coming back, if I haven’t said it before, that data comes back in the form of a JSON Array and the Dictionary Variable is the only object suitable to accept that data. So

image

and we name it ‘JSONResults’ so if we inspect the properties again you will see that we have everything we need now to call out to our HTTP Service and get meaningful results back

image

So now we can actually got GET the DATA now, by using yet another Action in the SPD2013 Ribbon, this time its the ‘Get Item from Dictionary’ Action

image

Once we do that we will want to click the ‘item by name or path’ link and give it the SUBSET or WHOLE path to the data we inspected in our JSON Viewer or whatever tooling you have to determine what you need. In our case we want the “d/results” subset then we will use the Index variable to get each line item. Furthermore we will want to store this information AS WELL into another Dictionary Variable.

SNAGHTML1c5cf00d

So in [1] you have me setting the path based on what i want from evidence in the JSON Viewer, then in [2] I am showing you the relationship of use getting the values from the HTTP Call made earlier and finally [3] I’m dumping that data into another Dictionary Object.

SNAGHTML1c5eb07e

if you forgot here is what i need to build that path take a look above at [1], then [2] to get my Array. In [3] I will use that below using the Index Variable inside a Loop Construct.

So that brings us to the last unique item we will be doing in our Dictionary Item and that is to get a COUNT of all the items in the Dictionary We Need. So again we go to the SPD2013 ribbon

SNAGHTML1c6355f1

and we now need to tell the action which Dictionary Item to count, REMEMBER we want the one that we did the Subset of, not the one returned from the Service Call.

SNAGHTML1c65a1a0

Above [1] we are referencing our Subset Data and we are creating a new Variable of Integer type [2] to put that Number in. NOW this is an important distinction as well because what we have here is our UPPERBOUND number in the Array for out Loop Counter 🙂 smart huh LOL

Next we will enter the Stage “Loop and Write out our Data we Need” and put a Conditional Loop inside there that will use the Index value of 0 to start and STOP when we have reached the UPPERBOUND value of items in the array

image

Now when that’s all done, WE are done, one more Screen Shot and some guidance and you can take a mental break..

image

Now admittedly its a bit busy, and yes, its been hours now since I started on this blog and fatigue is setting in 🙂 but I think i can capture everything I need to say here, and I will wrap up as usual with a video. So we are setting the Initialize and Upper bound of our Loop counter in [1] and [2] respectively. In [3] we are actually going to WRITE OUT TO THE LOG the information garnered from the above two lines and the remainder is the Loop Counter which you saw in Part 1, so now time for the video of it all.

So this blog post is long enough, what I will do for the External Data Call is do it in another Post and I will Link to it here. Hope you learned something new.

Cheers

Up Next – Addendum to Part 2 showing an External HTTP Service Call using REST/JSON [Hyperlink forthcoming]

April 29, 2013 Posted by | JSON, SharePoint 2013, SharePoint Designer 2013, SharePoint How-To, Webcast | , | 3 Comments

Limitations when using SharePoint 2013 Workflows to return JSON data

Précis

I struggled with the title for this blog; dare i say, after spending almost two days trying to make SharePoint Designer 2013 and Visual Studio 2012 Workflow return data from a few notable providers [YouTube, Twitter, iTunes] using the Dynamic Value / Dictionary Data Type in JSON format, and not being able to as documented, i was frustrated to say the least. I would have been Ok, if it wasnt for the fact that the same exact Logic/Process works for other providers such as [FreeWeatherOnline, etc] returning data in JSON format, the key difference being the structure of the JSON data being returned.

This is for a few SharePoint Community Events that I have been scheduled to present this material. So after beating my head against the wall, sending out S.O.S tweets, and pinging a few folks that are versed on the topic, I ended up doing my Demos with the REST API provider that returns the JSON data in the way that works OOB and documented in both SharePoint Designer 2013 (SPD2013) and Visual Studio 2012 (VS2012). Nevertheless, this was going to haunt me if i cant get it to work, so i went back at it and with the help of Bart @bart_tubalinal Tubalinal who is the self professed and "fabian validated" Pound for Pound Undisputed Developer in the World, we proved out that if you altered the return JSON data from the providers that didn’t work as expected, and then make a call from SPD2013 and VS2012 using the DynamicType / Dictionary variable, it worked as expected.

I will describe the process we used to determine this below…

Background Research

Below, I will show you three data calls to REST API providers returning in JSON format, showing you both in the browser and in the JSON viewer, I call your attention especially to the JSON viewer and the hierarchy.

Screen clipping: Google YouTube API Above

Screen clipping: FreeWeatherOnline API Above

Screen clipping:  Twitter API Above

Screen clipping: YouTube JSON Data Returned Above

Screen clipping:  FreeWeatherOnline JSON Data Returned Above

Screen clipping:  Twitter JSON Data Returned Above

Hypothesis

What we found out is that if you have anything under the Root of the JSON node other than a JSON Array, for example as in the case of a few, the Version Number [returned as a JSON Object], although it works perfectly in a browser, or JSON Viewer, or Fiddler, it doesn’t make the right call when using SPD2013 or VS2012. If after modifying the data output and removing anything that is NOT a JSON Array from the Root of the Node, it should work as expected.

The first thing to prove it out is to have SPD2013 or VS2012 make a call to a URI that returns JSON, so that can easily be accomplished via Visual Studio Project to create a new Site, and then drop a file with the removing anything in the root that is not a JSON Array (Collection) and calling that Web Site from inside for example SPD2013 then it should return the requisite data than can be consumed by the Dictionary Object.

Test/Prove Out

To begin, the first thing we need to do is remove the "appVersion: 2.1" JSON object out of the data returned, below is what is looks like without any adjustment

after you remove it, the raw file looks like below

just to verify, ill put it into a JSON Viewer to make sure it is what i want

then drop that raw file into a text document and place it at the root of the newly created Visual Studio Project Web Site

verify that you can get to it from a browser call

Analysis

Now, below I will show you WITHOUT adjustment how the FreeWeatherService REST API works right off the bat, you will recall above that the only thing at the JSON root node is the JSON Array. So we put it into SharePoint Designer as below

Screen clipping: See the URI and Process Above

Screen clipping: Above you’ll notice the return data goes to the Dictionary DataType

So after you publish the Site Workflow and run it on the Site, you get what you expect to see below

Screen clipping: SharePoint Promoted Links List with JSON Data Returned

Showing the YouTube Working with the Mods to the Data Set and Without the Mods

So, below you will see how we make a call to the Virtual ASP Visual Studio Site [look at the URI call line you will see it set to localhost and port number]

Screen clipping: SPD2013 WF with the Modded JSON data call Above

After publishing the Site Workflow to the Promoted Links List we can see the output works as expected.

Screen clipping:  Promoted Links List showing the Top 10 YouTube Videos displayed with Modded JSON data Call

Now Showing it NOT working as Expected

Below you will see the same code as in the one that worked, except the URI is set to the Google YouTube REST API and no Mods will be done to this data set.

Screen clipping: Above the same Logic/ Process except this call is made directly to the YouTube REST API

Now pay attention to the logging above, you will see that when the Workflows is published and deployed and ran, that the line for the call fails.

Screen clipping:  Above is me executing the Site Workflow

Below you will see the Workflow status showing that the process halted

Screen clipping:  The Workflow Status showing it didnt work

Report

So, I originally called this a BUG, but for now it is just an observation. Ill be happy to know from our community folks, MVPs, MCMs, and other folks that come across this blog your thoughts. Sadly, I have turned off comments on my blog, but you can always shoot me a tweet at @fabianwilliams

thanks cheers.

Other Notables but I wont write it up, you can discern yourself

See me making it work in a Custom Action and also Not working using one REST API result that is in the format I said will work, and the other with it not.

Screen clipping taken: 12/31/2012 3:32 PM

Screen clipping taken: 12/31/2012 3:32 PM

Screen clipping taken: 12/31/2012 3:34 PM

December 31, 2012 Posted by | JSON, REST, SharePoint 2013, SharePoint Designer 2013 | , , , | 1 Comment