SharePoint Saturday Sydney 2014

November 29, 2014 was a great day for SharePoint Saturday.

I presented "Develop and Build Workflow Apps in SP2013. Wait, Workflow Apps?" - which is a session that covers lots of the new things you can build with SharePoint 2013 workflows using primarily Visual Studio 2013.  These work on premises and in Office 365.

 

The REST end points opens up SharePoint

 

I still think my transition along with exploring new activity and suddenly jumping into REST was tough.  I need to work on how to introduce that point.

The key point stands.  As a developer, or even a power user using Workflows - the REST API opens SharePoint completely to me.  I can use it to create lists, site columns or assign permission groups.  The trouble is that creating the JSON packet to talk to SharePoint end point is quite hard.  Which is why the need (and the ability) to package existing series of Actions into a reusable Custom Activity is a big deal.

 

Confusion over Workflow Custom Activity

 

There was actually a lot of confusion over the artefact "Custom Activity".  In 2010, these were sandbox or coded solutions.  In 2013, Custom Activity is completely declarative.  It is a way for you to save a series of workflow actions into a reusable piece of functionality that you can use over and over.  In my demo project I have about 10 custom activities.

 

Downloads

 

 

News Update

 

We announced at the beginning and the end of SharePoint Saturday 2014 that this would be the last SharePoint Saturday in Australia.  As Microsoft and Industry trends towards Office 365, we will move with that trend.

From 2015 onwards, Office 365 Saturday will return, bigger and with more coverage of Office and SharePoint Online, but also address your On Premises needs.

Hope you have a great holidays and see you soon in 2015!

Style recommendation for writing CAML in C#

 

I came across some sample CAML code a while back, and have been following their way of defining CAML in C#

Today a colleague commented on my code and I thought, hey, I should write this down.

 

Use string.concat

 

var query = new SPQuery()
{
    Query = string.Concat(
                                "<Where>",
                                    "<Eq>",
                                    "<FieldRef Name='Parent' LookupId='true'/>",
                                    "<Value Type='Integer'>", id, "</Value>",
                                    "</Eq>",
                                "</Where>",
                                "<OrderBy>",
                                    "<FieldRef Name='DueDate' Ascending='TRUE' />",
                                    "<FieldRef Name=’Priority’ Ascending='TRUE' />",
                                "</OrderBy>")
};
var items = list.GetItems(query);

 

Benefits

 

  • You don't have to bury the CAML in one crazy line or block of text.
  • I can arbitrarily indent my XML elements and the code doesn't see it at all. 
  • You almost always need to insert some sort of variables in the middle of your CAML.  This way you don't have to rely on fancy string.format, or consider type casting.
  • You don't have to remember to add "..." + "..." at the end of every line. 
  • Don't like a chunk of XML?  You can easily line comment or region comment.

 

Try it out and see if this changes your life as it has for me.

(if you have been doing this for years and have blogged or seen this documented before, let me know where.)

And I say to ASHX for SharePoint: make me a folder.

Sometimes, I do get to do some on-premises farm stuff.  Because deep in my soul, I'm a hardcore dev.  Muahaha.

Scenario

In InfoPath, we want to be able to:

  • Send people to a folder within a document library, for them to upload attachments. 
  • Each form has its own unique ID, say "1234".  The folder will be <site>/Attachments/1234/
  • The folder doesn't need to be created when it's not used.  That is, it would be great to create the folder ON DEMAND
  • Finally, InfoPath is quite dumb.  It only has a hyperlink.

 

Solution

  • Create a HTTP Handler that takes this URL:  <site/sitecollection>/_layouts/InfoPathHelper/InfoPathHandler.ashx?folder=<site>/attachments/<ID>
  • Create a folder on demand, and then respond via a HTTP Redirect.

 

Steps

 

image

  1. Add a ashx handler to your SharePoint solution.  CKSDev has great template for this.
  2. Add code to ProcessRequest
    public void ProcessRequest(HttpContext context)
    {
        if (!string.IsNullOrEmpty(context.Request.QueryString["folder"]))
        {
            CreateFolderAndRedirectResponse(context);
            return;
        }
    }

  3. Add a function to check for the folder, create it if we need it, and end with a redirect.

    private void CreateFolderAndRedirectResponse(HttpContext context)
    {
        // <site-collection>/_layouts/InfoPathHelper/InfoPathHandler.ashx?folder=<site>/

        var path = context.Request.QueryString["folder"];
        var server = new Uri(SPContext.Current.Web.Url);
        var url = string.Format("{0}://{1}{2}", server.Scheme, server.Authority, path);

        // elevate permission to create the folder.
        SPSecurity.RunWithElevatedPrivileges(() =>
        {
            try
            {
                using (var site = new SPSite(url))
                {
                    using (var web = site.OpenWeb())
                    {
                        SPFolder folder = web.GetFolder(path);
                        SPFolder f = folder;
                        List<SPFolder> folders = new List<SPFolder>();
                        SPDocumentLibrary library = folder.DocumentLibrary;
                        if (library == null)
                        {
                            return;
                        }
                        while(f.Url.ToLower() != library.RootFolder.Url.ToLower()){
                            if (f.Exists || string.IsNullOrEmpty(f.Url))
                            {
                                break;
                            }

                            folders.Add(f);

                            f = f.ParentFolder;
                            if (f == null)
                            {
                                // if this happens we're in trouble
                                return;
                            }
                        }


                        if (folders.Count > 0)
                        {
                            // we are in a GET request - need to allow unsafe updates
                            web.AllowUnsafeUpdates = true;
                            folders.Reverse();
                            foreach (SPFolder f1 in folders)
                            {
                                if (!f1.Exists)
                                {
                                    f1.ParentFolder.SubFolders.Add(f1.Name);
                                }
                            }
                            web.AllowUnsafeUpdates = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {

            }
        });

        context.Response.Redirect(path, true);
    }

  4. Wait what's all the strange looking nested folder stuff?  That's right, as a bonus, if you specify nested folder within the document library, the HTTP Handler will create those too!

    folder=/attachments/1234/1235/
  5. InfoPath is super simple, just add a Hyperlink to the URL

    image

    Link to Data Source:

    concat("/_layouts/InfoPathHelper/InfoPathHandler.ashx?attachments?folder=", my:ID)

 

See it running

 

image

Copying SP2013 Workflow XAML files between VSNET projects

The Error

If you copy Workflow or Workflow Custom Activity objects between your SharePoint VSNET projects, sometimes you would see these errors:

Project file must include the .NET Framework assembly 'WindowsBase, PresentationCore' in the reference list.

Project file must include the .NET Framework assembly 'WindowsBase' in the reference list.

 

Why

This is actually a result of VSNET getting quite confused.  It thinks your workflow.xaml files are WPF XAML files.

image

 

The Build Action "Page" is special and tells VSNET that this is a WPF Page object.  Which then triggers the compiler requirement that the necessary libraries are not included in the project.

This can happen when you copy Workflow or Workflow Custom Activity files from one project to another, then use VSNET to "Include in Project"

image

 

The Fix

Is to tell VSNET the correct Build Action for an Workflow XAML file:

image

 

The correct Build Action for Workflow XAML objects is XamlAppDef

Go through your project and check every XAML file.

 

Result

image

 

Happy again.

SPS Canberra 2014 - Building SharePoint 2013 Workflows Apps (post-show notes)

 

On Friday afternoon I drove down to Canberra for SharePoint Saturday.  It really was a wonderful afternoon.

WP_20141017_17_36_24_Pro

 

Building SharePoint 2013 Workflow Apps

 

My talk focused on Building SharePoint 2013 Workflow Apps.  That is, practical examples of what you can build with Workflow Manager for both Office 365 and SharePoint 2013.  Lots of individual examples, Workflow Custom Activities, and a full SharePoint App with Forms, Lists and Workflows (and I found this humourous: no C# or JavaScript).

 

Questions

A question was asked about parallel tasks, the short-cut logic and whether it could be done for approval workflows that required (3 out of 5) votes to progress.  While I think that would be possible, the Out Of Box Composite Task covers that scenario directly so you don't need to build this via a tricky Parallel Task.  Much applause for Microsoft.

 

Why is REST the best thing in the slide

I didn't stress this point enough.

Traditionally, when MS adds new services or methods, we need to wait for MS (hahaha) or ask someone to build a custom activity to use them in the workflow.

Now, in SP2013 - REST is first class.  That means any new service gets a REST end point.  Want to work with Delve?  There's a REST API for that.  That means, automatically that as soon as the API is available, you can use it in your Workflow.

That means, Workflow is also first class.

That is why this is the best thing in SP2013 Workflows. 

 

Demo Fail

I was not able to add the Artezio Workflow Custom Activities in SharePoint Designer 2013.

https://sp2013workflowactivities.codeplex.com/

This is because Workflow Custom Activities can be packaged in a solution as part of "App" or "Sandbox Solution"

The activities that I had in my demo site were packaged as App.  That means they could be used in my App within the App Web, but not directly in the Host Web by SharePoint Designer.

If you are using SharePoint Designer and want to use the Workflow Custom Activities, you need to grab the sandbox solution and activate that on your site first separately.

The codeplex project has both types of solution.

https://sp2013workflowactivities.codeplex.com/releases

 

Integrated Workflow Apps

Because of time, I did not cover this point.  Integrated Workflow Apps is a variation of the Workflow App for SharePoint.  The distinction being that the workflows defined in the App (which then runs in the AppWeb) is available for Workflow Associations in the Host Web (instead of the App Web).

This is very useful if you want to build complex Approval workflows and have the logic run in the App Web, but still allow users to associate that workflow to their libraries in their own sites.

At the moment, Integrated Workflow Apps can only be created via editing the XML in the solution package.  VSNET support will be updated in the future to provide a UI for this type of set up.

 

The Sample Project is not completed

There are still plenty to do to complete the workflows, but the key points are there - flow-chart, parallel actions, scopes, workflow activity, permissions, app step.

 

Downloads