2018-12-11

Are you tired of writing many lines of code to perform CRUD (Create, Read. Update, Delete) operations? Let’s take a simple case of trying to read a SharePoint List and add items to it. Normally, you would have to write two functions using CSOM/JSOM/SSOM, such as:

  1. ReadList()
  2. AddListItem()

With the advent of REST in SharePoint, you can now focus your efforts on functionality rather than code/language. All you have to do is to construct the URL based on your needs and reuse the REST CRUD code.

The SharePoint team has done a lot of coding to make the functionality common across a URL.

All you need to do is, append the SharePoint site URL with REST Url.

Let’s see how.

Construction of REST Url

The REST Url begins with /_api/

After the API, you specify the Object. Example web, site. For nested objects /web/lists

Let’s begin with a simple Url that reads the Site Url.

First, you get the Site Collection Url and then append that with the Rest Url

Example:

  • https://abc.sharepoint.com/sites/dev/_api/site/Url

If you open the above Url, you would see output like below:

  • <d:Url>https://abc.sharepoint.com</d:Url>

The results can be accessed by data.d or data.d.results for multiple items

If you are new to REST, here is a quick explanation of REST basics. Teh REST technology allows developers to write functions using any technology of their choice and publish that as a URL which does the following tasks for a resource/object:

  • GET- Read the object and returns the results mostly in JSON/XML format
  • POST- Get the data details and Add Items
  • PUT – Update the Object
  • DELETE – Delete the Object

How to use PowerShell with SharePoint to code against the REST API

Let’s use PowerShell to get connected to SharePoint URL and do various Read operations. The cool thing is there are only one PowerShell code and the URL alone changes. In regular CSOM coding, you need to remember the Class and the methods to get your job done. In REST, the code remains the same and the URL alone changes.

You can download the code from here.

Here is a quick summary of the PowerShell code to do the CRUD operations.

  1. Get connected to SharePoint using SharePoint credentials
  2. Add the necessary headers to $headers based on CRUD operation
    1. GET- “Accept”, “application/json;odata=verbose”
    2. POST- “X-RequestDigest”, $formDigest from /_api/contextinfo
    3. PUT- “X-HTTP-Method”, “PUT” or “MERGE”
    4. PUT- “X-HTTP-Method”, “DELETE”
  3. Invoke-RestMethod -Uri $fullUrl -Headers $headers -Method Post -WebSession $webSession -ContentType “application/json;odata=verbose”

Once, you run the above code, you can start using Read-SPObject,Create-SPObject,Update-SPObject on various SharePoint objects like Site, Lists,Files and many more based on your needs.

Add List Items using REST

Let’s start with our original need to read a SharePoint List and add Items to a SharePoint list say PSRestList.

$targetSite="https://xxx.sharepoint.com/sites/dev"
$User="[email protected]"
$restUrl="/_api/web/lists/getbytitle('PSRestList')"
$listsPSRest=Read-SPObject -targetSite $targetSite -User $User -restUrl

Below are the details needed to add a item to a SharePoint list. Note that the data part contains the List Name, List Columns like Title.

#Add List Item
$listName="PSRestList"
[email protected]"
{ '__metadata': { 'type': 'SP.Data.$($listName)ListItem' }, 'Title': 'OneTwoThree','CurrencyThangu':'2500' }
"@
$restUrl="/_api/web/lists/GetByTitle('"+$listName+"')/items"
$listItemNew=Create-SPObject -targetSite $targetSite -User $User -restUrl $restUrl -data $data

Summary

As a quick summary, you learnt the following:

  1. Basics of REST and its benefits
  2. Construction of SharePoint REST Url
  3. PowerShell code that Invokes the SharePoint URL
  4. Adding a SharePoint List Item using the REST Url.

If you like to learn by watching videos, here is a sample REST demo.

(Visited 394 times, 1 visits today)

About the author 

Thangu Thangu

I love exploring SharePoint and talking about SharePoint. Few of my contributions can be viewed here https://www.youtube.com/channel/UCGqSWCnFPZ4GuPEwjtCYx1A/videos?sort=dd&view=0&shelf_id=0

Leave a Reply

  1. Jeff, thanks for mentioning PnP. There are various ways to do CRUD in SharePoint like CSOM,JSOM,REST,PnP. I am exploring all options and let the readers or developers choose what they are comfortable with based on their skills. I vote for CSOM and PnP personally. Here are top reasons I started off with REST CRUD here:

    1. I received requests to learn about REST in SharePoint and felt that REST using PowerShell is the easiest way to begin. Once REST Url construction is clear, you can use REST Url to write applications in a SharePoint Page or other application(after authentication) or SharePoint Framework app.
    2. Options to write code which are not working always in PnP:
    Add-PnPListItem fails sometimes and I have to use Set-PnPListItem to add item to a list.
    I will blog on PnP too soon, hopefully.

  2. I think pnp covers almost all, I was hopping this to be useful for cross-platform (powershell core; linux/mac) query, but seems like it depends on the same dlls than pnppowershell, right?

    1. PnP uses CSOM mostly behind the scenes.I use CSOM for advanced things like adding new Language label in Taxonomy. PnP is more than enough. Understanding REST Url construction is helpful if you are developing apps for mobile or SPFx apps. This PowerShell REST can be very useful when you do not have to worry about Authentication/Authorisation using OAuth and focus on getting the functionality done. I was frustrated with Postman Authentication and hence wrote this code.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

2 Free Ebooks: How to govern Microsoft teams 

Download your 2 free Microsoft Teams governance Ebooks and learn the steps necessary to create a bullet-proof governance strategy.