0

I want to write a "simple" application that reads PDF files on the SharePoint site of the nonprofit I volunteer for, extracts an attribute (let's take "Subject" as an example) and writes it into the custom column "Subject" that I've created in my SharePoint folders.

I've been reading web sites all morning on this, and quite frankly it's overwhelming. There are so many options for how to do this, and it's very hard to tell which are obsolete and of those that aren't, which are actually easy for my intended purpose.

There are web sites that recommend using System.Net.NetworkCredential (which I'm 98% sure no longer works), there's the SharePoint REST API (which I guess underlies everything, once you've got a token), Microsoft Graph API, SharePoint Web Services (which are probably obsolete), and CSOM.

The PnP Framework lives, I think, on top of CSOM, and it claims it will make my life easier, but its "getting started" section talks about a Microsoft 365 tenant, whatever that is. It's got a nice introductory YouTube video, but it has sections on "PnP Framework" and "PnP Core" but no indication how to know which one applies to which user needs. It's also got sample code that omits 90% of what you need to know and assumes you already have an appsettings.json, azureAppId, certificatePassword, certificatePath, and azureTenantName.

I realize that I could read the entire OAuth 2 spec, and for some reason folks recommend that for newbies, but that spec covers 1,000 scenarios and I have no idea which parts apply to what I'm doing.

This stack exchange question looks possible, but the answer points me to the Microsoft "auto code flow" which seems like good information but is at such a high level, I can't figure out what code I need to write.

So yeah, I'm an OAuth 2 newbie.

So folks who have done this kind of thing before - if you were tasked with the application I mentioned (copying PDF "Subject" attribute into a custom column in SharePoint), which technology would you use? Should I skip Graph API (which I've used before for a different purpose and wasn't too impressed with) and go with PnP Framework instead?

EDIT

@grisha, your answer is amazing! You definitely have the exact knowledge I was hoping to find.

I've been a software engineer for 40 years, but almost exclusively in the Windows desktop environment. I recently started setting up some pipelines in Azure DevOps for work so I'm familiar with the Azure environment.

I created an Azure account for the nonprofit, and now I'm facing the home page where Azure helpfully offers about a million things it can do for me. Where do I start?

1
  • To better help you solve your problem, it is recommended that you post a new thread for your new problem.
    – Xyza_MSFT
    Commented Nov 18 at 8:28

1 Answer 1

1

talks about a Microsoft 365 tenant, whatever that is

Dear Betty, I think you should start with that part before you touch oAuth. Microsoft 365 tenant is where your SharePoint is. You will need a SharePoint administrator to do most of the steps required.

The second step, as you rightly pointed out, is choosing the right solution.

  1. The C# desktop application sounds the hardest to do in the described situation
  2. If there are no requirements for doing it in a desktop app, go online.
  3. Online, you can create a simple app in Azure or a Power Platform flow. For extra buck, there is Syntex which will do " extracts an attribute (let's take "Subject" as an example) and writes it into the custom column "Subject" " in about 10-20 clicks
  4. If the extra buck is available - pick Syntex, you will love it
  5. If not, pick Power Platform
  6. If you have an Azure Subscription - go there and use the same code as for desktop.
  7. Your question wanted a simple application, so here it is. Just copy the code and you can use it in ISE, VSCode or other tool you use for programming:
Install-Module -Name "PnP.PowerShell" -Scope CurrentUser
# Import the PnP PowerShell module
Import-Module PnP.PowerShell

# Variables
$siteUrl = "https://yourtenant.sharepoint.com/sites/yoursite"
$libraryName = "Documents" # Replace with your library name
$searchWord = "Betty"
$customPropertyName = "CustomProperty" # Replace with the exact name of your custom property

# Connect to the SharePoint site
Connect-PnPOnline -Url $siteUrl -Interactive

# Get all files in the document library
$files = Get-PnPListItem -List $libraryName -PageSize 1000 | Where-Object { $_.FileSystemObjectType -eq "File" }

foreach ($file in $files) {
    $fileName = $file.FieldValues.FileLeafRef
    $fileContent = Get-PnPFile -Url $file.FieldValues.FileRef -AsString

    # Search for the word "Betty" in the file content
    if ($fileContent -match "\b$searchWord\b") {
        Write-Output "Word '$searchWord' found in $fileName"

        # Update the custom property if the word is found
        $properties = @{
            $customPropertyName = "Betty found in file"
        }

        Set-PnPListItem -List $libraryName -Identity $file.Id -Values $properties
    }
    else {
        Write-Output "Word '$searchWord' not found in $fileName"
    }
}

# Disconnect from SharePoint
Disconnect-PnPOnline
1
  • this is great, thank you! I added a new question to the body of my initial post Commented Nov 9 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.