InfoPath - abusing a secondary datasource as temporary variable

 

This is an InfoPath technique that I've recently started using, and is pretty awesome.

Problem:

  • InfoPath is great with rules and fields, but modifying the field means that your form has now changed.  In code behind we have formstate variables, but they are not visible by InfoPath rules.
  • We may want to change these fields in later versions of the template.
  • If only we can have temporary variables to use in InfoPath

Solution in one sentence:

  • Create a secondary datasource, use abuse it to hold variables that you don't want to save to your main form

 

Real Example

How to detect if your form has been changed by the user since it was opened.  This is a tweaked version of Alec Pojidaev's How to check if your InfoPath form is “dirty”? (Tracking changes) using secondary temporary variables.

 

Steps

  1. Create a separate secondary datasource
    image
  2. Add the XML file as a secondary datasource
    image
  3. Now we have a secondary data source that we can use
    image
  4. Use that datasource to keep all the "UI" logic.  Such as:
    • "a checkbox to confirm whether I want to save and close, or just close"
    • A rich text field to show a message that I display on a separate message View
    • Various validation or toggle Booleans to control the UI
  5. Because the secondary datasource is not saved back to the main datasource, changes to the secondary datasource tree can be considered "discarded", effectively giving us "temporary variables" that rules can use.
  6. Create the form rules
    image

  7. Set the field "Form" on the secondary data source to that of the current main datasource
    image
  8. To test if the current form, at a later point in time, is the same as the form when it was opened
    image

Demo

  1. On open, is the form the same as when we opened?
    image
  2. After some changes
    image
  3. Delete our changes - "true" again
    image

 

How to use it

  1. When a form is opened, use rules (or code) to set the default values in the secondary datasource
  2. Through the lifetime of the form you can set the values to whatever you want, and use it in conditions
  3. When the form closes it doesn't get saved

Benefits

  1. You can track UI logic without actually modifying the main form
  2. You can then use the string(.) trick to save the current form on form-load, then use that value to compare with the form at a later date to see if the form had been changed by the user
  3. Some values, like "current user displayname" can be stored temporarily and is perfect for this
  4. If you didn't like a temporary variable, just remove them or rename them.  Because they are not saved to the main data source, you don't have to worry about breaking existing or future forms.

WindowsPhone 7.5 Mango and Office 365

 

Adding an account

  1. Go to settings | email + accounts | add an account | Outlook
  2. Provide Office 365 login email and password
  3. Once setup, the account will default to the name Outlook (or Outlook #)
  4. In settings | email + accounts, tab the Outlook entry once and you’ll be able to change the name to something more meaningful, like Office 365

Office hub

  1. Now in the Office hub, you can connect to your Office 365 account. 
  2. The first time you login it will open a web browser control and ask you to login.  You can choose to remember login and password to skip this step in the future.
  3. Once authenticated, you’ll see the a view of the lists and document libraries from your Office 365 team site

For those of us keeping itchy to write our own SharePoint - WindowsPhone applications, Microsoft cheated here and the web browser control used in step 2 is accessed via COM to obtain the cookiejar file, which contains the tokens for the Office hub to talk to Office 365.

SP2010 pretty up mysite with showModalDialog

Disclaimer: Totally, unsupported.

OK, that’s out of the way, let me describe the problem. 

SharePoint 2010 ships with this pretty mysite.  Packed with features.

image

 

The problem is, your users gets lost.  It doesn’t look anything like your nice branded site.  It doesn’t share the same global navigation.  In fact, users are so lost that they think they are in a place that they shouldn’t be in. 

Result?  They close the browser.

 

If only we can render our mysite in a SharePoint 2010 showModalDialog, then it would look like this:

image

 

  • Mysite remains totally un-branded, but now it is just demoted to an utility page
  • Users are familiar with the SharePoint modal dialog, and can easily close the mysite via the top right close buttons.
  • Users don’t feel like they’ve left the site, because they can clearly see the previous page right beneath them.

 

I did a simple prototype by overriding a SharePoint javascript function:

$(document).ready(function(){

    window.oldSTSNavigate2 = window.STSNavigate2;
    window.STSNavigate2 = function (evt, Url){
        if (Url.indexOf("mysite") != -1) {
            SP.UI.ModalDialog.showModalDialog({
                url: Url + "#",
                title: "My Site",
                autoSize: true
            });
            return;
        }
        window.oldSTSNavigate2(evt, Url);
    };
});

STSNavigate2 is used by these out of the box menus:

image

Changes in SharePoint Client Object Model Redistributable SP1

 

Summary:

  • Enum Microsoft.SharePoint.Client.RecycleBinItemType.Web was added in Silverlight Client Object Model SP1.  No other significant changes noted.
  • This means old code using the previous version of the Client Object Model will work fine without recompilation.  Unless you happen to be doing stuff in the RecycleBin
  • I wish I had my evening back

 

Microsoft’s SharePoint Client Object Model Redistributables

Microsoft announced that along with the latest shiny SharePoint 2010 Service Pack 1, they are also releasing an updated SharePoint Client Object Model Redistributable SP1.  These are the libraries for .NET and Silverlight that you can use to talk to SharePoint, without having actually installed SharePoint on your machine and pulling the same DLLs from the /ClientBin/ folder.

http://support.microsoft.com/kb/2508825

image

Figure: installing the redistributable.  Note the Cancel button is where you expect Next to be :-(

Once installed, they are hiding in

C:\Program Files\Common Files\Microsoft Shared\SharePoint Client

 

Did anything actually change?  Do I have new secret goodies in the client object model?

Being the Silverlight and SharePoint fan that I am, I set about discovering what were the changes between the first RTM version of the Client Object Model vs. the Service Pack 1 version.

First thoughts were odd, but at least drove me onward:

image

Figure: RTM DLLs

 

image

Figure: SP1 DLLs

 

The striking thing was essentially, there are no differences in Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll – these are the .NET versions.

But there was a change in Microsoft.SharePoint.Client.Silverlight.dll and Microsoft.Client.Silverlight.Runtime.dll – these are the Silverlight versions.

What’s also interesting was that the changes were done in October last year and guessing from the file size differences it doesn’t look like it was a major change.

 

Undeterred, I disassembled

image

Figure: Only minor changes in most of these files.

 

The only difference that is significant:

 

image

Figure: Additional Enum Microsoft.SharePoint.Client.RecycleBinItemType.Web added to Silverlight Client Object Model library

 

This raises an interesting question – so… this enum doesn’t exist in the .NET version of the DLLs?

10 InfoPath tips for SharePoint developers

 

Here are 10 nice tips designed for someone that's familiar with SharePoint, but may be new to InfoPath. 

Having brooded over the idea for a while, I decided to quickly write this down, and if there are any questions I can expand some of these key points.

 

  1. Use InfoPath Designer 2010 to author your forms, even for 2007 forms. The UI is better, design checker gives you more information, and the rules editor supports copy/copy all and paste.
    Note: design checker gives you a lot of warnings and sometimes... they can be ignored.  Which ones are safe and which aren't... is as far as I know, a personal experience thing, think of it as VS.NET warnings.
  2. Design your main context fields first, and then try your best not to change existing fields (add new fields are OK).  Removing or renaming fields often break existing forms that had already been filled out.
  3. Decide upfront whether this is a rich form or a browser form, and set the compatibility level appropriately.  Use the design checker. 
    If you are planning to create hybrid forms that works on both - there's a form option that will allow your code behind to use the Rich Form API but still check the form for Browser Form compatibility.  In this case, always check in your code whether the form is running in the browser before you call those APIs, otherwise you will get UnsupportedExceptions.
  4. If you need to promote InfoPath fields to SharePoint so they appear as columns (and can be used in workflows), you should always use site columns.  You might want to consider always using Content Type as well. 
    It may be tempting to use publish to list - but this creates list fields that are now very hard to manage, and when you realize down the road that you should have used content types, you now have to fix existing list columns and move their data to the site columns.  This always happens
  5. Brush up on your XPath skills well.  InfoPath renders every view via a XSLT transform and the output is actually a HTML page (either for rich form or web forms).  You need to use XPath when you want to start defining rules that are relative to the current field.  Why use rules when you can use code behind?  See next point.
  6. Code behind are powerful, and may look much simpler to a developer, but has deployment considerations.  In 2010, code behind can run either in a farm solution, or via the sandbox user code service.  However, code in the sandbox service sometimes may not run when the service is "busy".  Your best bet is either: deploy code through central administration - if you have access, but then you trip up tip #4 if you haven't been using site columns, or don't use code behind and write your logic using only rules.  You can find detailed InfoPath documentation for developers on MSDN (it may not look like much, but you have functions like get-SharePointServerRootUrl that are just gems hiding)
    Plus, trust me you feel awesome when you can write complex logic using declarative XPath and no C# code. It's like saying yes I could cheat and just use C#, or I can be godlike and do it in XPath.
  7. Copying pictures increases your resource size.  The best way for a repeated picture is to include it as a resource, then use a picture button and set the image to your included resource.  Unfortunately you can't set the image of a picture control.
  8. Export form as Source files, and work with the manifest.xsf.  While I'm pretty certain it is unsupported to tweak the view.xsl files manually by hand, at least you can now put all the component files within a source control and check what has changed using a simple text difference tool.
  9. Learn how to call webservice with Rules.  InfoPath is pretty dumb beyond what's within the form.  Webservices gives you lots of capabilities but only if you know how to call them.  E.g. how to interact with SharePoint users.
  10. Don't use the "can't be blank" option, always create validation rules.  When any validation rules fail, it puts the form into an invalid state and prevents any submit action.  If you have them all defined as validation rules - you can add an additional condition that allows saving E.g. if ForceSave = "0" and "MyField" is empty
    This gives you control over what happens when the user is trying to save, and allow you to disable the checking when you need to.

 

Leave a comment if you feel this was helpful, or you want more explanation for any of the points.  Thanks for reading.