Recent Comments

Category: SharePoint Online

A friend of mine Erdem Avni Avni SELÇUK from Turkey showed a video sample which retrieves SharePoint Online list column information in Excel! Truly amazing stuff for SharePoint IT Professionals at least for me. So, thought of Sharing my demo code which simply lists all SharePoint list title and includes a header text and footer with image in word document. This is very ugly code but may give some dice for IT Pro’s for customizing and building the solution as desired for content managers.

This is our output – Modified the image so header and footer will be visible

A colleague asked me a script for deleting nodes from Global Navigation or from Quick Launch in a SharePoint Site. Before we get into the PowerShell script lets know about the Navigation (In General). The below image illustrates the navigation settings in a SharePoint Site

All the headings with folder signs are parent nodes and links are children i.e child nodes. The requirement for now is to remove the parent node which can be accomplished by enumerating the appropriate collections QuickLaunch and TopNavigationBar. Here is the full script

A colleague asked me a script to retrieve SharePoint Site Groups information by excluding the user’s information and added he couldn’t use Select-Object cmdlet to exclude user’s collection in the group property. Yes, it’s not possible! But, we have a fix for it and here is the PowerShell script to get Group properties by ignoring the UserCollection property in it

Reason: We need to explicit request and execute to get user collection information but in our case it’s not required. So, simply we skipped the property which has type as collection. Note: In group property user is the only collection type property.

Introduction

In Part 3 we showed the steps to add Format files for our Script module and in this article we will demo an alternate method to Add custom view for our objects without creating PSOject explicitly and this is just a tip to get List Property information. Do remember we are using CSOM and PowerShell and there are high chances of performance issues which will be addressed in later upcoming blog articles. For, now we will cover alternate options.

What we did here is a very simple and ugly way of getting List Information and created a default view using Format.ps1xml. The advantage is we will allow users to select the required properties! So, no need to type all properties names and keeps the code neat and clean. Again! Please ignore the performance we will discuss about it soon. This function takes maximum 4 seconds and 1 second minimum to query.

In this article we will demo about adding format files to get desired output.

What is Format File?

Why we need to add format file? The very simple reason is to define display the object in command line. For example check the output of Get-Service cmdlet. By default it shows the properties like Status, Name and DisplayName which is defined in the file DotNetTypes.Format.ps1xml. Please use this for reference and do not modify it to avoid tampering of your existing view in PowerShell. So, when we run Get-Service we get three properties in output by default and to see more we either do one of the below as required. Okay, with this reference we will create our own Format File and name it as SPOnlineModule.Format.PS1XML and the location is under our module folder. To make this article easy I am opting to make List View Basic

Get Started

Let’s make our job easy! I copied the code from the List View Basic and modified my PowerShell function to meet the requirement.

XHTML

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

<?xml version="1.0"encoding="utf-8"?>

<Configuration>

<ViewDefinitions>

<View>

<Name>SharePointOnline.Format.Custom.ListView</Name>

<ViewSelectedBy>

<TypeName>SharePointOnline.Format.Custom.ListView</TypeName>

</ViewSelectedBy>

<TableControl>

<TableRowEntries>

<TableRowEntry>

<TableColumnItems>

<TableColumnItem>

<PropertyName>Title</PropertyName>

</TableColumnItem>

<TableColumnItem>

<PropertyName>ItemCount</PropertyName>

</TableColumnItem>

</TableColumnItems>

</TableRowEntry>

</TableRowEntries>

</TableControl>

</View>

</ViewDefinitions>

</Configuration>

All we need to do is simple steps

Create a Format File and name it as SPOnlineModule.Format.ps1xml

Save the file inside your module directory

Include this file name in PSD1 file at FormatsToProcess = @(‘SPOnlineModule.Format.ps1xml’)

Import the Module

Connect to SharePoint Online and execute Get-xSPOList cmdlet which yields only two properties Title and ItemCount.

Just as an example I am selecting first 5 list and the output is illustrated below
Note: We haven’t fixed the Connect-xSPOTenant cmdlet yet! We need to add Conditional Scope to make it more friendly.

In our Get-xSPOList cmdlet I included the below code which appends the view name to the PSCustomObject

Introduction

In our last blog post (Part 1) we have show cased two functions which allows us to connect to SharePoint Online and retrieves list information from the site collections. Now, we will create a small PowerShell module with help.

About Modules

To learn about PowerShell Modules refer this link. We will be focusing about Script Module! Which is easy to understand and to troubleshoot as well. A script Module is nothing but valid script saved as .psm1 file and it has a structure to save. For our demo, we will save the PowerShell module is User Module path i.e “C:\Users\ChenV\Documents\WindowsPowerShell\Modules\SPOnlineModule” in which SPOnlineModule is the folder which contains .PSM1, .PSD1 file and required DLL’s. Ensure the psm1 and psd1 files are also named as SPOnlineModule.

Specific User

This is the location for installing modules for specific user “$home\Documents\WindowsPowerShell\Modules\”

For All Users

This is the location for installing modules for all users “$EnvProgramFiles\WindowsPowerShell\Modules\”

Creating Modules

It’s very simple to create module but before distributing it ensure you have done the below

Test the Module

Include PowerShell Version

Create a Proper psd1 file

Include all required assemblies (In our case it’s required)

Include Help Files (This blog post will cover this!)

Create a Custom View for the Output

PSD1 File

This is psd1 file for the two functions we created named “Connect-xSPOTenant” and Get-xSPOList. I have moved all the SharePoint Online CSOM SDK Assemblies to my module folder. Just to distribute it to my clients and they don’t need to install the package. I have included the required dll in NestedModule Section.

PowerShell

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

#

# Module manifest for module 'SPOnlineModule'

#

# Generated by: Chendrayan Venkatesan

#

# Generated on: 11-5-2016

#

@{

# Script module or binary module file associated with this manifest.

RootModule='SPOnlineModule.psm1'

# Version number of this module.

ModuleVersion='1.0'

# ID used to uniquely identify this module

GUID='c62fa4e8-d1d2-41e2-9304-af38c827dda9'

# Author of this module

Author='Chendrayan Venkatesan'

# Company or vendor of this module

CompanyName='Free Lancer'

# Copyright statement for this module

Copyright='(c) 2016 904870. All rights reserved.'

# Description of the functionality provided by this module

Description='Include the Module Description.'

# Minimum version of the Windows PowerShell engine required by this module

PowerShellVersion='5.0'

# Name of the Windows PowerShell host required by this module

# PowerShellHostName = ''

# Minimum version of the Windows PowerShell host required by this module

# PowerShellHostVersion = ''

# Minimum version of Microsoft .NET Framework required by this module

# DotNetFrameworkVersion = ''

# Minimum version of the common language runtime (CLR) required by this module

# CLRVersion = ''

# Processor architecture (None, X86, Amd64) required by this module

# ProcessorArchitecture = ''

# Modules that must be imported into the global environment prior to importing this module

# RequiredModules = @()

# Assemblies that must be loaded prior to importing this module

# RequiredAssemblies = @()

# Script files (.ps1) that are run in the caller's environment prior to importing this module.

# ScriptsToProcess = @('GetCredential.ps1')

# Type files (.ps1xml) to be loaded when importing this module

# TypesToProcess = @()

# Format files (.ps1xml) to be loaded when importing this module

# FormatsToProcess = @()

# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess

NestedModules=@('Microsoft.SharePoint.Client.dll',

'Microsoft.SharePoint.Client.Runtime.dll',

'Microsoft.Online.SharePoint.Client.Tenant.dll',

'CredentialManagement.dll')

# Functions to export from this module

FunctionsToExport='*'

# Cmdlets to export from this module

CmdletsToExport='*'

# Variables to export from this module

VariablesToExport='*'

# Aliases to export from this module

AliasesToExport='*'

# DSC resources to export from this module

# DscResourcesToExport = @()

# List of all modules packaged with this module

# ModuleList = @()

# List of all files packaged with this module

# FileList = @()

# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.

PrivateData=@{

PSData=@{

# Tags applied to this module. These help with module discovery in online galleries.

# Tags = @()

# A URL to the license for this module.

# LicenseUri = ''

# A URL to the main website for this project.

# ProjectUri = ''

# A URL to an icon representing this module.

# IconUri = ''

# ReleaseNotes of this module

# ReleaseNotes = ''

}# End of PSData hashtable

}# End of PrivateData hashtable

# HelpInfo URI of this module

# HelpInfoURI = ''

# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.

# DefaultCommandPrefix = ''

}

PSM1 File

We haven’t added help / comments or display view for the functions. All we did is created the module with required files. The below code is my PowerShell .psm1 file which has only two functions as I mentioned earlier.

Take look at the folder contents inside my module folder – And remember we named it as SPOnlineModule It’s good to start! Let’s try to import the module and test the functionality so we can add the requirement components which makes our module rich and easy to use.

Impo

PowerShell

1

Import-ModuleSPOnlineModule-Verbose

Fantastic, we can see the module loading successfully. always use Verbose switch to get information about the cmdlets we are loading to the session. Now, let’s see the available cmdlet by simply executing the below snippet

gcm

PowerShell

1

Get-Command-ModuleSPOnlineModule

and the output is shown below let’s skip the functionality test because we did it in our previous blog (Part 1). So we will focus on help and custom view for output formatting in this blog post. If we run the below PowerShell code we get very basic information about the cmdlet.

help

PowerShell

1

2

3

help Connect-xSPOTenant

help Connect-xSPOTenant-Detailed

help Connect-xSPOTenant-Examples

Something like this Okay enough of theory, we just need to add comment based help for our function and PowerShell will take care of the rest. To do this in our psm1 file we will include the comment based help inside the PowerShell functions like shown below

Comment Help

PowerShell

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<#

.SYNOPSIS

Short description

.DESCRIPTION

Long description

.EXAMPLE

C:\PS> <example usage>

Explanation of what the example does

.INPUTS

Inputs (if any)

.OUTPUTS

Output (if any)

.NOTES

General notes

#>

Just by doing this we are adding more feature to our PowerShell module and makes others to read the help before using the module. Avoids many hindrances and break dependencies at work place. Any PowerShell folks can go through the code and understand it. So, now Connect-xSPOTenant looks like below

Connect-xSPOTenant

PowerShell

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

functionConnect-xSPOTenant{

<#

.SYNOPSIS

This cmdlet is to establish connection to SharePoint Online Tenant.

.DESCRIPTION

Prior to any other cmdlet this needs to be used. In order to perform SharePoint Online Tasks using Client Side Object Model establish the connection

using Connect-xSPOTenant cmdlet.

.EXAMPLE

C:\PS> Connect-xSPOTenant -Url "https://contoso-admin.sharepoint.com"

Prompts to enter the SharePoint Online Admin Credentials without User Name

Introduction

CSOM is no more a secret for SharePoint IT Professionals and developers. MS released a new version of SharePoint Online CSOM and it’s available in Nuget which makes our life bit easier. All we need is to run Install-Package “Microsoft.SharePointOnline.CSOM” -Verbose We can use VS Community Edition for building binary modules or use Visual Studio Code editor to build scripts or script modules. After installing the packages I found my Nuget folder under the path “C:\Program Files\NuGet\” and the required DLL are located in “C:\Programfiles\NuGet\Packages\Microsoft.SharePointOnline.CSOM.16.1.5026.1200\lib\net45“. The DLL files are listed below. This is a part 1 so we will just use Two DLL’s for now to get very basic information.

Microsoft.Office.Client.Policy.dll

Microsoft.Office.Client.TranslationServices.dll

Microsoft.Office.SharePoint.Tools.dll

Microsoft.Online.SharePoint.Client.Tenant.dll

Microsoft.ProjectServer.Client.dll

Microsoft.SharePoint.Client.dll

Microsoft.SharePoint.Client.DocumentManagement.dll

Microsoft.SharePoint.Client.Publishing.dll

Microsoft.SharePoint.Client.Runtime.dll

Microsoft.SharePoint.Client.Runtime.Windows.dll

Microsoft.SharePoint.Client.Search.Applications.dll

Microsoft.SharePoint.Client.Search.dll

Microsoft.SharePoint.Client.Taxonomy.dll

Microsoft.SharePoint.Client.UserProfiles.dll

Microsoft.SharePoint.Client.WorkflowServices.dll

Using Import-Module “Path to DLL” I loaded the binaries to my PowerShell session so the Visual Studio Code will help us in intellisense. Thus, we do code at ease! The below function will establish the connection to SharePoint Online at tenant level. To understand the SharePoint Online site hierarchy refer the below image (Just as a References) PowerShell Code to Connect With SharePoint Online Tenant

Just halt here! We didn’t use the conditional scopes to validate. If you pass incorrect values to variables no exception will be thrown. If you are planning to commit this in Binary module then refer this blog post. After establishing the connection we can query different site collections to get list information. For example, refer the following function which connects to one of the Site Collections and retrieve List Information.

Introduction

Recently I was building a PowerShell Module for one of our customers SharePoint farm. Of course, it’s completely customized. The SharePoint Online farm has multiple site collections with different languages. Any organization located in across geographical area needs this set up. So, in one our cmdlet we retrieve site information including language (In My Lab all the site are in en-US but the code works for other LocaleID ).

Get Started

Though, I was in middle of building binary module, I just paused it for a while and used SharePoint Online Management module for testing.Good let’s import the module to meet our need

Import-Module

PowerShell

1

Import-Module-NameMicrosoft.Online.SharePoint.PowerShell-Verbose

Well, module is imported with warning due to unapproved verbs and that’s not going to impact. Now, let’s connect to SharePoint Tenant by using the below cmdlet

Introduction

Recently one of our customer requested us to share a code which should throw an exception while connecting to SharePoint Online if in case of credential invalid or not connecting to tenant site. Well, they have a binary module delivered by some suppliers which is pretty straight forward and simple. If all set to good code will execute if not exception thrown in other cmdlet. The module they use has bunch of cmdlets which are more or less mock up of SharePoint Online cmdlets. Connect-SPOService throws exception if credential are invalid and they need custom error message for the custom built cmdlet Connect-SPOTenant

Requirement

Connect-SPOTenant : The sign-in name or password does not match one in the Microsoft account system

Solution

Indeed, the above code will not throw any exception because the ClientContext Object is instantiated and will not validate the credential but returns the ClientContext Object as output. More over Microsoft.SharePoint.Client.ClientContext doesn’t have ServerObjectIsNull property. No matter we supply wrong password the host shows Connected to Site!

Conditional Scope

C# code with conditional scope is below. This is just a demo so we haven’t used any scope methods or properties

During SharePoint Online discussion a question popped up “How to get all installed application information in SharePoint Online?” a simple answer is Get-SPOAppInfo cmdlet! But, wait we are partially correct but read this documentation https://technet.microsoft.com/en-us/library/fp161398.aspx. The below image illustrates the parameters of Get-SPOAppInfo both set to be false and it’s not $TRUE – This cmdlet needs either Name or Product ID! So, we can’t use this cmdlet to retrieve all the apps installed in the given Tenant!

Issue:

So we can Get installed apps information by using below

Enough! We are not going to use this. Let’s use Client Side Object Model in PowerShell and solve the issue.