Introduction
While working with SharePoint, we most times need to create test content related to a specific topic. OpenAI API is the best fit in this case to generate content related to any topic.
In this article, we will explore how OpenAI API can be combined with Power Automate to generate content in SharePoint.
Get the API Key
Follow the below steps to get the API key to work with Open AI APIs.
- Navigate to OpenAI. Sign up, or register to proceed.
- From your profile, select View API Keys.
- Click + Create new secret key to create one. Note down the secret key for future use.
Open AI Playground
Before we start using the Open AI functionality in our application, the best place to play and test it, is the Open AI Playground.
![](https://nanddeepnachanblogs.com/media/2023-02-12-test-docs-spo-openai-power-Automate/03.png)
Once you are happy with evaluating the functionality, click View code. The available code options are python, node.js, curl, and json.
The curl
option will provide you with more information on passing header information. We will use json
format for our Power Automate flow.
![](https://nanddeepnachanblogs.com/media/2023-02-12-test-docs-spo-openai-power-Automate/04.png)
Power Automate
We will now develop a Power Automate flow to make a request to OpenAI API. We will make 2 calls to OpenAI APIs to generate a document (.txt) in SharePoint.
- Generate a given number of random words.
- Write an essay for each of the generated random words.
Follow the below steps to set up the Power Automate flow:
- We will start by creating an instant cloud flow and add input as a Question. In the next step, we will define a variable to store the “Number of documents” to generate.
- In the next step, we will use
HTTP
action to post the request to OpenAI API. This will help us to get the random words from OpenAI. - Run the flow manually to see the result. Copy the Body from the output of the HTTP request.
- Now, we will use Parse JSON to extract the output.
- In the next step, let us initialize a variable with the below expression to store the response from Open AI (i.e., randomly generated comma-separated words).
replace(first(body('Parse_JSON')?['choices'])['text'],decodeUriComponent('%0A'),'')
- Further, let us split the comma-separated words string into an array stored in a variable.
- Using the Apply to each action, we will iterate through each word by calling OpenAI API to write an essay on it.
- Finally, the test document (.txt) can be created in the SharePoint document library using Create file activity.
Use the below formulas:
Parameter | Formula |
---|---|
File Name | concat(items('Apply_to_each'), '.txt') |
File Content | replace(first(body('Parse_JSON_Essay_Response')?['choices'])['text'],decodeUriComponent('%0A'),'') |
End Result
After running the Power Automate, the test documents will be generated in the SharePoint document library as follows.
![](https://nanddeepnachanblogs.com/media/2023-02-12-test-docs-spo-openai-power-Automate/10.png)
Summary
OpenAI API is the best fit to generate content related to any topic. This feature can be combined with Power Automate to generate content in SharePoint.
References
About the Author:
Hello! I’m Nanddeep Nachan.
I am from Pune, Maharashtra, India. Microsoft MVP (Office Apps & Services), Microsoft Certified Trainer (MCT). I am happily married and living with my loving family (Parents, caring sister, lovely wife, and a sweet newborn junior).
I born in a small town Khopoli, cocooned amidst the Sahyadri mountains. Spent my childhood at Matheran, a cozy little hill station. Eventually moved to Mumbai for higher educations and landed in Pune for the job.
I like reading historical books and a big fan of Sherlock Holmes. Photography is my recent developed hobby and I have perceived a Diploma in Photography from Shaw Academy (UK).
On a technical note, I’m a results oriented Technology Architect with experience in Microsoft Technologies especially with SharePoint, MS Azure and .NET. Experienced in design, implementation, configuration, and maintenance of several large-scale projects.
Organizer and frequent speaker at various industry seminars, conferences, and community events including SPS, Global Microsoft 365 Developer Bootcamp, and Global Power Platform Bootcamp.
Reference:
Nachan, N. (2023). Create Test Documents in SharePoint with OpenAI APIs and Power Automate. Available at: https://nanddeepnachanblogs.com/posts/2023-02-12-test-docs-spo-openai-power-Automate/ [Accessed: 2nd May 2023].