Sharepoint SPFx spuser getting addition fields - sharepoint

I'm using the SPFx with React in Typescript and able to get the SPUser, but it only has a couple of fields like displayName, email, and loginName. Is there a way to get the Department of the user? Or do I have to use PNP-JS?
Example how I get the displayName:
this.props.context.pageContext.user.displayName
Is there a way to get other fields of the user?

You could use REST api also.
Sample script:
this.context.spHttpClient.get(this.context.pageContext.web.absoluteUrl + `/_api/sp.userprofiles.peoplemanager/GetMyProperties`, SPHttpClient.configurations.v1)
.then((response: SPHttpClientResponse) => {
var data= response.json();
console.log(data);
});

Related

Hubspot API - list of properties needed (nodejs)

I am integrating the hubspot API to track user interaction with our site. I am creating dynamic lists and I want to filter a user into a certain contact list by which URL they visit.
"filters": [
[
{
"operator": "CONTAINS",
"property": "hs_URL",
"value": `${id}`
},
],
]
I keep getting this error for all my attempts:
{"status":"error","message":"Couldn't find a Property with the given name 'hs_URL'","correlationId":"0723dcee-534b-4f92-9104-509d6885abbe","propertiesErrorCode":"PROPERTY_NOT_FOUND"},
I cannot seem to find a master property list and have tried many string combinations. Anyone familiar with hubspot property lists would be my savior.
Thank you~!
It's been a few months, so you may not need this anymore, but since I landed here while looking for a way to get all the properties from an object type in hubspot using nodejs, this might help others looking for the solution.
The master list of properties can be retrieved with the following API call:
const response = await hubspotClient.crm.properties.coreApi.getAll(objectType, false);
The arguments for getAll() expect:
objectType: a string, i.e "contacts".
archived: a boolean, i.e false. Set this true if you want to get archived properties.
The following code was adapted based on this page from the hubspot API docs:
https://developers.hubspot.com/docs/api/crm/properties
Once you're on the page, you can click on the "Endpoints" Tab to reveal code snippets for multiple environments, including nodejs.
For this example, getProperties(), retrieves all properties for a given object type. I used contacts for the object type, which I believe is where you are storing the url property, but you could use the same function to get properties for other object types such as companies or deals.
It might be worth noting that I mapped the results to return just the property names, which sounds like all you need for your case, but more information is contained in the results if you need it. Just remove this bit to get more information on each property:
.map(prop => prop.name)
const hubspot = require('#hubspot/api-client')
const hubspotClient = new hubspot.Client({ apiKey: "YOUR_API_KEY" })
const getProperties = async (objectType) => {
try {
const response = await hubspotClient.crm.properties.coreApi.getAll(objectType, false);
return response.body.results.map(prop => prop.name);
} catch (e) {
e.message === 'HTTP request failed'
? console.error(JSON.stringify(e.response, null, 2))
: console.error(e);
}
}
Here's an example for running the function to get a list of all property names for contacts.
(async () => {
var properties = await getProperties("contacts");
console.log(JSON.stringify(properties ,null,2));
})();
It took me a bit to find this, so figured I would post here in the hopes it saves time for someone else. This is the first time I've posted a solution, and I'm pretty new to this API and Hubspot in general, so feedback and/or better solutions are welcome. Cheers.

Microsoft Authentication Library(MSAL) not working with PNPJS to create SharePoint List Item

Using below article we should be able to perform access & update SharePoint data.
Calling SharePoint via MSAL
But currently we can only access the data but not able to update/write data to SharePoint list.
Here is the code snippet which we are using in our solution.
import { MsalClientSetup } from "#pnp/msaljsclient";
import { sp } from "#pnp/sp/presets/all";
sp.setup({
sp: {
baseUrl: "https://{my tenant}.sharepoint.com/sites/dev/",
fetchClientFactory: MsalClientSetup({
auth: {
authority: "https://login.microsoftonline.com/mytentant.onmicrosoft.com/",
clientId: "00000000-0000-0000-0000-000000000000",
redirectUri: "https://mytentant.sharepoint.com/sites/dev/SitePages/test.aspx",
},
}, ["https://mytentant.sharepoint.com/.default"]),
},
});
const r = await sp.web();
//create list item
await sp.web.lists.getByTitle('FoodOrder')
.items
.add({
Title: 'Chicken Fry',
Quantity: 5,
Remarks: 'Extra Spicy'
})
.then(async results => {
console.dir(results);
})
.catch(err => {
console.error(err.message);
});
Below are the permissions granted through Azure APP.
When we are trying to create/update item in SharePoint list it throws the below error.
Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
It will be great helpful if any one have solution for this. Any ideas will be appreciated, thanks a lot.
It looks you need to allow your application to write to the lists. Just give it permission to do so (you need AllSites.Write). The ClientId should not be "0000000000-00....", it should be your app id, the id of the app to which you give the permissions. "0000-..." is just a placeholder.
Like the documentation says:
When calling the graph API you must specify the scopes you need and
ensure they are configured in AAD
So basically you should do just that:
https://learn.microsoft.com/en-us/graph/auth-register-app-v2

Podio Filter item method response missing files details

In nodejs I am trying to filter podio data by using FilterItems its working fine but its not giving files attributes. I can get the files count but I need uploaded files details. Here is my sample code
var Podio = require('podio-js').api;
var podio = new Podio({
authType: 'server',
clientId: 'XXXXX',
clientSecret:'*****************'
});
podio.authenticateWithApp('XXXXX', 'YYYYYYYYYYYYYY', function(err) {
podio.request('POST', '/item/app/XXXXX/filter', {
"filters": { "email":'sample#gmail.com'}
}).then(function(responseData) {
console.log(responseData);
}).catch(function(e) {
console.log(e);
});
});
To solve this problem am doing one more call Get Item by using Item-id (Which I have received from filter call).
Here my question is why filter method is not giving files details whether its bug in podio filter call or is there any specific reason. Please suggest better way too.
Note: In my scenario I should use only filter functionality;
You can get additional fields (including files) by bundling responses using fields parameter. It's described here: https://developers.podio.com/index/api
Sample:
// Include files when getting filtered items
/item/app/{app_id}/filter/?fields=items.fields(files)

Inserting a user into a person field in sharepoint using the rest api

I'm trying to update a list item in a remote sharepoint site using the rest api from a workflow. I'm having issues understanding how to populate a person field. I've looked online and read that you should use the id of the user and not the login, however what if I do not know the users id? Where can I get this from?
I've been reading the following link but it doesn't explain where the id comes from
how to add user to sharepoint list item user field using REST api in sp2013?
You will be able to view all UserProfile Properties by using this REST call:
http://siteurl/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=#v)?#v='domain\user'
To get a specific Property (in this case UserProfileGUID) use:
http://siteurl/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=#v,propertyName='UserProfile_GUID')?#v='domain\user'
If you are using CSOM this might help you:
The proper way to write a SharePoint User to a User Field in a SharePoint list
I have done this successfully in the past using both CSOM (js) and SSOM (powershell).
I know its a a bit late but for anyone looking for the answer on how to get the user id use the below function
function getUserId(userName) {
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/siteusers?$select=Id&$filter=Title eq '" + userName + "'",
type: 'GET',
headers: {
"Accept": "application/json;odata=verbose"
},
success: function (data) {
return data.d.results[0].Id;
},
error: function (error) {
console.log(error);
}
});
}

Hoodie - Update a CouchDB document (Node.js)

I'm handling charges and customers' subscriptions with Stripe, and I want to use these handlings as a Hoodie plugin.
Payments and customer's registrations and subscriptions appear normally in Stripe Dashboard, but what I want to do is update my _users database in CouchDB, to make sure customer's information are saved somewhere.
What I want to do is updating the stripeCustomerId field in org.couchdb.user:user/bill document, from my _users database which creates when logging with Hoodie. And if it is possible, to create this field if it does not exist.
In hoodie-plugin's document, the update function seems pretty ambiguous to me.
// update a document in db
db.update(type, id, changed_attrs, callback)
I assume that type is the one which is mentioned in CouchDB's document, or the one we specify when we add a document with db.add(type, attrs, callback) for example.
id seems to be the doc id in couchdb. In my case it is org.couchdb.user:user/bill. But I'm not sure that it is this id I'm supposed to pass in my update function.
I assume that changed_attrs is a Javascript object with updated or new attributes in it, but here again I have my doubts.
So I tried this in my worker.js:
function handleCustomersCreate(originDb, task) {
var customer = {
card: task.card
};
if (task.plan) {
customer.plan = task.plan;
}
stripe.customers.create(customer, function(error, response) {
var db = hoodie.database(originDb);
var o = {
id: 'bill',
stripeCustomerId: 'updatedId'
};
hoodie.database('_users').update('user', 'bill', o, function(error) {
console.log('Error when updating');
addPaymentCallback(error, originDb, task);
});
db.add('customers.create', {
id: task.id,
stripeType: 'customers.create',
response: response,
}, function(error) {
addPaymentCallback(error, originDb, task);
});
});
}
And between other messages, I got this error log:
TypeError: Converting circular structure to JSON
And my file is not updated : stripeCustomerId field stays null.
I tried to JSON.stringify my o object, but It doesn't change a thing.
I hope than some of you is better informed than I am on this db.update function.
Finally, I decided to join the Hoodie official IRC channel, and they solved my problem quickly.
Actually user.docs need an extra API, and to update them you have to use hoodie.account instead of hoodie.database(name)
The full syntax is:
hoodie.account.update('user', user.id, changedAttrs, callback)
where user.id is actually the account name set in Hoodie sign-up form, and changedAttrs an actual JS object as I thought.
Kudos to gr2m for the fix; ;)

Resources