How to execute ANY SharePoint powershell command programmatically using C#

In one of my projects my team faced following challenges:

  • How to add query rules programmatically using C#
  • How to update thesaurus programmatically using C#

I tried to find information in official documentation but it was not very helpful neither was googling.

Powershell cmdlets to c# assembly mapping

In my team we were thinking what to do in this situation and one of my colleagues came with brilliant idea – he searched for PowerShell cmdlet in file explorer with searching in files content option turned on.

Result? What he found was exactly what we were looking for.

In location “C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\CONFIG\PowerShell\Registration” there is file named OSSSearchCmdlets.xml.

What it contains is xml structure with following structure:

       <ps:Cmdlet>

            <ps:VerbName>Get-SPEnterpriseSearchCrawlContentSource</ps:VerbName>

            <ps:ClassName>Microsoft.Office.Server.Search.Cmdlet.GetSearchCrawlContentSource</ps:ClassName>

            <ps:HelpFile>Microsoft.Office.Server.Search.dll-help.xml</ps:HelpFile>

        </ps:Cmdlet>

 

My eyes see this just as below:

       <PowershellToAssembllyMapping>

            <PowerShellCmdName>What-I-Have</PowerShellCmdName>

            <C#NameAndLocation>What-I-Am-Looking-For</C#NameAndLocation>

            <Whatever>Whatever.xml</Whatever>

        </PowershellToAssembllyMapping>

Maps for Search, WSS and many more

OSSSearchCmdlets.xml file contains ps cmdlets to .NET assemblies mapping only for SharePoint Search.

But in the same location there is also another file called WSSCmdlet.xml that contains all kind of cmdlets mapping like

  • Enable-SPFeature
  • New-SPContentDatabase
  • Get-SPFarm
  • Etc.

Shortly everything that you can do with SharePoint Application using PowerShell.

 

If you just want to quickly check what those files contains I’ve uploaded them to my github. I put there also more files like for Reporting Services, Workflows etc. You can check it here.

Have you found useful this tip? Maybe you know alternative way? Share it in comments!

Thanks & Have a great day! 🙂

 

How to get all terms from Managed Metadata Service in SharePoint?

In this post I want to show you quick solution how to get all terms from Managed Metadata Service in Sharepoint Online

Problem

A colleague from my company asked me is there an easy way to get all terms from Managed Metadata Service in SharePoint Online. I told him:

– “sure, there is managed metadata app that allows you to navigate through whole managed metadata terms structure. Have you tried that?”

– “Yeah I know. But I need to export all terms, with their structure path and present them in list” (don’t really remember specific reason – suppose some data science probably). 

– “Oh, I see…”

Continue reading “How to get all terms from Managed Metadata Service in SharePoint?”

Clean inline html styling

Problem

I need to clean inline html styling from a text file

Solution

Just use below powershell line:

Get-Content .\someTextFile.txt | Foreach-Object {$_ -replace 'style=".*?"',""} >>newTextFile.txt

Explanation

In my case I’m making all kind of notes (short one, long ones, even blog posts drafts) in Microsoft Onenote (I will write some post about how to get all bests from this awesome tool). Since Onenote allows for simple formatting (headings, bolding, cursive etc) I very often use it’s built-in styling. Continue reading “Clean inline html styling”