Karine Bosch’s Blog

On SharePoint

Format your columns with JSON

Recently, Microsoft introduced the possibility to customize the formatting of fields using JSON. Currently this is only available in SharePoint Online.

Do know that there is a difference between Column Formatting and the Field Customizer extension of the SharePoint Framework:

  • The Field Customizer is more powerful, because you can use it to write code to control how a field is displayed.
  • Column formatting is more easier but less flexible, because it does not allow for custom code; it only allows for certain predefined elements and attributes.

When you create a site columns or a field on a list or library, you can specify your custom format in the Column Formatting section of the Field settings page.

If the field already exists and you want to apply additional column formatting, you can click the arrow next to the column and choose Column settings > Format this column. The pane on the right of the page will allow you to apply your custom format:

You can use column formatting to apply styles, classes, and icons to fields, depending on the value inside those fields. Column formatting is done using JSON.

You can learn more about the JSON you can use to format fields here:

To preview the formatting, select Preview; to commit your changes, select Save. When you save, anyone who views the list will see the customization that you applied.

This is how you do it when you configure your SharePoint fields in the browser. But if you provision your SharePoint artifacts in code, you can use the new Field attribute CustomFormatter to specify your custom formatting:

<Field ID="{b75d0647-1950-46ef-b4f5-dbf0608191b7}" DisplayName="Amount" Name="Amount" 
       Type="Number" Decimals="2" Percentage="FALSE"  
       SourceID="{ca3e8e77-ebc9-4f1d-b0a8-7ea39bbd4b5e}" 
       CustomFormatter="{<your json>}" Version="1" />

Important to know is that, when a field is deployed this way, the CustomFormatter will only be applied in the modern list view; it will not be applied when the list is in classic view.

I tested this on a document set with a column that is shared with its underlying documents. I created the following content types:

  • a content type “Insurance Dossier” based on the Document set content type
  • a content type “Contract” based on the Document content type

Both content types share the same site column “Balance”.

The first picture shows the modern view:

The second picture shows the classic view (modern view is not yet possible within a document set):

 

Advertisements

November 24, 2017 Posted by | O365, SharePoint Online | Leave a comment

Making custom list forms with PowerApps

Finally, the long awaited feature to create custom list forms with PowerApps has been released last week!

Custom list forms could be handy when you are facing one of the following scenarios:

  • When you need extra validation of the input data.
  • When you need to visualize certain fields differently. For example, if you need to specify a location, you could visualize it with the Bing control.
  • When you there is a need for cascading dropdowns (list options in a second dropdown must be limited based on the selection made in a first dropdown.
  • When there is a need to hide or show certain sections on the form, based on a selected option

It is very simple to start the creation of a custom form: on the list view, select the PowerApps button and choose Customize forms:

This will open the PowerApps canvas where you can start customizing your form.

There is also a change in the List Settings to give you the possibility to switch between the default forms, InfoPath forms and PowerApps forms: go to List Settings > Forms settings:

To start with this feature, you can read the following:

https://powerapps.microsoft.com/en-us/blog/q3-2017-update/

https://docs.microsoft.com/en-us/powerapps/customize-list-form

This feature is only available in SharePoint Online, and only for lists; not (yet) for document libraries.

November 22, 2017 Posted by | SharePoint Online, PowerApps | Leave a comment

SharePoint Online – Permanent links to documents

Sometimes I get the question how you can reference a document stored on SharePoint from within a Word document, or from within CRM, without risking to end up with a broken link, even after the document is moved or renamed.

You can achieve this by activating the Document ID Service feature, which is a site collection feature. This feature already exists for a long time in SharePoint, but as this question comes up regularly, I decided to write a post about it.

This feature assigns a unique id to each document. If you now go to a document in a document library, and you click the Copy link button,

you get a URL similar to this:

https://boske.sharepoint.com/sites/projects/ABC/_layouts/15/DocIdRedir.aspx?ID=PROJECTDOC-476-47&e=586b214f0a64467cbd65c8b26c469dc1

If you rename a document, or move it, this URL still works. Also URLs as follows will work, as long as the URL is a valid URL to an existing SharePoint site, and as long as this SharePoint site has the Document ID service feature activated:

https://boske.sharepoint.com/sites/accounts/_layouts/15/DocIdRedir.aspx?ID=PROJECTDOC-476-47&e=586b214f0a64467cbd65c8b26c469dc1
https://boske.sharepoint.com/teams/group123/_layouts/15/DocIdRedir.aspx?ID=PROJECTDOC-476-47

If you look at the URL of the opened document, you will see something like this:

https://boske.sharepoint.com/:w:/r/sites/projects/ABC/_layouts/15/WopiFrame.aspx?sourcedoc={28027ac9-1ca9-4f46-98c3-ea47c9983bbe}&action=default

You can even get to the document using your OneDrive URL, (but again, the Document ID service feature needs to be activated on your OneDrive, which is a site collection):

https://boske-my.sharepoint.com/personal/kboske_com/_layouts/15/DocIdRedir.aspx?ID=PROJECTDOC-476-47

If you delete the site or the site collection afterwards, the URL will not work anymore, even if you moved the document to another location. Therefore I advise my users to use the URL of the root SharePoint site:

https://boske.sharepoint.com/_layouts/15/DocIdRedir.aspx?ID=PROJECTDOC-476-47

As long as the document exists in one of your SharePoint Online sites, you will be able to reference it this way.

For the record, in SharePoint 2016 we have durable links. You can find a good blog post on durable links in SharePoint 2016 here. As far as I know, this is different from SharePoint Online. If you know of a different way for SPO, please leave a comment.

November 21, 2017 Posted by | O365, SharePoint Online | Leave a comment

Adding Enterprise Keywords to your lists with CSOM

You can find a lot of posts that show you how to add the Enterprise Keyword to your lists and libraries:

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$ctx.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
 
$sprootweb = $ctx.Site.RootWeb
$taxKeywordField = $ctx.Site.RootWeb.Fields.GetByInternalNameOrTitle("TaxKeyword");
$list = $sprootweb.Lists.GetListByTitle("Documents");
$ctx.Load($sprootweb)
$ctx.Load($taxKeywordField)
$ctx.Load($list)
$ctx.ExecuteQuery()

$list.Fields.Add($taxKeywordField)
$ctx.ExecuteQuery()

This code snippet works fine if you have no or only one content type on your list or library. But if you have more content types attached to the list or library, only one content type is correctly modified, and it’s not always the default content type. You have to add the Enterprise Keyword column to the other content types yourself:

# get the Enterprise keyword from the list
 $field = $list.Fields.GetById($taxKeywordField.Id)
 $ctx.Load($field)
 $ctx.ExecuteQuery()

# add the Enterprise Keyword to all content types
 $cts = $list.ContentTypes
 $ctx.Load($cts)
 $ctx.ExecuteQuery()
 foreach ($ct in $cts)
 {
    # get all fields associated with the content type
    $fieldRefCollection = $ct.FieldLinks 
    $ctx.Load($fieldRefCollection)
    $ctx.ExecuteQuery()
 
    # add a FieldLink for the Enterprise Keyword
    $fieldLink = New-Object Microsoft.SharePoint.Client.FieldLinkCreationInformation 
    $fieldLink.Field = $field
    $ct.FieldLinks.Add($fieldLink)
    $ct.Update($false)
    $ctx.ExecuteQuery()
 }

And it’s the same story with the views: the Enterprise Keyword is not automatically added to the list views.

 # add the enterprise keyword field to all views
 $views = $list.Views
 $ctx.Load($views)
 $ctx.ExecuteQuery()

 foreach ($view in $views)
 {
    $view.ViewFields.Add($field)
    $view.Update()
 }
 $ctx.ExecuteQuery()

May 1, 2017 Posted by | CSOM, SharePoint 2013, SharePoint Online | Leave a comment

Creating fields using CSOM in SharePoint 2013

Recently I had a reader who asked how to to configure a calculated default value like “=YEAR([Today])”. This is not possible within one field as you cannot use [Today] or [Now] within the formula of a calculated field. The only way to solve this is to create 2 columns:

  • A DateTime field with default value Today. You can set this to hidden if you don’t want your users to see it.
  • A calculated field that outputs the year of your DateTime field.

Here is the code snippet:

private static void CreateCalculatedFieldBasedOnToday(ClientContext context)
{
     Web web = context.Web;
     List list = web.Lists.GetByTitle("CSOMFields");
     context.Load(list);
     context.ExecuteQuery();

     // Create a DateTime field that yields to Today
     string schemaTodaysDate = "<Field ID='{297B3AA2-85AD-408D-8346-0B64721C8090}' Type='DateTime' Name='TodaysDate' StaticName='TodaysDate' DisplayName='TodaysDate' Format='DateOnly' Hidden='FALSE' >"
           + "<Default>[Today]</Default></Field>";
     Field todaysDateField = list.Fields.AddFieldAsXml(schemaTodaysDate, true, AddFieldOptions.AddFieldInternalNameHint);
     context.ExecuteQuery();

     // Create a Calculated field that displays the Year of the Today field
     string formula = "<Formula>=YEAR(TodaysDate)</Formula>";
     string schemaCalculatedField = "<Field ID='{446A6933-1751-474D-A407-9EE0250C708B}' Type='Calculated' Name='TodaysYear' StaticName='TodaysYear' DisplayName='Todays Year' ResultType='Number' Decimals='0' Required='TRUE' ReadOnly='TRUE'>" + formula + "</Field>";
     Field calculatedField = list.Fields.AddFieldAsXml(schemaCalculatedField, true, AddFieldOptions.AddFieldInternalNameHint);
     context.ExecuteQuery();
}

Update: Steve Moucheron sent me his code snippet in which he solves in one go:

string fieldXml = "<Field DisplayName='Year' Type='Text'>"
   + "<DefaultFormula>=CONCATENATE(YEAR(Today))</DefaultFormula>"
   + "</Field>";
Field field = list.Fields.AddFieldAsXml(fieldXml, true, 
    AddFieldOptions.defaultValue);
context.ExecuteQuery();

Read more on creating fields using CSOM.

January 13, 2017 Posted by | CSOM, SharePoint Online | Leave a comment

Microsoft stops support for coded sandbox solutions on SharePoint Online

On July 29th Microsoft announced that it starts with the removal of coded sandbox solutions. You can read the official announcement here. This type of sandbox solutions was already deprecated since summer 2014.

A coded sandbox solution contains a DLL. Sandbox solutions containing declarative XML (f.e. the definition of site columns and content types), CSS, JS, images, etc. continue to be supported.

It is recommended to start migrating coded sandbox solutions as of today! You can find a lot of alternatives and sample code based on recommended coded practices on PnP. Additional migration guidance has been published here.

If you’re not sure if your O365 tenant is impacted by this decision, you can run a script that generates a list of sandbox solutions that are installed on your tenant. The report also displays a flag that indicates whether your sandbox solutions contains an assembly or not.

It is possible that your sandbox solutions contains nothing but CSS, JS, images, etc; but that your sandbox solution is listed as containing an assemby. It is possible that your sandbox solution contains an empty assemby. In that case you can get rid of that assembly by simply setting the Include Assembly in Package property to false. This is a property on the Visual studio project itself:

emptyassemby

This is also documented in this KB.

August 4, 2016 Posted by | O365, SharePoint Online | Leave a comment

The SharePoint Framework

Microsoft just announced the new development framework to develop against SharePoint. Don’t get worried, don’t throw your Add-ins through the windows, they are there to stay. 🙂  The SharePoint framework is just another tool in your toolbox to to ease the client-side development of pages and (web) parts. This framework will be deployed to SharePoint Online. It is not sure yet if it will be released on SharePoint 2016 on premise, but anyway not on SharePoint 2013 on premise.

You can read more about this brand new SharePoint framework here.

The framework itself is not released yet, but you can start preparing to get started: start learning TypeScript and React, and become familiar with tools like node.js, Gulp, Yeoman, and Git. Waldek Mastykarz has just posted a nice writeup on all these tools and techniques. And more details can be found on the blog of Chris O’Brien.

As a side note, I had the chance to play with a “beta” version during the DevKitchen that was organized in Brussels, a few weeks ago. The SharePoint product group came over to Brussels to give us an introduction to this very new SharePoint framework. And together with the product group, the creme of the european SharePoint community came to Brussels to participate to this event. This was just a week before the explosions, AND in the neighbourhood of one of them. I still get goose bumps when I think about what could have happened to the SharePoint community.

Keep your eyes on the net, because you will see popping up a lot of posts the coming weeks and months 🙂

Have fun!

May 5, 2016 Posted by | SharePoint Online | Leave a comment