TaxonomyFields in SPFx - reactjs

Currently I'm creating a new form in SPFx. When reading the fields of the list there is an taxonomyField.
I'm using the PnP Taxonomy control to display the taxonomy labels.
I would like to get the termset id from the taxonomy field and using the following code
var taxField = field as SP.Taxonomy.TaxonomyField;
ctx.load(taxField);
await new Promise((resolveTax, rejectTax) => {
ctx.executeQueryAsync(()=> {
let termSetID = taxField.get_termSetId();
console.log(termSetID);
resolveTax();
}, (sender,args) =>{
console.log("Could not retrieve taxonomyfield termsset id: " + args.get_message());
rejectTax();
});
});
}
I Always receive the following error:
TypeError: Cannot read property 'TaxonomyField' of undefined
at eval (eval at Type.parse (https://...sharepoint.com/_layouts/15/MicrosoftAjax.js:5:10143), :1:13)
at Function.Type.parse (https://...sharepoint.com/_layouts/15/MicrosoftAjax.js:5:10143)
at SP.ClientRequest.$3K_0 (https://...sharepoint.com/_layouts/15/SP.Runtime.js:2:51794)
at Array. (https://...sharepoint.com/_layouts/15/MicrosoftAjax.js:5:307)
at https://...sharepoint.com/_layouts/15/MicrosoftAjax.js:5:51370
at Sys.Net.WebRequest.completed (https://...sharepoint.com/_layouts/15/MicrosoftAjax.js:5:89652)
at XMLHttpRequest._onReadyStateChange (https://...sharepoint.com/_layouts/15/MicrosoftAjax.js:5:84251)
Anybody a suggestion to fix this?

I suggest to use open source tools to work with SharePoint's Taxonomy, for example react-taxonomypicker
This is an elegant Taxonomy Picker control built with TypeScript for React. Initially built for use in Office 365 / SharePoint.
Features:
Retrieve Terms from a Term Set by Term Set GUID.
Support for large Term Set using Async mode
Use SP.Taxonomy.js
Use Promise (polyfill it if needed IE)
You can test it here: https://jquintozamora.github.io/react-taxonomypicker/

Related

How do I make a query to get multiple content type in spfx? #pnp

I am making a project with React and Typescript (SPFX) and I have created a webpart with about 4 components and each component has a different Content type id. At the moment, I need to make a query search every time I want to get the CT information in each component, like this:
const searchNews = async () => {
const searchQuery: ISearchQuery = {
Querytext: [
'ContentTypeId:0x010100C568DBD9B2FDCC96666E9F2007948130EC3DB054237AF39005CF914A33608B74B8D05F01*',
'ANFHighlightOWSBOOL=1'
].join(' '),
RowLimit: 10,
SelectProperties: [
'Title',
'Path',
'ANFHighlightOWSBOOL',
'ANFImageOWSIMGE',
'AuthorOWSUSER',
'CreatedOWSDate'
]
};
They have mostly all the same SelectProperties, with specific differences.
I saw we can do a query in the webpart property pane to have a CT dynamic return so we can access it in the component. I also saw this tutorial: https://learn.microsoft.com/en-us/sharepoint/dev/spfx/dynamic-data But I am not quite sure how to make it and if there is a way to do it without de dynamic import.
How can I do a query in the property pane and make it usable in all my possible components?

geoFirestore - add new document with array as one of the document data property

While i try to add new document to the DB with geoFirestore and try to set one of the property as array with DocumentReference (refers to a document location in a Firestore database) i get the following error:
FirebaseError: Function DocumentReference.set() called with invalid data. Unsupported field value: a custom object
If i set there an empty array its success.
BTW if i set there a string or number its work too
The inseration code:
await geocollection.add({
coordinates: new firebase.firestore.GeoPoint(latlng.lat, latlng.lng),
arrayOfRef: [newRef]
});
I try to use Object.asset() to the newRef but get the same result.
Thanks for the helpers.
So in a lot of cases geofirestore minimally modifies the doc you give and then passes it through to Firestore. In this case it looks like Firebase/Firestore is throwing the error, not geofirestore.
I suspect that the issue is with the reference you're using. If that reference is provided by geofirestore, it's not necessarily a DocumentReference that Firestore uses, it is a GeoDocumentReference. You'll likely want to get the actual DocumentReference from the native property:
await geocollection.add({
coordinates: new firebase.firestore.GeoPoint(latlng.lat, latlng.lng),
arrayOfRef: [newRef.native]
});
Let me know if that helps! I'm also making a few assumptions as to your code, so seeing more of what happens before you call the add method would be awesome.

When should I use _id in MongoDB?

MongoDB has a field for every document called "_id". I see people using it everywhere as a primary key, and using it in queries to find documents by the _id.
This field defaults to using an ObjectId which is auto-generated, an example is:
db.tasks.findOne()
{
_id: ObjectID("ADF9"),
description: "Write lesson plan",
due_date: ISODate("2014-04-01"),
owner: ObjectID("AAF1") // Reference to another document
}
But in JavaScript, the underscore behind a field in an object is a convention for private, and as MongoDB uses JSON (specifically, BSON), should I be using these _ids for querying, finding and describing relationships between documents? it doesn't seem right.
I saw that MongoDB has a way to generate UUID https://docs.mongodb.com/manual/reference/method/UUID
Should I forget that _id property, and create my own indexed id property with an UUID?
Use UUIDs for user-generated content, e.g. to name image uploads. UUIDs can be exposed to the user in an URL or when the user inspects an image on the client-side. For everything that is on the server/not exposed to the user, there is no need to generate a UUID, and using the auto-generated _id is preferred.
An simple example of using UUID would be:
const uuid = require('uuid');
exports.nameFile= async (req, res, next) => {
req.body.photo = `${uuid.v4()}.${extension}`;
next();
};
How MongoDB names its things should not interfere in how you name your things. If data sent by third-party hurts the conventions you agreed to follow, you have to transform that data into the format you want as soon as it arrives in your application.
An example based in your case:
function findTaskById(id) {
var result = db.tasks.findOne({"_id": id});
var task = {
id: result._id,
description: result.description,
something: result.something
};
return task;
}
This way you isolate the use of Mongo's _id into the layer of your application that is responsible to interact with the database. In all other places you need task, you can use task.id.

key of the value is undefined with Firebase 4.5.2 and ionic 3

I'am using ionic 3 and firebase 4.5.2 to make an application. I have a project in firebase and I would like to add and delete some values in my list "shoppingItems". I can now retrieve the list view and add items.
Screen of my database
My problem I can't remove a task because the $key of my value is undefined.
I get my list like this :
My values are contained in my variable result ( is an array of the object item: which contain 2 string the value and the key).
Thank's
What version of angularfire2 are you using? I've been attempting to learn Angular and in running through a CRUD tutorial while running on angularfire2 v5.0, I discovered that valueChanges() does not return any metadata.
The following information is gleaned from the angularfire2 documentation located at https://github.com/angular/angularfire2/blob/master/docs/version-5-upgrade.md
Calling .valueChanges() returns an Observable without any metadata. If you are already persisting the key as a property then you are fine. However, if you are relying on $key, then you need to use .snapshotChanges() and transform the data with an observable .map().
The documentation does provide an example as well.
constructor(afDb: AngularFireDatabase) {
afDb.list('items').snapshotChanges().map(actions => {
return actions.map(action => ({ key: action.key,...action.payload.val() }));
}).subscribe(items => {
return items.map(item => item.key);
});
}
Hopefully, you've discovered the solution by now. But I thought I'd drop this here in the event someone else finds your post.

How to set Custom Dimension in Google Analytics using Angulartics

I am using Angulartics for track some information to Google Analytics from AngularJS.
I need to set a Custom Dimension, I need to do something like this but using Angulartics third party.
ga('set', 'dimension5', 'custom data');
In the documentation of Angulartics I just saw pageTrack() or eventTrack() methods. So I don't if it's possible?
Angulartics allows to set Custom Dimensions and Custom Metrics via setUserProperties({'dimension' + index : 'dimension-value'}). For Metrics you pass an object with the analogous data. The important thing is to use the prefix 'dimension' or 'metric' concatenated with the index of your custom dimension/metric. Indices for free users are [0,19], for premium users [0,199].
Valid examples of setting custom dimensions/metrics are:
$analytics.setUserProperties({'dimension1': 'dimension1-value'});
$analytics.setUserProperties({'dimension2': 'dimension2-value'});
$analytics.setUserProperties({'dimension3': 'dimension3-value'}); ...
$analytics.setUserProperties({'metric1': 'metric1-value'});
$analytics.setUserProperties({'metric2': 'metric2-value'});
$analytics.setUserProperties({'metric3': 'metric3-value'});...
A look into angulartics-google-analytics' source code shows:
$analyticsProvider.registerSetUserProperties(function (properties) {
// add custom dimensions and metrics
setDimensionsAndMetrics(properties);
});
function setDimensionsAndMetrics(properties) {
if (window.ga) {
// add custom dimensions and metrics
for(var idx = 1; idx<=200;idx++) {
if (properties['dimension' +idx.toString()]) {
ga('set', 'dimension' +idx.toString(), properties['dimension' +idx.toString()]);
}
if (properties['metric' +idx.toString()]) {
ga('set', 'metric' +idx.toString(), properties['metric' +idx.toString()]);
}
}
}
}
See Allow setting dimensions/metrics via setUserProperties()
As of version 0.1.2 angulartics.google.analytics supports setting custom dimensions for both page and event tracking.
You can set custom dimensions like this
$analytics.setUserProperties({dimension1: 'myValue'});

Resources