Forum Discussion
SPFx Tenant Properties Preview now available for First Release tenants
Thanks! Not sure what changed but it works now.
great to see the ever useful old 'property bag' resurface.
be good to see PnP JS Core encapsulate the GET request to make this neater and obviate uneeded code lines. hopefully in next release perhaps?
- Gautam ShethNov 21, 2017Brass Contributor
Hey kath patterson , have added the support for this endpoint in PnP js. It will be available in the next release which will be available around December.
Do update it when it becomes available.
After that, you can use the below code in your webpart:pnp.sp.web.getStorageEntity('SPFxTestKey').then(r => { console.log(r); });
- kath pattersonNov 21, 2017Iron Contributor
that's brilliant Gautam Sheth. I am a big fan of pnp js as a productivity tool and a way of encapsulating agile code such that it can move forward wthout breaking solutions or requiring any rework.
I will certainly try this out and post feedback. great job and keep going with this excellent toolset.
- kath pattersonNov 09, 2017Iron Contributor
just been having a run through with this and the powershell side was sweet, no problem. I have checked that I can retrieve the value.
but am a bit stuck with setting up the right typescript code to read the value back. I don't seem to be getting any data but that may be my code is wrong. Still learning Typescript.
Can anyone help and confirm correct coding for this basic read op? thanks hopefully.
private getTenantProperty(): Promise<ITenantProperty> {return this.context.spHttpClient.get(this.context.pageContext.web.absoluteUrl + `/_api/web/GetStorageEntity('Test1')`, SPHttpClient.configurations.v1).then((response: SPHttpClientResponse) => {var prop = response.json();return prop;});}export interface ITenantProperty{'@odata.null'?:boolean;Value:string;Description?:string;Comment?:string;}- kath pattersonNov 09, 2017Iron Contributor
sorted this out in the end and is all working nicely now. Its worth noticing that no json properties are returned for a key that does not exist. so that needs checking for. I guess there is a neat way to use the @odata.null for this but I don't know how so have gone for a simpler test.
export interface ITenantProperty{'@odata.null'?:boolean;Value?:string;Description?:string;Comment?:string;}private getTenantProperty(key :string):Promise<ITenantProperty>{if (DEBUG && Environment.type === EnvironmentType.Local) {Promise.resolve("LOCAL");}else{let prop:ITenantProperty = {Value:'fetching...'};return new Promise<ITenantProperty>((resolve,reject) => {let endpoint = Text.format("{0}/_api/web/GetStorageEntity('{1}')", this.context.pageContext.web.absoluteUrl,key);this.context.spHttpClient.get(endpoint, SPHttpClient.configurations.v1).then((response: SPHttpClientResponse) => {if(response.ok) {response.json().then((json:any) => {if(json.Value ===undefined){prop.Comment = key + "key not found";}else{prop.Value = json.Value;prop.Comment = json.Comment;prop.Description = json.Description;}resolve(prop);}).catch((error) => { reject(error); });}else {reject(response);}}).catch((error) => { reject(error); });});}}