Powershell Scripts
Powershell Scripts
Powershell Scripts
Vijai Anand
Author
Sam Hobbs
Editor, C# Corner
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Vijai is a frequent contributor to C# Corner (www.c-sharpcorner.com). He has authored around 500 articles
and 400 blogs on www.c-sharpcorner.com for SharePoint 2013, SharePoint 2010, SharePoint Workspace,
SharePoint Designer 2010, Powershell, C # and Silverlight. He currently holds Microsoft Most Valuable
Professional and Mindcracker Most Valuable Professional award for SharePoint Server.
He has authored the following eBooks:
SharePoint 2013 .Net Client Side Object Model Cookbook that was published in CSHARPCORNER.com
Getting Started with Managed Metadata Service in SharePoint 2010 that was published in
CSHARPCORNER.com
Business Data Connectivity Services - Step by Step tutorial that was published in ITFUNDA.com
Microsoft SharePoint 2013, Developing Microsoft SharePoint Server 2013 Core Solutions
Microsoft SharePoint 2010, Designing and Developing Microsoft SharePoint 2010 Applications.
Acknowledgments
I am really thankful to each and every one that has motivated me to write articles and to publish my fourth
eBook.
I would like to express my special thanks to Mahesh Chand (Microsoft MVP, Founder of Mindcracker
Networks) and to the entire CSharpcorner team for motivating me to publish my third eBook. Thanks to all the
reviewers for reviewing my eBook.
I would like to express my thanks to all my colleagues and Architects who supported me in writing this book.
Thanks to all my friends who helped me to publish this eBook.
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
TABLE OF CONTENTS
1 SharePoint 2013 Online Management Shell: an Overview ...................................................................... 8
2 Prerequisites ............................................................................................................................................ 8
3 Perform SharePoint list tasks using CSOM in Powershell script .............................................................. 9
3.1
3.2
3.3
3.4
3.5
How to enable folder creation for the list in the website ............................................................ 16
3.6
3.7
3.8
3.9
3.10 How to enable Require Check Out for the document library ...................................................... 27
3.11 How to enable content approval for the list ................................................................................ 29
3.12 How to specify the permission required to view minor versions and drafts within the list ........ 31
3.13 How to get all the list templates available for creating lists ........................................................ 33
4 Perform SharePoint website tasks using CSOM in Powershell script .................................................... 36
4.1
4.2
4.3
4.4
5 Perform SharePoint list item tasks using CSOM in Powershell script .................................................... 44
5.1
5.2
5.3
5.4
5.5
5.6
How to get all the attachments for the list item .......................................................................... 54
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
5.7
6 Perform SharePoint content type tasks using CSOM in Powershell script ............................................ 58
6.1
How to get all the content types from the website ..................................................................... 58
6.2
6.3
6.4
6.5
How to get all the content types from the list ............................................................................. 66
6.6
6.7
7.2
7.3
7.4
7.5
7.6
How to set the default value for the list field .............................................................................. 81
7.7
7.8
8 Perform SharePoint list view tasks using CSOM in Powershell script .................................................... 88
8.1
8.2
How to get all the fields available in the list view ........................................................................ 90
8.3
8.4
8.5
8.6
How to get all the top level folders from the website ................................................................. 99
9.2
How to get all the top level folders from the list ....................................................................... 101
9.3
9.4
9.5
9.6
How to get the number of items inside the folder .................................................................... 109
10 Perform SharePoint file tasks using CSOM in Powershell script .......................................................... 111
10.1 How to get the major version of the file .................................................................................... 111
10.2 How to get the minor version of the file .................................................................................... 113
10.3 How to check out the file in the document library .................................................................... 115
10.4 How to get the user login name that has checked out the file .................................................. 117
10.5 How to get the user login name who added the file.................................................................. 119
10.6 How to get the check out type associated with the file ............................................................. 121
10.7 How to check in the file .............................................................................................................. 123
10.8 How to get the check in comment of the file............................................................................. 125
10.9 How to unpublish the major version of the file ......................................................................... 127
10.10 How to discard check out of the file ........................................................................................... 128
10.11 How to delete the file from the document library ..................................................................... 130
11 Perform SharePoint file version tasks using CSOM in Powershell script ............................................. 132
11.1 How to get all the versions for the file ....................................................................................... 132
11.2 How to get the file version for the document by version Id ...................................................... 134
11.3 How to delete a file version by version ID for the document .................................................... 137
11.4 How to delete a file version by version label for the document ................................................ 138
11.5 How to restore a specific file version for the document............................................................ 140
11.6 How to check if the file version is a current version for the document ..................................... 142
11.7 How to delete all the file versions for the document ................................................................ 144
12 Perform SharePoint group tasks using CSOM in Powershell script ..................................................... 146
12.1 How to get all the site groups .................................................................................................... 146
12.2 How to create a new site group ................................................................................................. 148
12.3 How to set the user as owner for the site group ....................................................................... 150
12.4 How to set the group as owner for the site group ..................................................................... 152
12.5 How to get all the users from the site group ............................................................................. 154
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
SharePoint Online Management Shell includes a set of cmdlets. Nearly 30 cmdlets are provided to manage users,
sites, and organizations instead of using the SharePoint Online Administration Center. But this covers only the
basic operations whereas for SharePoint 2013 On-Premise there are nearly 780 cmdlets that can be extensively
used to perform most of the SharePoint tasks. To know more about the cmdlets available for SharePoint Online
refer to http://technet.microsoft.com/en-us/library/fp161364(v=office.15).aspx. To overcome this we could use
Powershell using the Client Side Object Model (CSOM) that enables running scripts against SharePoint Online
remotely and it can be used in the same way that we are accustomed to (on-premises SharePoint). The scripts
created using the Client Side Object Model can be reused for SharePoint 2013 On-Premise also.
2 Prerequisites
In this section you will see the prerequisites required to create the Powershell script using Client Side Object
Model to run against SharePoint Online remotely.
The following are the prerequisites required:
1.
Make sure that you have installed Windows PowerShell 3.0. If you do not have PowerShell 3.0, you will
need to download the Windows Management Framework 3.0.
2.
You will need to install the SharePoint Online Management Shell, that can be downloaded from
the Microsoft Download Center.
3.
Make sure SharePoint Client Runtime assemblies are available and this can be downloaded here.
4.
Authentication: You can connect to SharePoint Online using the new SharePointOnlineCredentials
class.
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Note: I am using the cloudshare environment (development environments) where all the above prerequisites are
available to execute the Powershell scipt using Client Side Object Model against SharePoint 2013 Online.
Thus in this section you have seen the prerequisites required to create the Powershell script using the Client Side
Object Model.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
$url = "https://c986.sharepoint.com/"
### References
# Specify the path where the dll's are located.
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"
### Function
function GetAllLists()
{
# Connect to SharePoint Online and get ClientContext object.
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
# Get the SharePoint Web
$web=$clientContext.Web;
# Get all the lists
$listColl=$web.Lists;
$clientContext.Load($listColl);
# Execute the query
$clientContext.ExecuteQuery();
# Loop through all the lists
foreach($list in $listColl)
{
# Display the list name and ID
write-host -ForegroundColor Green "List Name: " $list.Title " ID: " $list.Id
}
}
### Calling the function
GetAllLists
Result
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
### References
# Specify the path where the dll's are located.
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"
### Function
function CreateList()
{
# Connect to SharePoint Online and get ClientContext object.
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
# Specifies the properties of the new custom list
$creationInfo= New-Object Microsoft.SharePoint.Client.ListCreationInformation;
$creationInfo.Title="CSOM List";
$creationInfo.Description="CSOM custom list created using Powershell";
$creationInfo.TemplateType=[int][Microsoft.SharePoint.Client.ListTemplateType]::Gener
icList
# Create a new custom list
$newList=$clientContext.Web.Lists.Add($creationInfo);
$clientContext.Load($newList);
# Execute the query
$clientContext.ExecuteQuery();
# Display the newly created list and ID
write-host -ForegroundColor Green "List Name: " $newList.Title " ID: "
$newList.Id
}
### Calling the function
CreateList
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
In this example you will see how to update a list in the website using the .Net Client Side Object Model in
Powershell scripts.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
# Get the list by Title
$list=$clientContext.Web.Lists.GetByTitle("Employee Details");
# Update the list description
$list.Description="Employee Details list updated using Powershell";
$list.Update();
$clientContext.Load($list);
Result
3.5 How to enable folder creation for the list in the website
In this example you will see how to enable folder creation for the list in the website using the .Net Client Side
Object Model in Powershell scripts.
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
d)
Type cd c:\Vijai in the the management shell and then click on Enter.
e)
Type .\VijaiAnand_CSOM_Powershell_Office365.ps1 in the the management shell and then click on Enter.
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the the
Credentials pop up. Click on Ok.
Script
Result
Navigate to the SharePoint site. Click on the Employee Details link in the quick launch bar. Click on the List
Settings button in the ribbon interface. Click on the Advance Settings link available in the General Settings
section. You will see that the folder creation for the list is enabled successfully.
Reference
http://msdn.microsoft.com/en-IN/library/microsoft.sharepoint.client.list.enablefoldercreation.aspx
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Source Code
$list=$clientContext.Web.Lists.GetByTitle("Employee Details");
# Disable the attachments for the list
$list.EnableAttachments=$false;
# Update the list
$list.Update();
# Execute the query
$clientContext.ExecuteQuery();
}
### Calling the function
DisableAttachments
Result
Navigate to the SharePoint site. Click on the Employee Details link in the quick launch bar. Click on the List
Settings button in the ribbon interface. Click on the Advance Settings link available in the General Settings
section. You will see the attachments to the list items disabled successfully.
Reference
http://msdn.microsoft.com/en-IN/library/microsoft.sharepoint.client.list.enableattachments.aspx
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
$list=$clientContext.Web.Lists.GetByTitle("Employee Details");
# Display the list on the quick launch
$list.OnQuickLaunch=$true;
# Update the list
$list.Update();
# Execute the query
$clientContext.ExecuteQuery();
}
### Calling the function
OnQuickLaunch
Result
Navigate to the SharePoint site. Click on the Employee Details link in the quick launch bar. Click on the List
Settings button in the ribbon interface. Click on the List name, description and navigation link available under
the General Settings section. You will see the option to display the list in the quick launch is enabled
successfully.
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Reference
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.list.onquicklaunch.aspx
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
Result
Navigate to the SharePoint site. Click on the Employee Details link in the quick launch bar. Click on the List
Settings button in the ribbon interface. Click on the Version Settings link available under the Settings section.
You will see versioning settings for the list is enabled successfully.
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Reference
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.list.enableversioning.aspx
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
Result
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Navigate to the SharePoint site. Click on the Documents link in the quick launch bar. Click on the Library
Settings button in the ribbon interface. Click on the Versioning Settings link available under the Settings
section. You will see minor versions for the document library is enabled successfully.
Reference
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.list.enableminorversions.aspx
3.10 How to enable Require Check Out for the document library
In this example you will see how to enable Require Check Out for the document library using the .Net Client Side
Object Model in Powershell scripts.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Script
}
### Calling the function
ForceCheckOut
Result
Navigate to the SharePoint site. Click on the Documents link in the quick launch bar. Click on the Library
Settings button in the ribbon interface. Click on the Versioning Settings link available under the Settings
section. You will see Require Check Out for the document library is enabled successfully.
Reference
http://msdn.microsoft.com/enus/library/microsoft.sharepoint.client.list.forcecheckout%28v=office.14%29.aspx
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
Navigate to the SharePoint site. Click on the Documents link in the quick launch bar. Click on the Library
Settings button in the ribbon interface. Click on the Versioning Settings link available under the General
Settings section.
Reference
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.list.draftversionvisibility.aspx
3.12 How to specify the permission required to view minor versions and drafts
within the list
In this example you will see how to specify the permission required viewing minor versions and drafts within the
list using the .Net Client Side Object Model in Powershell scripts.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
$dl.Update();
# Execute the query
$clientContext.ExecuteQuery();
}
### Calling the function
DraftVersionVisibility
Result
Navigate to the SharePoint site. Click on the Documents link in the quick launch bar. Click on the Library
Settings button in the ribbon interface. Click on the Versioning Settings link available under the General
Settings section.
Figure3.13.1: Specify which users should be able to view drafts in this list
Reference
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.list.draftversionvisibility.aspx
3.13 How to get all the list templates available for creating lists
In this example you will see how to get all the list templates available for creating lists using the .Net Client Side
Object Model in Powershell scripts.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
Result
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
Result
In this example you will see how to update the website properties using the .Net Client Side Object Model in
Powershell scripts.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
# Get the SharePoint web
$web=$clientContext.Web;
# Update the web title and description
$web.Title="CSOM Online";
$web.Description="Updated using Powershell";
$clientContext.Load($web);
# Execute the query
$clientContext.ExecuteQuery();
# Display the web properties
Write-Host -ForegroundColor Green "Title: " $web.Title " - Description: "
$web.Description
}
### Calling the function
UpdateWeb
Result
b)
c)
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
### Function
function GetSpecificProperties()
{
# Connect to SharePoint Online and get ClientContext object.
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$ctx.Credentials = $credentials
$referencedAssemblies = (
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
# Loop through all the items and display the title field
foreach($item in $itemColl)
{
Write-Host -ForegroundColor Green $item["Title"]
}
}
### Calling the function
GetListItems
Result
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Script
$item["Title"]
}
### Calling the function
UpdateItem
Result
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
$clientContext.ExecuteQuery();
}
### Calling the function
DeleteItem
Result
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
5.6 How to get all the attachments for the list item
In this example you will see how to get all the attachments for the list item using the .Net Client Side Object
Model in Powershell scripts.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
$url = "https://c986.sharepoint.com/"
### References
# Specify the path where the dll's are located.
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"
### Function
function DeleteAttachment()
{
# Connect to SharePoint Online and get ClientContext object.
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
# Get the list by title
$list=$clientContext.Web.Lists.GetByTitle("Custom List")
# Get the item by ID
$item=$list.GetItemById(1);
# Get the attachment by file name
$attach=$item.AttachmentFiles.GetByFileName("Lighthouse.jpg");
# Delete the attachment
$attach.DeleteObject();
# Execute the query
$clientContext.ExecuteQuery();
}
### Calling the function
DeleteAttachment
Result
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
6.1 How to get all the content types from the website
In this example you will see how to get all the content types from the website using the .Net Client Side Object
Model in Powershell scripts.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
Navigate to the SharePoint site. Click on Settings. Click on Site Settings. Click on Content Types available
under the Galleries section. You will see a newly created content type under the Vijai Content Types group as
shown in Figure 6.2.1.
b)
c)
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
The site content type is deleted successfully.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
Navigate to the SharePoint site. Click on Settings. Click on Site Settings. Click on Content Types available
under the Galleries section. Click on Vijai Content Type available under the Vijai Content Types group. You
will see the content type is set as read only as shown in Figure 6.4.1.
Reference
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.contenttype.readonly.aspx
6.5 How to get all the content types from the list
In this example you will see how to get all the content types from the list using the .Net Client Side Object Model
in Powershell scripts.
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
$web=$clientContext.Web;
# Get the custom list by title
$list=$web.Lists.GetByTitle("Employee Details");
# Get all the content types from the custom list
$ctColl=$list.ContentTypes;
$clientContext.Load($ctColl);
# Execute the query
$clientContext.ExecuteQuery();
# Display all the list content types
foreach($ct in $ctColl)
{
write-host -ForegroundColor Green $ct.Name
}
}
### Calling the function
GetListContentTypes
Result
Figure 6.5.1: Get all the content types from the list
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
$web=$clientContext.Web;
# Get the custom list by title
$list=$web.Lists.GetByTitle("Employee Details");
# Get all the content types from the custom list
$ctColl=$list.ContentTypes;
$clientContext.Load($ctColl);
# Execute the query
$clientContext.ExecuteQuery();
# Loop through all the content types
foreach($ct in $ctColl)
{
# Delete the content type from the list
if($ct.Name -eq "Vijai Content Type")
{
$ct.DeleteObject();
break;
}
}
# Execute the query
$clientContext.ExecuteQuery();
}
### Calling the function
DeleteListContentType
Result
Content type is deleted successfully from the list.
b)
c)
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
Result
Content type is added successfully to the list.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
$clientContext.Load($fieldColl);
# Execute the query
$clientContext.ExecuteQuery();
# Loop through all the fields
foreach($field in $fieldColl)
{
# Display the field title and ID
Write-Host -ForegroundColor Green "Field Name: " $field.Title " ID: "
$field.ID
}
}
### Calling the function
GetListFields
Result
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
Navigate to the SharePoint site. Click on the Employee Details link available in the quick launch bar. Click on
List Settings in the ribbon interface. You will see a new field added to the list available under the Columns
section.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
$url = "https://c986.sharepoint.com/"
### References
# Specify the path where the dll's are located.
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"
### Function
function AddExistingField()
{
# Connect to SharePoint Online and get ClientContext object.
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
# Get the SharePoint web
$web=$clientContext.Web;
# Get the custom list by Title
$list=$web.Lists.GetByTitle("Employee Details");
# Get a specific field bt title from site columns
$field=$web.Fields.GetByTitle("MultiChoice");
# Add the existing field to the list
$list.Fields.Add($field);
# Execute the query
$clientContext.ExecuteQuery();
}
### Calling the function
AddExistingField
Result
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Navigate to the SharePoint site. Click on the Employee Details link available in the quick launch bar. Click on
List Settings in the ribbon interface. You will see a new field added to the list available under the Columns
section.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
### Function
function DeleteListField()
{
# Connect to SharePoint Online and get ClientContext object.
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
# Get the SharePoint web
$web=$clientContext.Web;
# Get the custom list by Title
$list=$web.Lists.GetByTitle("Employee Details");
# Get a specific field bt title
$field=$list.Fields.GetByTitle("MultiChoice");
# Delete the field from the list
$field.DeleteObject();;
# Execute the query
$clientContext.ExecuteQuery();
}
### Calling the function
DeleteListField
Result
Navigate to the SharePoint site. Click on the Employee Details link available in the quick launch bar. Click on
List Settings in the ribbon interface. You will see a field is deleted from the list available under the Columns
section.
7.6 How to set the default value for the list field
In this example you will see how to set the default value for the list field using the .Net Client Side Object Model
in Powershell scripts.
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
Result
Navigate to the SharePoint site. Click on the Employee Details link available in the quick launch bar. Click on
List Settings in the ribbon interface. Click on the CustomField available under the Columns section. You will see
a default value is set for the field.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
Result
c)
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
$list=$web.Lists.GetByTitle("Employee Details");
# Get a specific field bt title
$field=$list.Fields.GetByTitle("Calculated");
# Cast the field
$calculatedField=New-Object
Microsoft.SharePoint.Client.FieldCalculated($clientContext,$field.Path);
# Set the formula for the calculated value
$calculatedField.Formula="=[Employee ID]";
# Update the field
$calculatedField.Update();
# Execute the query
$clientContext.ExecuteQuery();
}
### Calling the function
SetCalculatedFieldFormula
Result
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
### Function
function GetListViews()
{
# Connect to SharePoint Online and get ClientContext object.
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
# Get the SharePoint web
$web=$clientContext.Web;
# Get the custom list by Title
$list=$web.Lists.GetByTitle("Employee Details");
# Get all the views for the custom list
$viewColl=$list.Views;
$clientContext.Load($viewColl);
# Execute the query
$clientContext.ExecuteQuery();
# Loop through all the views
foreach($view in $viewColl)
{
# Display the view name
write-host -ForegroundColor Green $view.Title
}
}
### Calling the function
GetListViews
Result
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
7.10 How to get all the fields available in the list view
In this example you will see how to get all the fields available in the list view using the .Net Client Side Object
Model in Powershell scripts.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
SetDefaultView
Result
Vijai View view is set as the default view for the Employee Details list.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
Result
In this example you will see how to delete a field from the list view using the Client Side Object Model in
Powershell scripts.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
# Get the SharePoint web
$web=$clientContext.Web;
# Get the custom list by Title
$list=$web.Lists.GetByTitle("Employee Details");
# Get a specific list view by title
$view=$list.Views.getByTitle("Vijai View");
# Remove the field from the list view
$view.ViewFields.Remove("Department");
# Update the view
$view.Update();
# Execute the query
$clientContext.ExecuteQuery();
}
### Calling the function
RemoveViewField
Result
Department field is removed successfully from the Vijai View list view.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
$view=$list.Views.getByTitle("Vijai View");
# Delete the list view
$view.DeleteObject();
# Execute the query
$clientContext.ExecuteQuery();
}
### Calling the function
DeleteView
Result
Vijai View list view is deleted successfully from the custom list.
8.1 How to get all the top level folders from the website
In this example you will see how to get all the top level folders from the website using the .Net Client Side
Object Model in Powershell scripts.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
8.2 How to get all the top level folders from the list
In this example you will see how to get all the top level folders from the list using the .Net Client Side Object
Model in Powershell scripts.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
$clientContext.Load($folderColl);
# Execute the query
$clientContext.ExecuteQuery();
# Loop through all the folders
foreach($folder in $folderColl)
{
# Display the folder name
Write-Host -ForegroundColor Green $folder.Name
}
}
### Calling the function
GetFoldersFromList
Result
Figure9.2.1: Get all the top level folders from the list
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
Result
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
$folder.DeleteObject();
# Execute the query
$clientContext.ExecuteQuery();
}
### Calling the function
DeleteFolder
Result
Folder at the specified server relative URL is deleted successfully.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
$url = "https://c986.sharepoint.com/"
### References
# Specify the path where the dll's are located.
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"
### Function
function CreateFolder()
{
# Connect to SharePoint Online and get ClientContext object.
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
# Get the SharePoint web
$web=$clientContext.Web;
# Adds the folder that is located at the specified URL to the collection
# Create a new folder in SharePoint Documents
$folder=$web.Folders.Add("Shared Documents/FolderC");
$clientContext.Load($folder);
# Execute the query
$clientContext.ExecuteQuery();
# Display the folder name and URL
Write-Host -ForegroundColor Green "Folder Name: " $folder.Name " URL: "
$folder.ServerRelativeUrl;
}
### Calling the function
CreateFolder
Result
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Reference
http://msdn.microsoft.com/en-IN/library/microsoft.sharepoint.client.file.majorversion.aspx
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
Reference
http://msdn.microsoft.com/en-IN/library/microsoft.sharepoint.client.file.minorversion.aspx
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
$credential=Get-Credential
$username=$credential.UserName
$password=$credential.GetNetworkCredential().Password
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
### Input Parameters
$url = "https://c986.sharepoint.com/"
### References
# Specify the path where the dll's are located.
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"
### Function
function FileCheckOut()
{
# Connect to SharePoint Online and get ClientContext object.
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
# Get the SharePoint web
$web=$clientContext.Web;
# Get the list by title
$list=$web.Lists.GetByTitle("Documents");
# Get an item by ID
$item=$list.GetItemById(19);
# Get the file that is represented by the item from a document library
$file=$item.File;
# Check out the file
$file.CheckOut()
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
http://msdn.microsoft.com/en-IN/library/microsoft.sharepoint.client.file.checkout.aspx
9.4 How to get the user login name that has checked out the file
In this example you will see how to get the user login name that has checked out the file using the .Net Client
Side Object Model in Powershell scripts.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
Reference
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.file.checkedoutbyuser.aspx
9.5 How to get the user login name who added the file
In this example you will see how to get the user login name that added the file using the .Net Client Side Object
Model in Powershell scripts.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
Result
Reference
http://msdn.microsoft.com/en-IN/library/microsoft.sharepoint.client.file.author.aspx
9.6 How to get the check out type associated with the file
In this example you will see how to get the check out type associated with the file using the .Net Client Side
Object Model in Powershell scripts.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
Reference
http://msdn.microsoft.com/en-IN/library/microsoft.sharepoint.client.file.checkouttype.aspx
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
### References
# Specify the path where the dll's are located.
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"
### Function
function FileCheckIn()
{
# Connect to SharePoint Online and get ClientContext object.
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
# Get the SharePoint web
$web=$clientContext.Web;
# Get the list by title
$list=$web.Lists.GetByTitle("Documents");
# Get an item by ID
$item=$list.GetItemById(19);
# Get the file that is represented by the item from a document library
$file=$item.File;
# Check in the file
$file.CheckIn("Checked in using powershell",
[Microsoft.SharePoint.Client.CheckInType]::MajorCheckIn);
# Execute the query
$clientContext.ExecuteQuery();
}
### Calling the function
FileCheckIn
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
http://msdn.microsoft.com/en-IN/library/microsoft.sharepoint.client.file.checkin.aspx
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
{
# Connect to SharePoint Online and get ClientContext object.
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
# Get the SharePoint web
$web=$clientContext.Web;
# Get the list by title
$list=$web.Lists.GetByTitle("Documents");
# Get an item by ID
$item=$list.GetItemById(19);
# Get the file that is represented by the item from a document library
$file=$item.File;
$clientContext.Load($file);
# Execute the query
$clientContext.ExecuteQuery();
# Display the file check in comment
Write-Host -ForegroundColor Green "File Check In Comment: " $file.CheckInComment
}
### Calling the function
GetCheckInComment
Result
Reference
http://msdn.microsoft.com/en-IN/library/microsoft.sharepoint.client.file.checkincomment.aspx
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
### Function
function UnPublishFile()
{
# Connect to SharePoint Online and get ClientContext object.
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
# Get the SharePoint web
$web=$clientContext.Web;
# Get the list by title
$list=$web.Lists.GetByTitle("Documents");
# Get an item by ID
$item=$list.GetItemById(19);
# Get the file that is represented by the item from a document library
$file=$item.File;
$file.UnPublish(" Unpublishing the major version using powershell");
# Execute the query
$clientContext.ExecuteQuery();
}
### Calling the function
UnPublishFile
Result
http://msdn.microsoft.com/en-IN/library/microsoft.sharepoint.client.file.unpublish.aspx
In this example you will see how to discard the check out of the file using the .Net Client Side Object Model in
Powershell scripts.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
# Get the SharePoint web
$web=$clientContext.Web;
# Get the list by title
$list=$web.Lists.GetByTitle("Documents");
# Get an item by ID
$item=$list.GetItemById(19);
# Get the file that is represented by the item from a document library
$file=$item.File;
# Discard check out
$file.UndoCheckOut();
# Execute the query
$clientContext.ExecuteQuery();
}
### Calling the function
DiscardCheckOut
Reference
http://msdn.microsoft.com/en-IN/library/microsoft.sharepoint.client.file.undocheckout.aspx
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
# Get an item by ID
$item=$list.GetItemById(19);
# Get the file that is represented by the item from a document library
$file=$item.File;
# Delete the file
$file.DeleteObject();
# Execute the query
$clientContext.ExecuteQuery();
}
### Calling the function
DeleteFile
Result
http://msdn.microsoft.com/en-IN/library/microsoft.sharepoint.client.file.deleteobject.aspx
b)
c)
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
Result
10.2 How to get the file version for the document by version Id
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
In this example you will see how to get the file version for the document by version Id using the .Net Client Side
object Model in Powershell scripts.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
# Get the SharePoint Web
$web=$clientContext.Web;
# Get the list by title
$list=$clientContext.Web.Lists.GetByTitle("Documents")
# Get an item by ID
$item=$list.GetItemById(23)
# Get the version for the document using VersionId
$version=$item.File.Versions.GetById(1);
$clientContext.Load($version)
# Execute the query
$clientContext.ExecuteQuery();
Result
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
Result
10.4 How to delete a file version by version label for the document
In this example you will see how to delete a file version by version label for the document using the .Net Client
Side object Model in Powershell scripts.
b)
c)
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
Result
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
$credential=Get-Credential
$username=$credential.UserName
$password=$credential.GetNetworkCredential().Password
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
### Input Parameters
$url = "https://c986.sharepoint.com/"
### References
# Specify the path where the dll's are located.
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"
### Function
function RestoreFileVersion()
{
# Connect to SharePoint Online and get ClientContext object.
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
# Get the SharePoint Web
$web=$clientContext.Web;
# Get the list by title
$list=$clientContext.Web.Lists.GetByTitle("Documents")
# Get an item by ID
$item=$list.GetItemById(23)
# Restore the file version
$version=$item.File.Versions.RestoreByLabel("1.0")
# Execute the query
$clientContext.ExecuteQuery();
}
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
10.6 How to check if the file version is a current version for the document
In this example you will see how to determine if the file version is a current version for the document using the
.Net Client Side object Model in Powershell scripts.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
Result
10.7 How to delete all the file versions for the document
In this example you will see how to delete all the file versions for the document using the .Net Client Side object
Model in Powershell scripts.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
All the file versions for the document are deleted successfully.
Perform SharePoint group tasks using CSOM in Powershell script
In this section you will see how to do group related tasks using the SharePoint 2013 .Net Client Side Object
Model in Powershell scripts.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
### References
# Specify the path where the dll's are located.
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"
### Function
function CreateGroup()
{
# Connect to SharePoint Online and get ClientContext object.
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
# Get the SharePoint web
$web=$clientContext.Web;
# Get all the site groups
$groupColl=$web.SiteGroups;
# Create a new site group
$groupCreationInfo=New-Object
Microsoft.SharePoint.Client.groupCreationInformation;
$groupCreationInfo.Title="Vijai Custom Group";
$groupCreationInfo.Description= " Custom group created using Powershell";
$newGroup=$groupColl.Add($groupCreationInfo);
$clientContext.Load($newGroup);
# Execute the query
$clientContext.ExecuteQuery();
# Display the new group name
Write-Host -ForegroundColor Green "New group created successfully: "
$newGroup.Title
}
### Calling the function
CreateGroup
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
10.10 How to set the user as owner for the site group
In this example you will see how to set the user as owner for the site group using the .Net Client Side Object
Model in Powershell scripts.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
$url = "https://c986.sharepoint.com/"
### References
# Specify the path where the dll's are located.
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"
### Function
function SetGroupOwner()
{
# Connect to SharePoint Online and get ClientContext object.
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
# Get the SharePoint web
$web=$clientContext.Web;
$ownerUser=$web.EnsureUser("i:0#.f|membership|vijaianand@c986.onmicrosoft.com");
# Set the user as owner for the site group
$group=$web.SiteGroups.GetByName("Owners");
$group.Owner=$ownerUser;
# Update the group
$group.Update();
$clientContext.Load($group);
# Execute the query
$clientContext.ExecuteQuery();
}
### Calling the function
SetGroupOwner
Result
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
10.11 How to set the group as owner for the site group
In this example you will see how to set the group as owner for the site group using the .Net Client Side Object
Model in Powershell scripts.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
### References
# Specify the path where the dll's are located.
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"
### Function
function SetGroupOwner()
{
# Connect to SharePoint Online and get ClientContext object.
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
# Get the SharePoint web
$web=$clientContext.Web;
# Get the specific site group by name
$ownerGroup=$web.SiteGroups.GetByName("Owners");
# Set the group as owner for the site group
$group=$web.SiteGroups.GetByName("Vijai Custom Group");
$group.Owner=$ownerGroup;
# Update the group
$group.Update();
# Execute the query
$clientContext.ExecuteQuery();
}
### Calling the function
SetGroupOwner
Result
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
10.12 How to get all the users from the site group
In this example you will see how to get all the users from the site group using the .Net Client Side Object Model
in Powershell scripts.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
Result
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
$url = "https://c986.sharepoint.com/"
### References
# Specify the path where the dll's are located.
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"
### Function
function RemoveUserFromGroup()
{
# Connect to SharePoint Online and get ClientContext object.
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
# Get the SharePoint web
$web=$clientContext.Web;
$user=$web.EnsureUser("i:0#.f|membership|vijaianand@c986.onmicrosoft.com");
$clientContext.Load($user);
# Execute the query
$clientContext.ExecuteQuery();
# Get the specific site group by name
$group=$web.SiteGroups.GetByName("Vijai Custom Group");
# Remove a user the specific group
$group.Users.RemoveByLoginName($user.LoginName);
# Execute the query
$clientContext.ExecuteQuery();
}
### Calling the function
RemoveUserFromGroup
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
The user is removed successfully from the Vijai Custom Group.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
11.1 How to get all the permission levels from the website
In this example you will see how to get all the roles or permission levels from the website using the .Net Client
Side object Model in Powershell scripts.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
### Function
function GetPermissionLevels()
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
{
# Connect to SharePoint Online and get ClientContext object.
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
# Get the SharePoint Web
$web= $clientContext.Web;
# Get all the permission levels
$roleDefColl=$web.RoleDefinitions;
$clientContext.Load($roleDefColl);
# Execute the query
$clientContext.ExecuteQuery();
# Loop through all the role definitions
foreach($roleDef in $roleDefColl)
{
Write-Host -ForegroundColor Green $roleDef.Name
}
}
### Calling the function
GetPermissionLevels
Result
Navigate to the SharePoint site. Click on Settings and then click on Site Settings. Click on Site Permissions
available under the Users and Permissions section. Click on Permission Levels available in the ribbon interface.
You will see all the permission levels available in the website.
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
$url = "https://c986.sharepoint.com/"
### References
# Specify the path where the dll's are located.
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"
### Function
function CreatePermissionLevel()
{
# Connect to SharePoint Online and get ClientContext object.
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
# Get the SharePoint Web
$web= $clientContext.Web;
# Base Permissions that has to be added to the role definition
$permissions = New-Object Microsoft.SharePoint.Client.BasePermissions;
$permissions.Set([Microsoft.SharePoint.Client.PermissionKind]::ViewListItems);
$permissions.Set([Microsoft.SharePoint.Client.PermissionKind]::ViewVersions);
# Initialize the role definition
$creationInfo = New-Object
Microsoft.SharePoint.Client.RoleDefinitionCreationInformation;
$creationInfo.Name = "My role";
$creationInfo.Description = "My role created using powershell";
$creationInfo.BasePermissions = $permissions;
# Add the role definitin to the site
$web.RoleDefinitions.Add($creationInfo);
# Execute the query
$clientContext.ExecuteQuery();
}
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
Navigate to the SharePoint site. Click on Settings and then click on Site Settings. Click on Site Permissions
available under the Users and Permissions section. Click on Permission Levels available in the ribbon interface.
You will see a new role or permission level is created successfully.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
### Function
function UpdatePermissionLevel()
{
# Connect to SharePoint Online and get ClientContext object.
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
# Get the SharePoint Web
$web= $clientContext.Web;
# Get the role definition by name
$roleDef=$web.RoleDefinitions.GetByName("My Role");
# Update the description
$roleDef.Description = " Description updated";
# Add the permissions
$permissions = New-Object Microsoft.SharePoint.Client.BasePermissions;
$permissions.Set([Microsoft.SharePoint.Client.PermissionKind]::ApproveItems);
$permissions.Set([Microsoft.SharePoint.Client.PermissionKind]::CreateAlerts);
$roleDef.BasePermissions = $permissions;
# Update the permission level
$roleDef.Update();
$clientContext.Load($roleDef);
# Execute the query
$clientContext.ExecuteQuery();
}
### Calling the function
UpdatePermissionLevel
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
Navigate to the SharePoint site. Click on Settings and then click on Site Settings. Click on Site Permissions
available under the Users and Permissions section. Click on Permission Levels available in the ribbon interface.
Click on My role permission level. You will see the specified role or permission level is updated successfully as
shown in Figure 13.3.1.
d)
e)
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
### Function
function RemovePermission()
{
# Connect to SharePoint Online and get ClientContext object.
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
# Get the SharePoint Web
$web= $clientContext.Web;
# Get the role definition by name
$roleDef=$web.RoleDefinitions.GetByName("My Role");
# remove the permissions
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
Navigate to the SharePoint site. Click on Settings and then click on Site Settings. Click on Site Permissions
available under the Users and Permissions section. Click on Permission Levels available in the ribbon interface.
Click on My role permission level. You will see the permissions are removed successfully from the role.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
$credential=Get-Credential
$username=$credential.UserName
$password=$credential.GetNetworkCredential().Password
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
### Input Parameters
$url = "https://c986.sharepoint.com/"
### References
# Specify the path where the dll's are located.
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"
### Function
function DeletePermissionLevel()
{
# Connect to SharePoint Online and get ClientContext object.
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
# Get the SharePoint Web
$web= $clientContext.Web;
# Get the role definition by name
$roleDef=$web.RoleDefinitions.GetByName("My Role");
# Delete the role definition
$roleDef.DeleteObject();
# Execute the query
$clientContext.ExecuteQuery();
}
### Calling the function
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
DeletePermissionLevel
Result
Navigate to the SharePoint site. Click on Settings and then click on Site Settings. Click on Site Permissions
available under the Users and Permissions section. Click on Permission Levels available in the ribbon interface.
You will see the specified role or permission level is successfully deleted from the website.
12.1 How to get all the Term Stores for the provided site
In this example you will see how to get all the termstores using the .Net Client Side Object Model in Powershell
scripts.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
### Function
function GetTaxonomyStores()
{
# Connect to SharePoint Online and get ClientContext object.
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$ctx.Credentials = $credentials
$referencedAssemblies = (
"Microsoft.SharePoint.Client, Version=15.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c",
"Microsoft.SharePoint.Client.Runtime, Version=15.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c",
"Microsoft.SharePoint.Client.Taxonomy, Version=15.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c",
"System.Core, Version=3.5.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089")
$sourceCode = @"
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Taxonomy;
using System.Collections.Generic;
using System.Linq;
public static class QueryHelper
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
{
public static void LoadListWithLimtedFields(ClientContext ctx,
TaxonomySession taxonomySession)
{
ctx.Load(
taxonomySession.TermStores,
termStores => termStores.Include
(termStore => termStore.Name)
);
}
}
"@
Add-Type -ReferencedAssemblies $referencedAssemblies -TypeDefinition $sourceCode
-Language CSharp;
Result
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
### References
# Specify the path where the dll's are located.
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"
### Function
function GetTermStoreGroups()
{
# Connect to SharePoint Online and get ClientContext object.
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$ctx.Credentials = $credentials
$referencedAssemblies = (
"Microsoft.SharePoint.Client, Version=15.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c",
"Microsoft.SharePoint.Client.Runtime, Version=15.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c",
"Microsoft.SharePoint.Client.Taxonomy, Version=15.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c",
"System.Core, Version=3.5.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089")
$sourceCode = @"
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Taxonomy;
using System.Collections.Generic;
using System.Linq;
public static class QueryHelper
{
public static void LoadListWithLimtedFields(ClientContext ctx, TermStore
termstore)
{
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
ctx.Load(
termstore.Groups,
termGroups => termGroups.Include
(termGroup => termGroup.Name)
);
}
}
"@
Add-Type -ReferencedAssemblies $referencedAssemblies -TypeDefinition $sourceCode
-Language CSharp;
Result
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
### Function
function CreateTermGroup()
{
# Connect to SharePoint Online and get ClientContext object.
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$ctx.Credentials = $credentials
# Get the taxonomy session
$taxonomySession=[Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomyS
ession($ctx);
# Get the term store by name
$termstore=$taxonomySession.TermStores.GetByName("Taxonomy_DoxF1n/9qNkL60u1n5J+tQ==")
;
# Create a new guid for group
$guid=[System.Guid]::NewGuid()
# Create a new group
$termGroup=$termStore.CreateGroup("NewGroup",$guid)
# Commit all the changes
$termStore.CommitAll();
# Execute the query
$ctx.ExecuteQuery()
}
### Calling the function
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
CreateTermGroup
Result
A new taxonomy group is created successfully as shown in Figure 14.3.1.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
### Function
function DeleteTermGroup()
{
# Connect to SharePoint Online and get ClientContext object.
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$ctx.Credentials = $credentials
# Get the taxonomy session
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
$taxonomySession=[Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomyS
ession($ctx);
# Get the term store by name
$termstore=$taxonomySession.TermStores.GetByName("Taxonomy_DoxF1n/9qNkL60u1n5J+tQ==")
;
# Group Guid
$guid=New-Object System.Guid("4b6caff1-6f6b-4c62-87e1-2d077eb62558")
# Get the term group by Guid
$termGroup=$termStore.GetGroup($guid)
# Delete the term group
$termGroup.DeleteObject();
# Execute the query
$ctx.ExecuteQuery()
}
### Calling the function
DeleteTermGroup
Result
The specified taxonomy group is deleted successfully from the termstore.
12.5 How to get all the termsets for the taxonomy group
In this example you will see how to get all the termsets for the taxonomy group using the .Net Client Side Object
Model in Powershell scripts.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
### Function
function GetTermSets()
{
# Connect to SharePoint Online and get ClientContext object.
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$ctx.Credentials = $credentials
# Get the taxonomy session
$taxonomySession=[Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomyS
ession($ctx);
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
.
Figure14.5.1: Get all the termsets
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
### Function
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
function CreateTermSet()
{
# Connect to SharePoint Online and get ClientContext object.
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$ctx.Credentials = $credentials
# Get the taxonomy session
$taxonomySession=[Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomyS
ession($ctx);
# Get the term store by name
$termstore=$taxonomySession.TermStores.GetByName("Taxonomy_DoxF1n/9qNkL60u1n5J+tQ==")
;
# Group Guid
$guid=New-Object System.Guid("da52b879-4c2c-4697-a574-ef5be1255d62")
# Get the term group by Guid
$termGroup=$termStore.GetGroup($guid)
# New termset name
$termSetName = "New TermSet";
# New Term Set GUID
$guid=[System.Guid]::NewGuid()
# New Term Set LCID
$LCID=1033;
# Create a new term set
$termSetColl=$termGroup.CreateTermSet($termSetName,$guid, $LCID);
# Execute the query
$ctx.ExecuteQuery()
}
### Calling the function
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
CreateTermSet
Result
12.7 How to delete the term set from the specified group
In this example you will see how to delete the termset from the specified group using the .Net Client Side Object
Model in Powershell scripts.
b)
c)
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
### Function
function DeleteTermSet()
{
# Connect to SharePoint Online and get ClientContext object.
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$ctx.Credentials = $credentials
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
b)
c)
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
### Function
function GetTerms()
{
# Connect to SharePoint Online and get ClientContext object.
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$ctx.Credentials = $credentials
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
### References
# Specify the path where the dll's are located.
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"
### Function
function CreateTerm()
{
# Connect to SharePoint Online and get ClientContext object.
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$ctx.Credentials = $credentials
# Get the taxonomy session
$taxonomySession=[Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomyS
ession($ctx);
# Get the term store by name
$termstore=$taxonomySession.TermStores.GetByName("Taxonomy_DoxF1n/9qNkL60u1n5J+tQ==")
;
# Get the term group by name
$termGroup=$termStore.Groups.GetByName("Global Navigation");
# Get the term set by name
$termSet = $termGroup.TermSets.GetByName("Technology");
# String Variable - New term name
$termName = "New Term";
# Guid - New Term
GUID
$guid=[System.Guid]::NewGuid()
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Result
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
### Function
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
function DeleteTerm()
{
# Connect to SharePoint Online and get ClientContext object.
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$ctx.Credentials = $credentials
# Get the taxonomy session
$taxonomySession=[Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomyS
ession($ctx);
# Get the term store by name
$termstore=$taxonomySession.TermStores.GetByName("Taxonomy_DoxF1n/9qNkL60u1n5J+tQ==")
;
# Get the term group by name
$termGroup=$termStore.Groups.GetByName("Global Navigation");
# Get the term set by name
$termSet = $termGroup.TermSets.GetByName("Technology");
# Get the term
by name
Result
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
### Function
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
function CopyTerm()
{
# Connect to SharePoint Online and get ClientContext object.
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$ctx.Credentials = $credentials
# Get the taxonomy session
$taxonomySession=[Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomyS
ession($ctx);
# Get the term store by name
$termstore=$taxonomySession.TermStores.GetByName("Taxonomy_DoxF1n/9qNkL60u1n5J+tQ==")
;
# Get the term group by name
$termGroup=$termStore.Groups.GetByName("Global Navigation");
# Get the term set by name
$termSet = $termGroup.TermSets.GetByName("Technology");
# Get the term
by name
$term = $termSet.Terms.GetByName("Silverlight");
# Make a copy of the term within the termset
# Need to pass a bool parameter - whether to copy the child terms or not
$copyTerm=$term.Copy($false);
# Execute the query
$ctx.ExecuteQuery();
}
### Calling the function
CopyTerm
Result
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
The specified term is copied to the same termset as shown in Figure 14.11.1.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
### Function
function DeprecateTerm()
{
# Connect to SharePoint Online and get ClientContext object.
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$ctx.Credentials = $credentials
# Get the taxonomy session
$taxonomySession=[Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomyS
ession($ctx);
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
$term = $termSet.Terms.GetByName("Silverlight");
# Deprecate the term
$term.Deprecate($true);
# Execute the query
$ctx.ExecuteQuery();
}
### Calling the function
DeprecateTerm
Result
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Script
### Function
function GetLabels()
{
# Connect to SharePoint Online and get ClientContext object.
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$ctx.Credentials = $credentials
# Get the taxonomy session
$taxonomySession=[Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomyS
ession($ctx);
# Get the term store by name
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
$termstore=$taxonomySession.TermStores.GetByName("Taxonomy_DoxF1n/9qNkL60u1n5J+tQ==")
;
# Get the term group by name
$termGroup=$termStore.Groups.GetByName("Global Navigation");
# Get the term set by name
$termSet = $termGroup.TermSets.GetByName("Technology");
# Get the term
by name
Result
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
### Function
function CreateLabel()
{
# Connect to SharePoint Online and get ClientContext object.
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$ctx.Credentials = $credentials
# Get the taxonomy session
$taxonomySession=[Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomyS
ession($ctx);
# Get the term store by name
$termstore=$taxonomySession.TermStores.GetByName("Taxonomy_DoxF1n/9qNkL60u1n5J+tQ==")
;
# Get the term group by name
$termGroup=$termStore.Groups.GetByName("Global Navigation");
# Get the term set by name
$termSet = $termGroup.TermSets.GetByName("Technology");
# Get the term
by name
$ctx.ExecuteQuery();
}
### Calling the function
CreateLabel
Result
d)
e)
f)
Enter the Office 365 username (vijaianand@c986.onmicrosoft.com) and password (*********) in the
Credentials pop up. Click on Ok.
Script
### Function
function DeleteLabel()
{
# Connect to SharePoint Online and get ClientContext object.
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$ctx.Credentials = $credentials
# Get the taxonomy session
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
$taxonomySession=[Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomyS
ession($ctx);
# Get the term store by name
$termstore=$taxonomySession.TermStores.GetByName("Taxonomy_DoxF1n/9qNkL60u1n5J+tQ==")
;
# Get the term group by name
$termGroup=$termStore.Groups.GetByName("Global Navigation");
# Get the term set by name
$termSet = $termGroup.TermSets.GetByName("Technology");
# Get the term by name
$term = $termSet.Terms.GetByName("SharePoint 2013");
# Get the label for the term
$label = $term.Labels.GetByValue("Office 365");
# Delete the label
$label.DeleteObject();
# Execute the query
$ctx.ExecuteQuery();
}
### Calling the function
DeleteLabel
Result
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Summary:
In this book we covered nearly all the basic operations that can be performed with Powershell scripts using the
SharePoint 2013 .Net Client Side Object Model. This book is only the beginning, there are many things that can
be done and now you are ready to develop advanced solutions with Powershells script using the .Net Client Side
Object Model.
2014 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.