Monday, September 22, 2014

Exploring Office Graph and the Graph Query Language

So you must have heard about the Office Delve launch recently. I have been trying out my hands at the Office Graph which powers Delve and the Graph Query Language which can be used to get data from the Office Graph. To get a comprehensive idea of how it all works, follow these links:

Developing Apps against the Office Graph - Part 1
http://blogs.msdn.com/b/richard_dizeregas_blog/archive/2014/09/15/developing-apps-against-the-office-graph.aspx

Developing Apps against the Office Graph – Part 2
http://blogs.msdn.com/b/richard_dizeregas_blog/archive/2014/09/17/developing-apps-against-the-office-graph-part-2.aspx

Using Graph Query Language (GQL) with the SharePoint Online Search REST API to query Office graph:
http://msdn.microsoft.com/en-us/library/office/dn783218(v=office.15).aspx

So basically we can say that the Graph Query Language can be used to filter normal search queries to return content which is highly relevant to the user. I think the most important thing about the GQL is the EdgeWeight property which can be used to return content sorted by it's "closeness" to the user. This 'magic' property will also return a relevance score of how close the object (content) is to the actor (user).

For example, if  I make a query to get all the people with whom the current user closely works with, the person who will have the highest relevance score will be the person with whom the current user works with most closely. The next person will have a slightly lesser relevance score and so on. This will also be true for other queries such as documents which are most relevant to the current user. This data can be very useful and can be used in a variety of scenarios.

Here is an example of a query to get the users with whom the current user works closely. I have modified the code from Richard diZerega's blog which is linked above.

Saturday, September 13, 2014

Deploy Workflows to Host Web with Integrated Workflow Apps.


In the recent Office 365 Developer YamJam on the Office 365 Technical Network, there were lots of great questions asked to and answered by the Office 365 Engineering Team. You can view all the questions over here: https://www.yammer.com/itpronetwork/#/threads/inGroup?type=in_group&feedId=4419638

One thing which I was interested in was the Integrated Workflow Apps. I have seen a lot of people ask about this as they want to deploy Workflows to their Host Web (or Content Site) from their Apps. I had known that something was in the pipeline as I had seen a glimpse of it in this video from SPC 2014 http://channel9.msdn.com/Events/SharePoint-Conference/2014/SPC3994 

Luckily, Tim McConnell who is the speaker in the video was part of the YamJam and he answered that Integrated Workflow Apps are already available on SharePoint Online but they are waiting for tooling support so no official announcement has been made till now.

I have tested them on my development Office 365 Tenant and it works! In this post, I will be detailing the process by which you can get it working too. 

Please bear in mind that this is a temporary workaround which involves modifying some Manifest XML files. Official tools will be released in the future which will automate this. Till then, use this method for experimental purpose only. Do not use this method to deploy Workflows in Production!

You will need to have installed Visual Studio 2013 and Microsoft Office Developer Tools for Visual Studio 2013 for this. My test project is available on GitHub here: https://github.com/vman/Integrated-Workflow-Apps

1) Start Visual Studio 2013 > New Project > App for SharePoint



2) Enter the SharePoint Online Site Url and select the hosting model for your app. I have selected SharePoint Hosted.



3) Right Click App Project > Add > New Item



 4) Select Workflow and type in a name for your Workflow

(Click on the Image to Zoom)

5) Select the type of Workflow as Site Workflow


(List Workflow Deployment is shown at the end of the post) 



6) Select "Create New" for History List and Task List.




7) Add some sample activities to your Workflow.


I have added a LookupWorkflowContextProperty to get the current Site Url and a WriteToHistory activity which will write the Current Site Url to the History List

(Click on the Image to Zoom)

(Click on the Image to Zoom)

8) Give your App Manage Permissions on the Web:



9) Right click App Project > Publish > Package the app. A window will pop up showing you the packaged .app file.

(Click on the Image to Zoom)

10) Now the interesting part. Open the .app file as an archive with a tool of your choice.


 I have used 7-Zip which is a really awesome tool. You can download it from here: http://www.7-zip.org/

(Click on the Image to Zoom)

11) Once opened, look for the WorkflowManifest.xml file and open it. It will be empty to start with. Copy the following in the file and save it.



(Click on the Image to Zoom)

12) After Clicking save, you will get the following prompt. Click Ok. 



13) Now your .app package is ready to be deployed to your App Catalog. Go to your App Catalog and upload the .app file in the "Apps for SharePoint" document library. 



14) Once uploaded, go to your Team Site > Site Contents > Add an App > Select your app and add it to your Team Site.




15) You will be prompted to trust your App. Click on "Trust It".



16) Once the App is installed on your site, go to Site Contents > Site Workflows



17) Click on your App to see your Site Workflow deployed on the site. Click on the Workflow to start it. 

(Click on the Image to Zoom)

18) Let the workflow complete. After it has completed, click on the Internal Status to see what the workflow logged to the History List 

(Click on the Image to Zoom)

19) You can see that the Workflow logged the url of the Team Site on which it was running. 

(Click on the Image to Zoom)

This proves that the Workflow was deployed to and ran on the Host Web (Content Site).

Deploy List Workflow:


The process to deploy the list workflow is quite the same except in step 5, select List Workflow.  



Do not associate your workflow to any List. We will do the association after the deployment of the Workflow.



Select when you want your workflow to start. 



Add a Sample Workflow Activity in the Workflow to Log the Current List Name and the Current Item Url:



Steps 8 to 15 are the same as for a Site Workflow. After the Workflow is deployed, Go to a list > List Settings > Workflow Settings > Add a Workflow



Click on your App, Associate your Custom Workflow by giving it a name and configuring other settings. Click on OK.  



After you associate the Workflow, Start it either manually, or create an item, or update an item depending on the method you selected in the Workflow Designer in the App. 



After the Workflow Completes, Click on the Internal Status to see what the Workflow Logged to the History List:



Thanks for reading!

Monday, August 25, 2014

Start SharePoint 2013 Workflows with JavaScript Client Object Model

This will be a quick post to follow up on couple of my previous posts:

1) Managing SharePoint 2013 Workflows with CSOM

2) Using the SharePoint 2013 Workflow Interop Service in CSOM

In this post, I will show you basically the same thing as in the previous posts, but by using the JavaScript Client Object Model (JSOM).

I recently had a requirement for which I had to start a Nintex Site Workflow with JavaScript. Now due to my previous posts, I had an idea that we can use the Managed CSOM to manage workflows but wanted to see if the same is possible from the JavaScript CSOM.  Nintex Workflows utilize the SharePoint 2010 Workflow Engine and hence we require the Interop Service to work with them in JavaScript.

Here is the code I put together to start my Site Workflow:



You can  have a look at my previous posts and the SP.WorkflowServices.debug.js file to see how to start other workflows such as List Workflows or SharePoint 2013 Workflow Engine workflows from the JavaScript Client Object Model.


Wednesday, June 11, 2014

Improving REST API performance with JSON Light

While browsing the SharePoint 2013 SP1 change log , I came across something really interesting:

2817429​ Minimal and no metadata are now enabled as supported JSON formats.

This really caught my attention as it was something I was waiting for. When we make a call with the REST API, there is a lot of additional data which we get back in the response. This additional data consists of the metadata and the Hyper Media Controls along with the required JSON data.  To know more about this, please see this excellent post describing REST Maturity Models: http://martinfowler.com/articles/richardsonMaturityModel.html

This makes the payload size of the REST response too big. If you are developing a REST heavy application, then each extra byte of data that travels over the wire to your client adds up and ends up hampering performance.

I did some digging around and found out that my SharePoint Online Tenant was already supporting these JSON formats! All I had to do was to modify the Accept header of my REST call.

Note: As mentioned before, for these formats to work on an On-Premises SharePoint 2013, you will need to install SP1.

I ran some tests using the Chrome Developer Tools and Postman REST Client to see the effect on the payload size.

All tests were done to simply fetch the current web details with the following api call:


https://siteurl.sharepoint.com/sites/test/_api/web


1) Accept : "application/json;odata=verbose"


According to Microsoft Guidance published before, to get the JSON data back from the REST call, we need to specify the Accept header as "application/json;odata=verbose" But I think that guidance might be a little outdated now. Here is the result of the REST Call made with the above header:

Payload
Size: 2.0 KB
Content Size: 5.1 KB

    (Click on Image to Enlarge)

And here is the JSON we get back. You can see that there is a lot of metadata along with the Hypermedia Controls. 


2) Accept : "application/json;odata=minimalmetadata" (OR Accept : "application/json")


Now when optimizing for performance, we do not need all the metadata and all the Hyper Media Controls as we are only concerned with the JSON data. We can then use this header to optimize them. It will return the required JSON with very minimal metadata attached to it. This is also the default option if you only specify "application/json" in your Accept header without specifying the odata parameter.

Payload
Size: 1.5 KB
Content Size: 1.0 KB

   (Click on Image to Enlarge)

And this is the JSON returned. You can see that the Hyper Media Controls are no longer returned and only some of the metadata is returned.


3) Accept : "application/json;odata=nometadata"


This is the option for the extreme "optimizers" who want no metadata or Hypermedia Controls attached to the response and are only concerned with the JSON

Payload
Size: 1.4 KB
Content Size: 800 Bytes

   (Click on Image to Enlarge)


And this is the JSON returned:

So as you can see, if you want to reduce the payload size from the REST response, you can use the different JSON formats depending on the degree of optimization you want.

Hope you found this information useful!

Sunday, June 1, 2014

Web Development Tools & Reference

Just a list of tools and reference material which I think is really useful for Web Development:

JavaScript:


1) jslint : http://www.jslint.com/

JSLint is a JavaScript program that looks for problems in JavaScript programs. It is a code quality tool. JSLint takes a JavaScript source and scans it. If it finds a problem, it returns a message describing the problem and an approximate location within the source. The problem is not necessarily a syntax error, although it often is. JSLint looks at some style conventions as well as structural problems. It does not prove that your program is correct. It just provides another set of eyes to help spot problems.

2) jsperf : http://jsperf.com/

jsPerf aims to provide an easy way to create and share test cases, comparing the performance of different JavaScript snippets by running benchmarks.

3) jsfiddle: http://jsfiddle.net/

Test and share JavaScript, CSS, HTML or CoffeeScript online.

4) JavaScript Garden : http://bonsaiden.github.com/JavaScript-Garden/

JavaScript Garden is a growing collection of documentation about the most quirky parts of the JavaScript programming language. It gives advice to avoid common mistakes and subtle bugs, as well as performance issues and bad practices, that non-expert JavaScript programmers may encounter on their endeavours into the depths of the language.

Tools





Reference:


1) Mozilla Developer Network : https://developer.mozilla.org/en-US/


Performance:


1) Browser Diet : http://browserdiet.com/

2) Yahoo Best Practices for Speeding up your WebSite: https://developer.yahoo.com/performance/rules.html

3) Google Web Development Best Practices: https://developers.google.com/speed/docs/best-practices/rules_intro?hl=sv

4) Why you should always host jQuery on the Google CDN: http://encosia.com/3-reasons-why-you-should-let-google-host-jquery-for-you/

Saturday, May 31, 2014

Using the SharePoint 2013 Workflow Interop Service in CSOM

In my previous post, I briefly introduced the SharePoint 2013 Workflow Architecture and also showed how to programmatically manage the SharePoint 2013 Workflows via the Workflow Services in the Client Site Object Model (CSOM). I will recommend to read that post before this one.

You can find the previous post here: Managing SharePoint 2013 Workflows with CSOM

As a follow up to that post, in this post I will show how to start a Workflow authored with the SharePoint 2010 Engine.

The Workflow Services in the CSOM contain an InteropService which is a hook in the SharePoint 2010 Windows Workflow Foundation runtime engine.

This code works with SharePoint 2013 On-Premises as well as with SharePoint Online. I have created my sample code against SharePoint Online.

Before you run this code, you will need to create a SharePoint 2010 Workflow (Definition) either via SharePoint Designer 2010/2013 or through Visual Studio 2012/2013 and deploy it to your SharePoint 2013 target site. You can then use this code to start the Workflow

You will need to reference the following Assemblies:
Microsoft.SharePoint.Client.dll
Microsoft.SharePoint.Client.Runtime.dll
Microsoft.SharePoint.Client.WorkflowServices.dll

1) Start a SharePoint 2010 Site Workflow with CSOM:




2) Start a SharePoint 2010 List Workflow with CSOM:


Managing SharePoint 2013 Workflows with CSOM

As you might have heard many times, the Workflow Architecture in SharePoint 2013 was entirely changed from what it was in SharePoint 2010. Instead of the Workflows running on the SharePoint server, they now have to run on a separate Workflow Manager.

On top of that, only declarative custom Workflow's are allowed to be deployed to the Workflow Manager. If there has to be any custom code, it has to be hosted in an external service and then consumed from the declarative workflow.

The Workflow Manager has certain activities and types which come predefined with it. This is called the Trusted Surface. Workflow Authors can consume these activities in their declarative workflows. A whole list of types and activities available under the trusted surface is here: http://msdn.microsoft.com/en-us/library/jj193509(v=azure.10).aspx and here: http://msdn.microsoft.com/en-us/library/jj193474(v=azure.10).aspx

This architecture is very much inline with Microsoft's strategy to move code execution away from the SharePoint server.

SharePoint 2013 supports two methods of workflow authoring. Authoring Workflows in the SharePoint 2013 engine and also authoring them in the SharePoint 2010 engine. Workflows which are authored in the SharePoint 2010 engine run on the SharePoint server and are allowed to execute code there. That architecture has not been changed and is available for backwards compatibility.

In the SharePoint 2013 Workflow platform, a Workflow definition is a Service Bus Topic and a Workflow Association is a Subscription to the topic. Topics and Subscriptions are used by the Service Bus to decouple message publishers from message subscribers. More about the Workflow Manager and Service Bus Pub/Sub architecture here: http://msdn.microsoft.com/en-us/library/office/jj163181(v=office.15).aspx#bkm_Subscriptions

Microsoft has also very recently added Workflow Management services to the Client Side Object Model (CSOM). The entire range of Workflow services available in the CSOM is published here: http://msdn.microsoft.com/en-us/library/office/dn481315(v=office.15).aspx. In this post, and the following posts, I will show you how to manage workflows with CSOM.

This is code works with SharePoint 2013 On-Premises as well as with SharePoint Online. I have created my sample code against SharePoint Online. Also, this code only works with Workflows which are authored on the SharePoint 2013 Workflow Engine. I will be writing another post on the CSOM Interop Service in which I will show how to start SharePoint 2010 Workflow Engine authored Workflows.

Before you run this code, you will need to create a Workflow (Definition) either via SharePoint Designer 2013 or through Visual Studio 2012/2013 and deploy it to your target site. You can then use this code to dynamically create associations of the Workflow, and also to start those associations.

You will need to reference the following Assemblies:
Microsoft.SharePoint.Client.dll
Microsoft.SharePoint.Client.Runtime.dll
Microsoft.SharePoint.Client.WorkflowServices.dll


1) Create a new Subscription (Association) from a Workflow Definition with CSOM:



2) Start a SharePoint 2013 Site Workflow with CSOM:



3) Start a SharePoint 2013 List Workflow with CSOM: