Friday, November 14, 2014

Set another user's profile properties with CSOM

So a while ago, writing User Profile properties via the CSOM was made possible. Vesa Juvonen has a great post about it here:

http://blogs.msdn.com/b/vesku/archive/2014/11/07/sharepoint-user-profile-properties-now-writable-with-csom.aspx

I have put together some code which will allow a Tenant Administrator to modify the User Profile Properties of another user in the tenant. This could be useful if a batch job has to be performed and profile properties have to be set (or updated) for all the users in a Tenant. The code is only to set the properties of one user but you can easily modify it to do the same for multiple users.

This code can also be found as a part of the Office 365 Patterns and Practices code samples on GitHub:
https://github.com/OfficeDev/PnP/tree/master/Samples/UserProfile.Manipulation.CSOM.Console

You will need the AccountName of the user whose profile properties you want to modify.  To get the AccountName of all the user's in a Tenant, you can use People search.

Some points to note with this approach:

1) You need to know the credentials of the Tenant Admin

2) I have tried using this code in a Provider Hosted App with App Only Policy but it does not seem to work. It only works in a Console Application for now.

3) This is only available in SharePoint Online for now and not SharePoint 2013 On-Premises.

4) You will need the 16.0.0.0 version of the following libraries:
 Microsoft.SharePoint.Client.dll
 Microsoft.SharePoint.Client.Runtime.dll
 Microsoft.SharePoint.Client.UserProfiles.dll

The code:

1) Set Single Value Profile Property of another user:




2) Set Multiple Value Profile Property of another user:




Monday, November 3, 2014

Writing signals into the Office Graph and Delve

Say you are developing a custom solution in SharePoint Online and you want to integrate data from the Office Graph in it. The method of reading data from the Office Graph is pretty straightforward and I have documented it in my previous post: http://www.vrdmn.com/2014/09/exploring-office-graph-and-graph-query.html

But what if you actually want to send behavior data to the Office Graph? You have a custom link to a User Profile or a document in your solution and when the user clicks the link, you want to send a "Clicked" signal to the Office Graph. If  a User Profile link of a user is clicked, you want to send a signal to increase the "closeness" of the current user to that user.

You want the user's interactions in your solution to affect the data that is stored in the Office Graph.

It is actually possible to push signals into the Office Graph from your custom application. There is actually an endpoint  _api/signalstore/signals to which you can send your custom signals.

Some things to note before we go ahead:

1) This API is not yet documented and might change in the future. It is not recommended for production solutions just as yet. 

2) This API is also throttled at a certain stage to avoid malicious use to provide false data to the Office Graph.

3) It takes about 24 hours for Delve to reflect your changes.

To see how this is done, you can open up Delve in your browser and press F12 to bring up the Developer Tools. When you navigate in various areas of Delve, you can see all the signals which are sent to the Office Graph as a result of your actions. For example, when you click a document, you can see the following request is sent:

(click to zoom)

Each signal is made of 3 properties. The Action, the Actor and the Item.

1) Actor: This is always the current user as you cannot send signals on behalf of other users.

2) Item: This is the object against which the signal is performed. This can be a document url in case of a document or it can be the login name of a user if the object is a user.

3) Action: This is the action e.g. Clicked, Shown or Elevate which is used to affect the relationship between the Actor and the Item.

I have put together couple of code samples which could be used as a base in sending custom signals to the Office Graph. These samples work right off the bat but they can be further customized by setting properties to modify the Office Graph behavior. You can find the full list of properties in your Developer Tools.

1) Send a document "Clicked" signal. This will tell the Office Graph that the current user has clicked a particular document. The Office Graph will take into account this action when calculating the closeness of the current user to this document.



2) Elevate a user with respect to the current user. This will increase the closeness and edge weight of the specified user with respect to the current user:



Hope this API will get documented soon. There is a lot of potential here for building custom solutions on top of the Office Graph and Delve.

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





4) JSON Pretty Print: http://jsonprettyprint.com/

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/