Tutorials/Power Automate/Automate Contract Sorting in SharePoint with Azure AI and Power Automate
Power Automateintermediate

Automate Contract Sorting in SharePoint with Azure AI and Power Automate

Use a custom classifier built with Azure AI Document Intelligence to automatically tag uploaded legal documents with their contract type and confidence score.

NA
Narmer Abader
@narmer · Published June 3, 2026

Every law firm or legal department receives dozens of contracts daily. Manually reading each file to determine its client or type is slow and error-prone. Azure AI Document Intelligence can be trained to recognise different contract layouts, and Power Automate can trigger on new uploads, call the AI model, and write the classification result back to SharePoint metadata. This article walks through building exactly that.

We will create a custom classifier that distinguishes between Northwind Legal and Fabrikam Counsel contracts, train it in Document Intelligence Studio, and then build a Power Automate flow that runs whenever a file lands in a SharePoint library.

The steps are:

  • Prepare and label sample documents
  • Train and test a custom classification model
  • Set up a SharePoint document library with the right columns
  • Build a Power Automate flow that calls the classifier API
  • Update the file’s metadata with the result

The same pattern can be reused for any document types – invoices, forms, reports – by replacing the training set and model ID.

Scenario and Data Preparation

Imagine your team uploads contracts to a SharePoint library called ContractVault. You want each contract to be automatically tagged with:

  • ClientType (e.g., “Northwind Legal” or “Fabrikam Counsel”)
  • ConfidenceScore (a decimal number indicating how sure the AI is)

To train the classifier, collect at least five sample contracts per client. For this walkthrough we will use nine PDFs per client – one will be kept aside for final testing.

Building the Custom Classification Model

  1. Open Azure AI Document Intelligence Studio and select Custom classification model.

  2. Choose Create a project.

    • Project name: LegalContractClassifier (use your own naming conventions).
    • Azure subscription, resource group, Document Intelligence resource – create new ones if you do not have them.
    • API version: 2023-07-31 (3.1 GA) – this ensures compatibility with the Power Automate HTTP action.
    • Storage account and blob container: select or create a container where your training PDFs will be uploaded.
    • Folder path can be left blank.
  3. After the project is created, open it. Upload the Northwind and Fabrikam contracts (at least five each).

  4. On the label data screen, create two document types: NorthwindLegal and FabrikamCounsel.

  5. Select each PDF file one by one and assign the appropriate document type. The files appear under their type on the right-hand panel.

  1. Click Train.

    • Model ID: legalContractClassifier (you will need this in Power Automate).
    • Confirm and wait for training to complete (usually less than a minute).
  2. Switch to the Test tab and upload a contract that was not used in training. Click Run analysis. The model should return the correct document type and a confidence score.

Good to know

If the confidence seems low, add more representative samples or check that the documents are clean and similar in layout.

SharePoint Library Configuration

In your SharePoint site, create a new document library named ContractVault (or use an existing one). Add two extra columns:

  • ClientType – Single line of text
  • ConfidenceScore – Number (decimal might be useful, but you can keep it as whole number by multiplying by 100)

You can also keep the default Name column for the file name.

Power Automate Flow: Step-by-Step

We will build an automated cloud flow triggered when a file is created in ContractVault.

Trigger and Get File Content

  1. Trigger: SharePoint – When a file is created (properties only). Select your site and library.
  2. Action: SharePoint – Get file content using path.
    • Site Address: your site
    • Path: use the Path dynamic content from the trigger.

Classify the Document via HTTP POST

Document classification is not a built-in Power Automate action, so we use a generic HTTP action.

textHTTP Action – POST
Method: POST
URI: https://{endpoint}/formrecognizer/documentClassifiers/legalContractClassifier:analyze?api-version=2023-07-31
Headers:
Content-Type  application/json
Ocp-Apim-Subscription-Key  {your Key} (from Azure portal – Keys and Endpoint)
Body:
{
"base64Source": "@{base64(body('Get_file_content_using_path'))}"
}

Store the Result ID

The classifier processes asynchronously. The POST response header contains an apim-request-id. We need it to fetch the final result.

Add a Compose action after the HTTP POST and name it Get Result ID.

textCompose: Result ID
outputs('HTTP_Post_Classify')['headers']?['apim-request-id']

Replace HTTP_Post_Classify with the actual name of your HTTP action.

Wait and Retrieve the Classification

Add a Schedule – Delay action set to 10 seconds (increase if your documents are large).

Then add a second HTTP action to perform a GET.

textHTTP Action – GET
Method: GET
URI: https://{endpoint}/formrecognizer/documentClassifiers/legalContractClassifier/analyzeResults/@{outputs('Get_Result_ID')}?api-version=2023-07-31
Headers:
Content-Type  application/json
Ocp-Apim-Subscription-Key  {your Key}

Extract Document Type and Confidence

Parse the GET response to grab the classification details. Create two Compose actions:

textCompose: Document Type
first(body('HTTP_Get_Result')?['analyzeResult']?['documents'])?['docType']
textCompose: Confidence Score
first(body('HTTP_Get_Result')?['analyzeResult']?['documents'])?['confidence']

The confidence is returned as a decimal between 0 and 1. You can multiply it by 100 if you prefer a percentage.

Update SharePoint Metadata

Add the SharePoint – Update file property action.

  • Site Address: your site
  • List or Library: ContractVault
  • Id: ID from the trigger
  • Title (optional)
  • ClientType: dynamic content from the Document Type Compose
  • ConfidenceScore: dynamic content from the Confidence Score Compose
Performance note

The GET request may fail if the processing has not finished. Wrap the flow in a Scope with a Configure run after set to “has failed” and add a retry loop, or increase the delay.

Security and Governance

  • Store the API key in Azure Key Vault and use the Key Vault – Get secret action, or store it as a plain text variable only if the flow environment is restrictive.
  • Consider using a Managed Identity for the Document Intelligence resource – the HTTP action can then authenticate with Azure AD instead of a key.
  • Do not hardcode the endpoint or model ID; use environment variables or a custom connector.

Common Troubleshooting

IssueLikely causeSolution
HTTP 401 / 403Invalid subscription key or endpoint.Verify key in Azure portal → Keys and Endpoint.
HTTP 404 (POST)Wrong API version or model ID.Confirm API version is 2023-07-31. Double‑check model ID in Document Intelligence Studio.
Low confidence scoreNot enough training samples, or samples are very different from the new document.Add more labelled files, ensure all documents are scanned at similar quality.
Flow fails with timeoutDelay too short for large files.Increase the Delay to 20 or 30 seconds, or implement a “Loop until” pattern.
No document type returnedThe classifier could not identify any known type.Check that the uploaded file is a supported format (PDF, image, etc.).

Final Recommendation

Custom document classification saves hours of manual sorting and reduces human error. The combination of Azure AI Document Intelligence and Power Automate is flexible: you can train classifiers for invoices, contracts, medical forms, or any other document category. Start with a small representative set of files, test thoroughly, and iterate.

References