Hello. In my project I need to check if current user
I have created this method and I need to check everything in one step. So I used a batch for this
I am working with SPFX webpart. When I remove inBatch everything is correctly working but I need to get result in one step. So I can chose what to display to user.
private GetPermission()
{
this.properties.addPermission = false;
this.properties.editPermission = false;
this.properties.displayPermission = false;
let batch = sp.createBatch();
sp.web.lists.getById(this.properties.listGuid).inBatch(batch).currentUserHasPermissions(PermissionKind.AddListItems).then(perms => {
this.properties.addPermission = true;
}).catch(e => {
});
sp.web.lists.getById(this.properties.listGuid).inBatch(batch).items.getById(parseInt(this.properties.itemId)).currentUserHasPermissions(PermissionKind.EditListItems).then(perms => {
this.properties.editPermission = true;
}).catch(e => {
});
sp.web.lists.getById(this.properties.listGuid).inBatch(batch).items.getById(parseInt(this.properties.itemId)).currentUserHasPermissions(PermissionKind.ViewListItems).then(perms => {
this.properties.displayPermission = true;
}).catch(e => {
});
batch.execute().then(() =>
{
alert("");
});
return null;
}
I am not sure what I am doing wrong with batch...Maybe questions if is this possible?
Before I display webpart I need to check what type of permission has. Thank you very much
Thanks, we will have a look. Your code looks fine at first glance.
I personally see some issues (not related to PnPjs directly) in the code as side effects and the impossibility to control the execution flow. It's always better returning a promise in such a case and never mutate something outside the function. But this is offtopic, just can't not to point on a potential issue.
Regards the problem the code solves, I'd rather recommend one single call to effective base permissions rather that a number of requests for one thing in a batch:
import { sp, PermissionKind } from '@pnp/sp';
const list = sp.web.lists.getByTitle('Custom');
list.select('EffectiveBasePermissions').get()
.then(({ EffectiveBasePermissions: eBasePerm }) => {
const { hasPermissions } = sp.web;
const { ViewListItems, AddListItems, EditListItems } = PermissionKind;
return {
viewListItems: hasPermissions(eBasePerm, ViewListItems),
addListItems: hasPermissions(eBasePerm, AddListItems),
editListItems: hasPermissions(eBasePerm, EditListItems)
};
})
.then(permissions => {
console.log(`Current user's permissions:`, permissions);
})
.catch(console.warn);
Anyway, there seems an issue in .inBatch(batch).currentUserHasPermissions batches and currentUserHasPermissions working together due to a nature of currentUserHasPermissions helper.
Hello Patrick and Andrew. Thank you for your messages,
Andrew I think that I understand you so in my SPFX webpart is something like this.
`
private myPromise: Promise
protected onInit(): Promise
sp.setup({
spfxContext: this.context
});
this.myPromise = this.GetPermission();
return super.onInit();
}`
then
private GetPermission() { .... return batch.execute() }
and on render this:
` public render(): void {
this.myPromise.then((results: any) => {
this.domElement.innerHTML = "
});
}`
First problem is that the batch is not working. Second problem is that if I remove batch and return for example only first method (AddPermission) nothing will render.... I don't know why this
this.myPromise.then((results: any) => {
is not working but I do not get any error....
@koltyakov your solution looks very good but the problem is that you check ViewListItems and EditListItems on whole list but I need to check on listitem....
If you look on my code you will see that AddListItem I am checking on list but View and Edit I am checking on specific Item so I think that there is no possibility to do this in one call therefore I have try to create batch.....Hmmm...Maybe I am doing something wrong... Again thank you for your help.
EffectiveBasePermissions can be retrieved for items too. You can even request a set of items with different permissions and know for sure what individual permissions are.
Andrew yes yes I know but I think that is not possible to check this in one step:
Therefore I was thinking that batch is better for that. Again thank you for your help.
Hello. I think that I have found the solution for my problem:
let batch = sp.createBatch();
sp.web.lists.getById(this.properties.listGuid).inBatch(batch).select('EffectiveBasePermissions').get()
.then(({ EffectiveBasePermissions: eBasePerm }) => {
const { hasPermissions } = sp.web;
const { ViewListItems, AddListItems, EditListItems } = PermissionKind;
return {
viewListItems: hasPermissions(eBasePerm, ViewListItems),
addListItems: hasPermissions(eBasePerm, AddListItems),
editListItems: hasPermissions(eBasePerm, EditListItems)
};
})
.then(listPermissions => {
this.properties.addPermission = listPermissions.addListItems;
})
.catch(console.warn);
if (this.properties.formMode !== "new") {
sp.web.lists.getById(this.properties.listGuid).items.getById(parseInt(this.properties.itemId)).inBatch(batch).select('EffectiveBasePermissions').get()
.then(({ EffectiveBasePermissions: eBasePerm }) => {
const { hasPermissions } = sp.web;
const { ViewListItems, AddListItems, EditListItems } = PermissionKind;
return {
viewListItems: hasPermissions(eBasePerm, ViewListItems),
addListItems: hasPermissions(eBasePerm, AddListItems),
editListItems: hasPermissions(eBasePerm, EditListItems)
};
})
.then(itemPermissions => {
this.properties.editPermission = itemPermissions.editListItems;
this.properties.displayPermission = itemPermissions.viewListItems;
})
.catch(console.warn);
}
return batch.execute();
Thank you Andrew for your nice example ....I have add this to batch and create another request but on item not on List and also added it to batch...And....Evrything start to work....
Thank you very much for your help. :)
Hi, Batch is not working on SPFx 1.7.1 -plusbeta on ensure user.
I had a working version on SPFx 1.7.0 and after I changed it to 1.7.1 -plusbeta it stopped working.
It gives no message nor returns anything
.then((persons) => {
const batch: SPBatch = sp.createBatch();
const batchPromises: Promise<any>[] = persons.map(p => {
return sp.web.inBatch(batch).ensureUser(p.User.Key);
});
batch.execute().then(
(value) => {
console.log(value);
Promise.all(batchPromises).then(values => {
values.forEach(v => {
let userPersona: any = lodash.find(persons, o => o.User.Key === v.data.LoginName);
if (userPersona && userPersona.User) {
let user: any = userPersona.User;
lodash.assign(user, v.data);
userPersona.User = user;
}
});
resolve(persons);
});
}
);
}, (error: Error): void => {
reject(this._peopleList = []);
};
));
Since -plusbeta supports normal batch I changed it back to the normal batch but it would be nice to have your batch working on both versions.
Thanks
@Dangerous-Mind - looks like you've added a new issue to the bottom of this thread. Could you please open a new issue to track your problem? If the pnpjs library works in one case and not the other that sounds like something strange in SPFx. Please be sure in your issue to provide an error or other steps to diagnose. Thanks!