Microsoft Word VSTO ADDIN – Retrieve SharePoint Site List Information Using CSOM

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

6

 

Step 1: Create a a Office VSTO office 2013 / 2016 addin solution using Visual Studio – Start From here

Step 2: Add a new item to the solution and choose Ribbon – it’s an xml file! – Start From here

Step 3: Install CSOM SDK assemblies 🙂 🙂 🙂 – Below is the sample code

Continue reading Microsoft Word VSTO ADDIN – Retrieve SharePoint Site List Information Using CSOM

Delete Navigation (Top Navigation or Quick Launch Node) Using CSOM and PowerShell

1

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

2

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

How to use this script? Copy paste the code in your script location for example C:\Scripts and follow the below steps. Warning: If you have deleted the node it can’t be restored.

$Credential = Get-Credential -Credential ‘admin@contoso.onmicrosoft.com’ – By doing this we can pass multiple site and node title and remove the nodes from different sites

I have removed few nodes from Top and Quick and the below image illustrates it. (Compare the preceding image)

4

In my next blog I will share few more scripts for SharePoint Online which may helps content managers to manage the site easily. Enjoy PowerShell!

Part 4 – Getting Started With CSOM | SharePoint Online | PowerShell

Introduction

We discussed and show cased few PowerShell and CSOM functionalities in earlier blogs. Please refer to the links below

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.

Code at Ease

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.

Output

333 Well! It looks good for now! Let’ test it with different properties

Result

444

Part 3 – Getting Started With CSOM | SharePoint Online | PowerShell

Introduction

In our last blog series we have seen few concepts of PowerShell CSOM like creating functions and including comments based help for our PowerShell functions in the module. Reference Links is below

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
111. 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.

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
222
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

 

Part 2 – Getting Started With CSOM | SharePoint Online | PowerShell

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.

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
2016-05-24_16-28-30 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.

Fantastic, we can see the module loading successfully.
2016-05-24_16-32-01 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

and the output is shown below
2016-05-24_16-34-35 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.

Something like this
2016-05-24_16-38-45 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

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

which now allows us to use help at ease.
2016-05-24_16-53-24 and examples like 34334

Part 1 – Getting Started With CSOM | SharePoint Online | PowerShell

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 2016-05-24_14-32-44 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)a30d1b67-e6ff-4fea-bc44-5f00ba7fcc33 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.

The below figure illustrates the output.
2016-05-24_15-39-18 In our next blog we will add some more functions and show case creating modules.

SharePoint Online PowerShell Tip: Customize Your Script to Meet Client Requirements

sharepoint

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.
1Good let’s import the module to meet our need

2 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

and by using the Cmdlet Get-SPOSite we can get the information we required like shown below

3 I just selected two properties to avoid exposing domain information. 1033 is LCID of en-US (English United States). Let’s play with our native Windows PowerShell

4
which accepts three overloads let’s grab the first one to show the display name of the LocaleID (A property (output) of Get-SPOSite)

5 Fine, we know the way to do! Let’s include in our Get-SPOSite cmdlet to get desired output.

6 or we can get display name by executing the below code

7 Clear, in the binary module I added the below piece of code to get desired output for our content managers

and the sample code is below

Note: I have included help and format files in this code!

SharePoint Online PowerShell Tip: Conditional Scope – Binary Module

SharePoint Online PowerShell Tip to throw exception while connecting to Tenant.

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
  • Connect-SPOTenant : Current site is not a tenant administration site.

PowerShell Code

Below is the PowerShell code shared by our client.

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

Screen Shot

1

2

Get-SPOAppInfo | SharePoint Online | PowerShell

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.

Output

Code:

Bit more to organize it, we can use PSObject!

2016-01-20_10-22-29