Working with folders and files via SharePoint 2013 REST in PowerShell

Overview

In the previous post we’ve already discussed how to perform CRUD operations by sending HTTPS requests to SharePoint RESTful web services in PoweShell. The Invoke-RestSPO function was introduced for that purpose since  Invoke-RestMethod cmdlet does not support claims based authentication and it makes this cmdlet impossible to use in O365 and SharePoint Online scenarios.

This time I am going to demonstrate how  to perform basic create, read, update, and delete (CRUD) operations on folders and files with the SharePoint 2013 REST interface using Invoke-RestSPO function.

Explore the REST service files and folder syntax

SharePoint 20123 Files and Folders REST syntax

 

Working with folders

Folder resource: represents a folder on a SharePoint Web site

Endpoint URI: http://<site url>/_api/web/getfolderbyserverrelativeurl(‘/<folder name>‘)

Supported HTTP methods:  GET  |  POST  |  DELETE  |  MERGE  |  PUT

The following examples demonstrates how to perform basic CRUD operations with Folder resource.

 

 

Working with files

Folder resource: represents a file in a SharePoint Web site that can be a Web Part Page, an item in a document library, or a file in a folder.

Endpoint URI: http://<site url>/_api/web/getfilebyserverrelativeurl(‘/<folder name>/<file name>‘)

Supported HTTP methods:  GET  |  DELETE  |  POST  (File resource)

The following examples demonstrates how to perform basic operations with File resource including:

  • upload file into SharePoint
  • download  file from a SharePoint

 

Summary

To summarize, it was demonstrates how to perform basic operations with files and folders, in particular how to  download and upload files via REST. For that purpose we  utilized Invoke-RestSPO function  that is intended for sending HTTPS requests to O365/SharePoint Online REST service.

References

Consuming the SharePoint 2013 REST API from PowerShell

Introduction

SharePoint 2013 introduces a Representational State Transfer (REST) service that is comparable to the  SharePoint CSOM and in addition to CSOM, REST API opens up a huge capabilities, in particular for administering and automating SharePoint Online when used with PowerShell.

Sending  REST requests to a SharePoint Online 

In the previous post we’ve already covered how to perform read operations by sending HTTPS requests to SharePoint RESTful web services. This time we are going to extend PowerShell script  in order to support all the CRUD operations.

The Invoke-RestSPO function sends  HTTPS requests to SharePoint REST web services that returns richly structured data (JSON)

Request Digests

Since SharePoint requires the user to include a request digest value with each create, update and delete operation, an additional request is invoked using Get-SPOContextInfo function to request Context Info entity that contains request digest value.

 ETag

In order to avoid an additional request, “*” eTag value is used to match any eTag value resulting in the operation being performed regardless of the actual value.

Lists manipulation using REST API in PowerShell

This section contains sample code for all of the CRUD operations.

References

 

Working with the SharePoint Online REST service via PowerShell

Since SharePoint 2013 introduces a Representational State Transfer (REST) service that is comparable to the existing SharePoint client object models, it opens up a huge capabilities, in particular for administering and automating SharePoint Online when used with PowerShell.

My first idea was to utilize  Invoke-RestMethod cmdlet, which was introduced in Windows PowerShell 3.0.  Invoke-RestMethod cmdlet contains Credential parameter which could  accept basic, digest, NTLM, and Kerberos authentication.

SharePoint Client Component SDK comes with  a SharePointOnlineCredentials class  which represents an object that provides credentials to access SharePoint Online resources. But unfortunately SharePoint Online credentials could not be passed in Invoke-RestMethod cmdlet, since claims based authentication is not supported by this cmdlet.

Below is demonstrated a simplified PowerShell script  that to some extent mimics Invoke-RestMethod cmdlet. This script is intended for sending HTTPS request to a SharePoint Online REST service:

Examples

References

Embedding and Sharing Video in SharePoint. Part Three: Aggregating Video Feeds and utilizing API

Introduction

Previous two posts were devoted to discussion of how to integrate video from YouTube and another Video Providers into SharePoint. In the first post we have discussed how to store embedded video properties and render YouTube player using Computed Field. In the second one we have discussed how to store embed code itself as it was generated by Provider.
This time we will discuss at how you can aggregate video from RSS/Atom Feeds and provide some information about using API for retrieving video content from Video Providers.

Aggregating from Video Providers RSS/Atom Feeds using YouTube

When you retrieve a video feed or list of search results, YouTube returns an Atom feed. Below is presented description of how to display videos from YouTube Feed, in our case we will display results from YouTube channel only.
For retrieving video content and displaying results  YouTube Viewer web part will be used here. In fact, it is just RSSAggregator web part, but with custom XSLT style sheet for processing YouTube feed, for details see implementation section.
So, let’s  see how to add video from YouTube feed on page:

  • Add YouTube Viewer web part which located under category Media Extensions on page
  • Specify FeedUrl for YouTube channel, for example to display video feed from Critical Path Training specify value as shown on picture
  • Result page with  YouTube web part configured is shown below

Utilizing Video Providers API

Another option to retrieve YouTube content  is to use .NET  or JavaScript client libraries directly in SharePoint.
The table below represents summary information about API for commonly used Video Providers listed in oEmbed.

Table 1. API support for Video Providers.

Provider Name API Description
YouTube Data API, Google Data client libraries for use the YouTube Data API (Java.NET, PHP, Python, Objective-C, JavaScript)
Viddler Viddler API
Qik Qik API, client for JavaScript
Vimeo Vimeo APIs

And for displaying it we need some logic to be implemented, for example by creating  custom web part. Implementation details for this functionality  are omitted here.

Implementation

For aggregating and rendering  YouTube Feeds RSSAggregator web part is used as was noted earlier with custom XSLT style sheet for processing YouTube Video Feeds.

To render YouTube feeds we provide custom  XSLT style sheet by specifying XslLink property

and the following XSLT style sheet

References

Embedding and Sharing Video in SharePoint

Introduction

The solution described here allows to embed video hosted on YouTube into SharePoint. The same approach may be applied for embedding video from another video hosting sites, for example from Vimeo.

The main idea here to store embedded video properties and not the embedded code itself.
For storing embedded video properties we will use Custom  List, see implementation section for description. Page for Video Links list (default view with embedded player)  is shown below

Usage

In order to embed a video into SharePoint:

  • On YouTube site click the Share button located under the video.
  • Click the Embed button.
  • Copy at least src attribute value provided in the expanded box (see table 1 for supported attributes).

  • Create new Video Link item  and paste attribute values for embedded code into Video Links item. Save it.

Table 1. Mapping between embedded code iframe attributes and Video Link item

Attribute name Video Links field name
src URL
width Video Width
height Video Height
allowfullscreen Allow Fullscreen
frameborder Frame Border

Video Links implementation

As was mentioned earlier only properties for embedded video code are stored and not the embedded code itself. For storing embedded video code properties is used Custom List that extends OOB Links List (TemplateType = 103)

For embedded video properties we create Video Link Content Type that inherits from Link CT (0x0105)

where we define the following fields

Pay attention, for rendering YouTube player Computed Field EmbeddedVideoOnForm is intended.
For rendering of a field on a List View the following XSLT style sheet is used

And finally, we create List Definition Video Links from Content Type Video Link. List schema and Template files for Video Links including whole project  may be found on GitHub.

References

  • Video Links project on GitHub
  • How to embed Vimeo player see here
  • How to embed YouTube player  see here
  • How to customize the rendering of a field in List View see here

Rendering Content Query Web Part results in Table Layout

It is known that OOB Content Query Web Part (CQWP)  renders results using Lists for arranging items as shown below

In ASP.NET WebForms for Web Control DataList there is a possibility to specify layout rendering mode using property RepeatLayout, what if the similar functionality would be available in CQWP?

So, our  goal to extend CQWP, i.e. in addition to List Layout rendering mode, lets implement functionality for rendering Content Query Web Part results in Plain Old Table Layout.
In this approach, we would like to achieve the following options:

  • Possibility to easily arrange results in columns
  • Specify items direction (horizontal or vertical)

Solution Structure

Content Query Web Part Class

Tool Part Class Implementation with the ability to specify Table Layout properties

Web Part manifest file

Pay attention that we provide custom XSLT for processing of the CQWP, for more details see How to: Customize XSL for the SharePoint Content By Query Web Part.

Main XSLT style sheet for rendering results in Table Layout

Results

Below is shown Web Part configured  to display results in 4 columns and horizontal mode layout

CBQTableLayoutHorizontal2

Visualizing organizational structure in SharePoint with Google Chart Tools

Sometimes in SharePoint we need to visualize hierarchical  data as organizational chart. Prerequisites: not to use  Flash or Silverlight technologies here.

So, in our scenario for storage of organization structure will be used SharePoint List based on Custom List , for rendering engine  Google Chart Tools.

Implementation

First, let’s take a look at list’s schema for storing organizational structure

Name Type Description
ParentOrgElement Lookup Used for parent/child relationship
OrgDescription Note Display text for chart box
OrgAdditionalProperties Note Visual behavior for chart box

From Google Charts library we’ll use Organizational Chart package only.

1. Google Charts Tools loading and package initialization

2. Fetch data using SharePoint Web Service from list that contains organizational structure

3. Bind data source and draw chart

Usage

Result page with Org Structure rendered as Chart in List View

 

Designing and deployment of Context Data in Nintex Workflow

Context Data in Nintex Workflow represent values specific to the workflow, the context of the item and the current task within the workflow. As an example below are some of them

Initiator: The username (domain\username) who caused the item to be entered into the workflow

Item URL: The URL of the item in workflow

The complete list of Contex Data items and how to manage it is described  in Nintex Workflow User Manual.

Designing custom Context Data usually include the implementation of specific handler(class). In Nintex Workflow API  for this purposes exist class ContextDataItemBase, from which all Context Data items usually inherits.

The implementation of Context Data for returning approvers comments in XML format is presented below

Below is shown page for managing Context Data with custom Context Data added  and Insert Reference dialog that demonstrates the usage of custom Context Data when designing workflow   

Now our goal to automate the deployment of Context Data. For example, it is possible to create Data Context item during feature activation. For this task we’ll use method of class CustomCommonDataCollection (in namespace:Nintex.Workflow) from Nintex Workflow API:

Adding Context Data item  in Nintex Workflow Management (shown below)

is equivalent to calling

You could grab the source code for the project that demonstrates how to provision Context Data here.

Automating deployment of reusable workflows in Nintex Workflow 2010

One of the options for creating workflows in Nintex Workflow 2010 (NW2010 for short) is to associate workflows  with Content Types.This category of workflows is known as Reusable workflows.

As stated in Nintex Workflow 2010 User Manual

A resusable workflow template allows the workflow to be used on a content type, list or library through the
default SharePoint Workflow settings option.
Reusable workflow templates can be created for use within a single site or an entire site collection

In our case we are only interested in reusable workflows for content types.

Let’s disscuss the scenario when we need to automate the deployment of reusable workflow. It may take place for example, when there are several environments. The approach discussed here allows to deploy workflows during feature activation.

NW2010 API contains publishing infrastucture (Nintex.Workflow.Publishing namespace) that allow to programatically publish workflow templates.
Class Publish contains  method

that allows to associate workflow using ContentTypeId parameter.

The method below demonstrate how to deploy reusable workflow file by passing it file name, contenttype name and workflow name(NWFMappingEntry parameter)  using Nintex API PublishAWorkflow method.

Project that demonstrates how to deploy Nintex reusable workflows may be found here