SharePoint CRUD REST Assignment

Search for SharePoint Online Client Components SDK

Screen1.jpg

Download SharePoint Online Client Components SDK

Screen2.jpg

Launch PowerShell ISE as Administrator

Screen3.jpg

Run the code from here

Screen4.jpg

Run the code

Screen5.jpg

Open a new window

Screen6.jpg

Store your work by running Start-Transcript and note the location

Screen8.jpg

Type your code based on the exercise with your SharePoint URL and Run the selected code using F8

Screen9.jpg

Create a Garden subsite from code

Screen1.jpg

Create a List named Fruits

Screen2.jpg

Create a column named Location

Screen3.jpg

Add 5 items to the Fruits list

Screen4.jpg

Read the Fruit Details

Screen5.jpg

Update the Place for Apple from Washington to Australia

Screen6.jpg

Create a Garden subsite from code.

$data=@"
{'parameters':{'__metadata': { 'type': 'SP.WebInfoCreationInformation' },'Url': 'Garden','Title': 'Garden', 'Description': 'Garden web','Language': 1033,'WebTemplate': 'sts','UseUniquePermissions': false}}
"@
$restUrl="/_api/web/webinfos/add"
$targetSite="https://xxx.sharepoint.com/sites/dev"
$User="admin@xxx.onmicrosoft.com"
$siteNew=Create-SPObject -targetSite $targetSite -User $User -restUrl $restUrl -data $data

0.SPREST0.png

Create a List named Fruits.

$data=@"
{ '__metadata': { 'type': 'SP.List' }, 'AllowContentTypes': true, 'BaseTemplate': 100,
    'ContentTypesEnabled': true, 'Description': 'My Fruits List', 'Title': 'Fruits' }
"@
$targetSite="https://xxx.sharepoint.com/sites/dev/Garden"
$User="admin@xxx.onmicrosoft.com"
$restUrl="/_api/web/lists"
$listNew=Create-SPObject -targetSite $targetSite -User $User -restUrl $restUrl -data $data

1.SPREST1.png

Create a column named Location.

$data=@"
{ '__metadata': { 'type': 'SP.Field' }, 'Title': 'Location', 'FieldTypeKind': 2 }
"@
$listName="Fruits"
$restUrl="/_api/web/lists/getbytitle('$($listName)')/fields"
$listFieldNew=Create-SPObject -targetSite $targetSite -User $User -restUrl $restUrl -data $data

2.SPREST2.png

Add 5 items to the Fruits list.

$items="Apple","Orange","Mango","Kiwi","Pomogranate"
$location="Washington"
$listName="Fruits"
$restUrl="/_api/web/lists/GetByTitle('"+$listName+"')/items"
foreach ($item in $items)
{$data=@"
{ '__metadata': { 'type': 'SP.Data.$($listName)ListItem' }, 'Title': '$($item)','Location':'$($location)' }
"@
$listItemNew=Create-SPObject -targetSite $targetSite -User $User -restUrl $restUrl -data $data
}

3.SPREST3.png

Read the Fruit Details.

$restUrl="/_api/web/lists/getbytitle('$($listName)')/items"
$listsFruits=Read-SPObject -targetSite $targetSite -User $User -restUrl $restUrl
$results = $listsFruits.ToString().Replace("ID", "_ID") | ConvertFrom-Json
$results.d.results|select Title,Location,Id

4.SPREST4.png

Find Apple.

$restUrl="/_api/web/lists/getbytitle('$($listName)')/items?$filter=Title eq 'Apple'"
$listsFruits=Read-SPObject -targetSite $targetSite -User $User -restUrl $restUrl
$results = $listsFruits.ToString().Replace("ID", "_ID") | ConvertFrom-Json
$results.d.results|select Title,Location,Id
$etag=$results.d.results[0].__metadata.etag
$listItemID=$results.d.results[0].id

5.SPREST5.png

Update the Place for Apple from Washington to Australia.

$UpdatedValue="Australia"
$restUrl="/_api/web/lists/GetByTitle('"+$listName+"')/items("+$listItemID+")"
$data="{'__metadata': { 'type': 'SP.Data.$($listName)ListItem' }, 'Location': '$($UpdatedValue)'}"
$updatedItem=Update-SPObject -targetSite $targetSite -User $User -restUrl $restUrl -data $data -etag $etag

6.SPREST6.png

Find Mango.

$restUrl="/_api/web/lists/getbytitle('$($listName)')/items?$filter=Title eq 'Mango'"
$listsFruits=Read-SPObject -targetSite $targetSite -User $User -restUrl $restUrl
$results = $listsFruits.ToString().Replace("ID", "_ID") | ConvertFrom-Json
$results.d.results|select Title,Location,Id
$etag=$results.d.results[0].__metadata.etag
$deletelistItemID=$results.d.results[0].id

7.SPREST7.png

Delete fruit Mango.

$restUrl="/_api/web/lists/GetByTitle('"+$listName+"')/items("+$deletelistItemID+")"
$listItemdeleted=Delete-SPObject -targetSite $targetSite -User $User -restUrl $restUrl

8.SPREST8.png

Have you come here after reading/practising all the Comics?May your efforts and knowledge be rewarded!

Click Here to Send Mail to spthangu@gmail.com with your Name to get your certificate!

Sample Certificate