Thursday 21 August 2014

Reduce Payload of REST API calls using JSON Light in Office 365 SharePoint Online

 The REST API support provided by SharePoint 2013 has now been improved with the addition of support for JSON Light in Office 365 SharePoint Online. JSON Light is an open standard that allows options to be provided in the REST call header to limit the amount of metadata returned. If your solution doesn’t require all of the metadata then you can reduce the payload returned by your REST API calls by passing the one of the options.

The JSON Light options that are available are

verbose

This option returns complete metadata. The syntax for making the REST API call with this option is

headers: { "Accept": "application/json; odata=verbose" }

minimalmetadata

This option returns minimum amount of metadata. The  syntax for making the REST API call with this option is

headers: { "Accept": "application/json; odata=minimalmetadata" }

nometadata

This option reduces the size of the return payload significantly. The  syntax for making the REST API call with this option is

headers: { "Accept": "application/json; odata=nometadata" }

The odata property in the header is not mandatory and when not provided it defaults to minimalmetadata option.

Below are the responses with the payload size from making a REST API call to return list items from a list which has just the title column and 4 rows.

Option: verbose Response Size: 8496 bytes


 
Option: minimalmetadata Response Size: 2113 bytes


Option: nometadata Response Size: 1241 bytes
 
 

 


Monday 11 August 2014

Export SharePoint Managed Metadata Terms using CSOM and PowerShell


SharePoint 2013 out of the box has provision for importing metadata terms from a CSV file but there is no provision to export the terms that have been created. Exporting the terms can be accomplished by using Get-SPTaxonomySession powershell cmdlet. Unfortunately this cmdlet  is not available for Office 365 online so cannot be used to export metadata terms from SharePoint Online.

An alternate approach is to connect to SharePoint Online Taxonomy Session through CSOM and export the terms to a file. Below script uses CSOM and a recursive function to loop through metadata terms in SharePoint Online and write them to a file

#Specify admin user and SharePoint site URL

$User = “User Name”

$Site = "SharePoint Online URL"

#Adding references to SharePoint client assemblies

Add-Type -Path “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll”

Add-Type -Path “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll”

Add-Type -Path “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll”

 
$Pwd = Read-Host -Prompt “Enter your password” -AsSecureString


$Context = New-Object Microsoft.SharePoint.Client.ClientContext($Site)

$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Pwd)

$Context.Credentials = $Credentials

$MMS = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Context)

$Context.Load($MMS)

$Context.ExecuteQuery()

 
#Get Term Stores

$TermStores = $MMS.TermStores

$Context.Load($TermStores)

$Context.ExecuteQuery()

 
$TermStore = $TermStores[0]

$Context.Load($TermStore)

$Context.ExecuteQuery()


#Get Groups

$Groups = $TermStore.Groups

$Context.Load($Groups)

$Context.ExecuteQuery()
 

#Create the file and add headings

$OutputFile = "Output File Path"


$file = New-Object System.IO.StreamWriter($OutputFile)

$file.Writeline("Term Set Name,GUID");


Foreach ($Group in $Groups)

    {

    $Context.Load($Group)

    $Context.ExecuteQuery()
 

    $TermSets = $Group.TermSets

    $Context.Load($TermSets)

    $Context.ExecuteQuery()

    Foreach ($TermSet in $TermSets)

        {

        $file.Writeline($TermSet.Name + "," + $TermSet.Id);

   
        $Terms = $TermSet.Terms

        $Context.Load($Terms)

        $Context.ExecuteQuery()

        Foreach ($Term in $Terms)

            {

                 $file.Writeline($Term.Name + "," + $Term.Id);

                 GetTerms($Term);

            }

        }

        $file.Flush();

          $file.Close();

 
        }
  

#Recursive function to get terms

function GetTerms([Microsoft.SharePoint.Client.Taxonomy.Term] $term)

{

 $SubTerms = $term.Terms;

        $Context.Load($SubTerms)

         $Context.ExecuteQuery();

            Foreach ($SubTerm in $SubTerms)

            {

            $file.Writeline($SubTerm.Name + "," + $SubTerm.Id);

            GetTerms($SubTerm);

            }

}

Note: Update the User, Site and OutputFile variables to values matching your environment.