2

If i write for example a node express app, i have the opportunity to pass some env vars from the server configuration.

Is there an opportunity in SharePoint Online also? As SharePoint Online is a managed service, there's imho no way to do so.

Is there possibly a workaround?

I stuck at creating any idea regarding this problem.

cheers Thomas

ThomasP
  • 35
  • 4

2 Answers2

2

Just as an option, you can use property bag (like, site property bag) to define custom properties for a site, and then get them from your SPFx web part. You have property bags for many things in SharePoint, including tenant, site, list, etc.

To set a property bag key/value for a site (powershell):

Connect-PnPOnline -Url https://<yourdomain>.sharepoint.com/sites/<yoursite>
Set-PnPPropertyBagValue -Key MYKEY -Value MYVALUE

To get this "site" value from an SPFx web part, you can use the allProperties API (somewhere in your web part initialization):

const propertyBag = await sp.web.allProperties.get();
const value = propertyBag.MYKEY;

console.log(value) // logs "MYVALUE"

The same code if you are not using pnpjs:

// context = <web part context>
const apiUrl = `${context.pageContext.web.absoluteUrl}/_api/web/allProperties`;
const rsp = await context.spHttpClient.get(apiUrl, SPHttpClient.configurations.v1);
const propertyBag = await rsp.json();
const value = propertyBag.MYKEY;

Alternatively, you can just put the .env file (to site assets for example) and then read it from your SPFx web part.

Not sure if this is common practice in any way (probably not), but just to give some options.

Nikolay
  • 10,752
  • 2
  • 23
  • 51
  • i'll give the propertyBag approach a try. Seems this is exact what I'm searching for. Thank you so much. – ThomasP Nov 19 '22 at 19:30
0

Can you expand on what variables you are trying to access?

  • SPFx has several "enviroment" properties available as part the context object
  • The properties in the root of your extension project can be changed when the extention is deployed using the app manifest file
  • The options listed above by Nikolay are options if the previous 2 don't work for you.

If you give a few more details or post a code snippet of what you are trying to accomplish, I'm sure you can find what you are seeking

Don Kirkham
  • 105
  • 9
  • Thanks alot for your answer. In my case the best fit is @nikolay 's. I want to configure at a site collection level, but the extension should be deployed at the tenant level, So I think the approach with the property bag should work fine. – ThomasP Dec 14 '22 at 21:01