Karine Bosch’s Blog

On SharePoint

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()
Advertisements

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