SharePoint Search Query Tool Login

If you have ever worked with SharePoint search you likely already know about the SharePoint Search Query Tool. If you are new to SharePoint and need a little insight into the SharePoint search experience this tool is a lifesaver!

SharePoint Query Tool GitHub: https://github.com/pnp/PnP-Tools/

In future posts, I will outline how to form queries and use the tool but for now, I want to simply connect to my SharePoint Online site.

Enter the URL for your SharePoint site, select the Authentication options shown above, then click Sign-In. If a web login form appears be sure to complete it. If your normal Windows login doesn’t work, try using your work email address and password, and if that doesn’t work try your work email address and App Password.

App Passwords are created and managed at this URL: https://account.activedirectory.windowsazure.com/AppPasswords.aspx

Power Automate Bad Gateway Error

I was trying to use a SQL Insert Row action to insert a new row in a SQL Server table and received a Bad Gateway error. First, I thought it was a permissions issue, then I thought my Flow stopped working…

Turned out to be an issue with the amount of data being inserted into a field. One SQL column was set to varchar(X) and the Flow was trying to insert more characters than X.

Flow Power Automate and SharePoint Required Fields

On the surface, this request sounded super simple and straightforward. “we need to copy files from a SharePoint library to Blob storage.” Simple enough? Well, yes, but the SharePoint library has a couple of required fields and a Flow is triggered by an action.

Consider what I’m outlining below to be version ONE of the process. In the near future, I will update this post with a slightly more resilient solution.

My SharePoint library has a required field titled DesinationFolder

Context of what I’m doing in the Flow:
Trigger: When files is created in a folder
When a file is added to a library the flow is triggered
Get file metadata
File Identifier: Use File identifier from the step above
Get file properties
Id: Use the ItemId from the previous step
Initialize variable
Name: vCheckedOut
Type: Boolean
Value: Checked out (field from Get properties)
Initialize variable
Name: vFolderPath
Type: String
Value:
Condition
vCheckedOut is equal to true
Yes:
Do until
vCheckout is equal to False
GetFileProperties
Set variable
Name: vCheckedOut
Value: Checked out (value from the Get file properties above)
No:
Set variable
Name: vFolderPath
Value: FolderPath (SharePoint field)

Compose
/blobfolder/vFolderPath (variable)
Create blob

Get-PnPSearchCrawlLog Filter to a List or Library

How do you use the Get-PnpSearchCrawlLog cmdlet to check if the SharePoint crawler is indexing your site, list / library, folder, or item / file?

Before you begin, you’ll want to make sure you have access to the Crawl Log: https://yourSite-admin.sharepoint.com/_layouts/15/searchadmin/crawllogreadpermission.aspx

Connect-PnPOnline -Url "https://sharepointed.sharepoint.com/sites/food" -interactive

$logs = Get-PnPSearchCrawlLog  -filter "https://sharepointed.sharepoint.com/sites/food/Lists/tacos/" -RawFormat

foreach($l in $logs)
{
    Write-Host "    "
    Write-Host $l.Url
    Write-Host $l.ItemId
    Write-Host $l.LogLevel
    Write-Host $l.CrawlTime
}

Get-PnPSearchCrawlLog details: https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/get-pnpsearchcrawllog

Other examples:

#get all items crawled in the Shared Documents library
Get-PnPSearchCrawlLog  -filter "https://sharepointed.sharepoint.com/sites/TestSite/Shared Documents/" -RawFormat

#get items in  Folder A
Get-PnPSearchCrawlLog  -filter "https://sharepointed.sharepoint.com/sites/TestSite/Shared Documents/Folder A/" -RawFormat

#check if a single file is indexed
Get-PnPSearchCrawlLog  -filter "https://sharepointed.sharepoint.com/sites/TestSite/Shared Documents/Folder A/test.txt" -RawFormat

#get everything crawled in the past 30 days for a target site
Get-PnPSearchCrawlLog  -filter "https://sharepointed.sharepoint.com/sites/TestSite/" -StartDate (Get-Date).AddDays(-30) -RawFormat

If you receive this error:
Get-PnPSearchCrawlLog: The current connection holds no SharePoint context. Please use one of the Connect-PnPOnline commands which uses the -Url argument to connect.
This means you did not READ the second thing I mentioned at the top of this post. The account running the Get-PnpSearchCrawlLog command will need permission to access the crawl log. You will need to access this site with an admin account or an account with enough permissions to get in trouble:

https://yourSITE-admin.sharepoint.com/_layouts/15/searchadmin/crawllogreadpermission.aspx

Replace yourSite with your tenant’s name.

System.MissingMethodException: Method not found Connect-PnPOnline

Using Visual Studio Code and SharePoint PNP I was trying to make some updates to a list but I wasn’t able to connect to a site.

Connect-PnPOnline -Url "https://taco.sharepoint.com/" -Credentials $creds

Error I was receiving:
System.MissingMethodException: Method not found: ‘System.Runtime.Remoting.ObjectHandle System.Activator.CreateInstance(System.String, System.String)’. at SharePointPnP.PowerShell.Commands.Base.ConnectOnline.ProcessRecord() at System.Management.Automation.CommandProcessor.ProcessRecord()

I tried uninstalling VScode, removed all traces of SharePoint from my laptop, and cleared the GAC. Nothing worked.

Here is what did work:
In VScode:

  1. Open the Command Palette on Windows or Linux with Ctrl+Shift+P. On macOS, use Cmd+Shift+P.
  2. Search for Session.
  3. Click on PowerShell: Show Session Menu.
  4. Choose one of the ___ (x86) options

Not sure how, but I was using an x64 session and SharePoint PNP clearly didn’t like that.

Edit: Updated VScode to the latest version and it managed to reset my session settings. When this happened, it caused my CSOM scripts to report a The remote server returned an error: (400) Bad Request error. The fix above will resolve the error.

How to use an iFrame in a modern SharePoint Online page

Using the Embed web part I was trying to paste in a site URL when I should have been using the iFrame HTML tag.

example:

<iframe src="https://sharepointed.com" height="200" width="300"></iframe>

If you encounter this error: This website doesn’t support embedding using just the address ….

You will need to update the HTML Field Security settings in the Site Settings area of your site. In my case, I simply added sharepointed.com to the allow iframes from this domain list, then updated the web part again.

Connect to SharePoint Online Using PowerShell

Update and a much better way to approach this:
Use a SharePoint App Only Client Id and Secret to access the site, list, or library.

Microsoft documentation:
https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs
You can create an app principle that is limited to a single site, list, library, or a combination of them:
https://piyushksingh.com/2018/12/26/register-app-in-sharepoint/

 $sampleConnect = Connect-PnPOnline -Url "https://YOURsite.sharepoint.com/sites/parent/child" -AppId "12345-94c3-4149-bda5-abcedffadsf" -AppSecret "643r4er5sfdadsfadsfdsf=" -ReturnConnection

Write-Host  $sampleConnect.Url
In this example, I’m connecting to a Site Collection on my tenant.

Assumptions:
1) You have created a token in your o365 site
1.1) https://portal.office.com/account/
1.2) On the left site of the page click Security & privacy, then click Create and manage app passwords
1.3) In the app password page click the create button and give it a name.
1.4) Save the password to a secure location.
1.5) There is a better way of doing this that I will cover in a future post.
2) You have downloaded to CSOM DLL(s) from Nuget

Clear-Host

$userName = "me@sharepointed.com"
$pw = "abc123taco"  # I"M USING AN APP PASSWORD 
$siteCollectionUrl = "https://sharepointed.sharepoint.com/sites/taco"

#Secure the password
$securePassword = ConvertTo-SecureString $pw -AsPlainText -Force

Add-Type -Path "C:\Code\DLL\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Code\DLL\Microsoft.SharePoint.Client.Runtime.dll"

#Create Context
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteCollectionUrl)

#Authorise
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $securePassword)

$web = $ctx.Web
$properties = $web.AllProperties
$ctx.Load($web)
$ctx.Load($properties)
$ctx.ExecuteQuery()

Write-Host " Site Collectione URL: $($web.Url)"
Write-Host "Properties are "

foreach ($prop in $properties) {
    $prop.FieldValues
}

Use Flow to Update SharePoint Hyperlink Field

If you want to update a hyperlink field in SharePoint Online using Flow, you will need to use the Send an HTTP request to SharePoint action (for now). I first tried using the Update item Flow action, but it would not correctly set the URL and Description values. Another issue I had was using the Post method instead of Patch. Every blog/forum post was suggesting the use of the Post method, but the Patch method is what I ended up using.

For testing, create a list titled Contractors. In the list, add a Hyperlink field titled website. In your Flow, add a Send HTTP request to SharePoint action.

Site Address: select the site you created the Contractors list in.
Method: PATCH
Uri: _api/web/lists/getbytitle(‘Contractors’)/Items(‘ID From above Action’)
Headers
Accept application/json
Content-Type application/json; odata=verbose
X-HTTP-TYPE MERGE
IF-MATCH *
Body

{
'__metadata': { 'type': 'SP.Data.ContractorsListItem' },
   'website':
   {
          '__metadata': {'type':'SP.FieldUrlValue'},
               'Description':'Title from above Action',
                'Url': 'http://www.bbb.com'
  }
}

Peek code:

{
    "inputs": {
        "host": {
            "connection": {
                "name": "@parameters('$connections')['shared_sharepointonline']['connectionId']"
            }
        },
        "method": "post",
        "body": {
            "method": "PATCH",
            "uri": "_api/web/lists/getbytitle('Contractors')/Items('@{triggerBody()?['ID']}')",
            "headers": {
                "Accept": "application/json",
                "Content-Type": "application/json; odata=verbose",
                "X-HTTP-TYPE": "MERGE",
                "IF-MATCH": "*"
            },
            "body": "{\n'__metadata': { 'type': 'SP.Data.ContractorsListItem' },\n   'website':\n   {\n          '__metadata': {'type':'SP.FieldUrlValue'},\n               'Description':'@{triggerBody()?['Title']}',\n                'Url': 'http://www.bbb.com'\n  }\n}"
        },
        "path": "/datasets/@{encodeURIComponent(encodeURIComponent('https://EnterYourURL.sharepoint.com/sites/spdev'))}/httprequest",
        "authentication": "@parameters('$authentication')"
    },
    "metadata": {
        "flowSystemMetadata": {
            "swaggerOperationId": "HttpRequest"
        }
    }
}

Get 5 Most Recently Update Items From ALL Lists in a FARM

A wise man recently asked me how I’d go about retrieving the five most recent modified items from every list in a farm. Fun question, and there are a couple of ways of going about this, but here is what I came up with.

Things to note: 1) on prem farm with more than one web app. 2) if you are dealing with a large farm, I’d suggest chunking out the Get-SPSite -limit all and the $site.allwebs into to smaller data sets. 3) script needs comments and error handling or reporting

Clear-Host

if ($null -eq (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue)) {
    Add-PsSnapin Microsoft.SharePoint.PowerShell
}

$sites = Get-SPWebApplication | Get-SPSite -limit all 

foreach ($site in $sites) {
    foreach ($web in $site.AllWebs) {
        $lists = $web.Lists
        foreach ($list in $lists) {
            $query = New-Object Microsoft.SharePoint.SPQuery 
            $query.Query =  
            "<Query>
                <OrderBy>
                  <FieldRef Name='Modified' Ascending='False' />
                </OrderBy>
            </Query>" 
            $query.RowLimit = 5
            $listItems = $list.GetItems($query)
            
            if ($listItems) {
                foreach ($lIem in $listItems) {
                    Write-Host "Web: $web -- List: $list -- Item: $($lIem.Name) "
                }
            }
        }
        if ($web) {
            $web.Dispose()
        }
    }
}

Using SharePoint Keyword Query to Search Across Site Collections

Quick and easy way to search for an item across site collections. I would suggest using one of the Keyword query tools to fine-tune your queries. Also note that SharePoint will limit your search results to 10,000 items, but you can page your results and cycle through them. In the example below, I’m searching across all the site collections off of the /sites/ managed path. With the returned dataset, I’m looping through the rows getting the SPFile of each row.

$site = New-Object Microsoft.SharePoint.SPSite "https://example.site.com"

$keywordQuery = New-Object Microsoft.office.Server.Search.Query.KeywordQuery $site

$queryText = "SomeField:Taco AND Path:https://example.site.com/sites/*"
$keywordQuery.QueryText = $queryText
$keywordQuery.TrimDuplicates = $false
$searchExec = New-Object Microsoft.Office.Server.Search.Query.SearchExecutor
$searchResults = $searchExec.ExecuteQuery($keywordQuery)

$dTable = $searchResults.Table[000].Table.Rows

foreach($row in $searchResults.Table[000].Table.Rows)
{
      $web = Get-SPWeb $row.SPWebUrl
      $file = $web.GetFile($row.Path)
      Write-Host $file.ServerRelativeUrl
}