Pnpjs: Add external User via email to SharePoint group

Created on 18 Oct 2018  路  6Comments  路  Source: pnp/pnpjs

I want to add an external User to a SharePoint Group (like owners, members, visitors). The Problem is that the User I want to add is not really a User at the time I add him. I only got the email adress. So i can not use sp.web.sitegroups.adduser since this method needs a Loginname.
Is this scenario even possible via pnpjs?

Category

  • [ ] Enhancement
  • [ ] Bug
  • [x] Question
  • [ ] Documentation gap/issue

Version

Latest

Please specify what version of the library you are using: [ Latest ]

Please specify what version(s) of SharePoint you are targeting: [ online ]

code details needed question

Most helpful comment

Hi there,

This is supposed to be possible using REST API.

You can try:

import { sp, SharingRole } from "@pnp/sp";

sp.web.shareWith('[email protected]', SharingRole.View, {
    body: 'Message body'
}).then(console.log);

Depending on sharing role it can be default Visitors, Members groups.

Also, there is a chance to add a user to a specific custom group consuming /_api/SP.Web.ShareObject endpoint. It's not automated within PnPjs, but here is a custom implementation.

import { SPHttpClient } from '@pnp/sp';

const siteUrl = 'https://spnode.sharepoint.com/sites/hug/projects';

const emailBody = 'Welcome to site';
const externalUserEmail = '[email protected]';

const client = new SPHttpClient();
client.post(`${siteUrl}/_api/SP.Web.ShareObject`, {
    body: JSON.stringify({
        emailBody,
        includeAnonymousLinkInEmail: false,
        peoplePickerInput: JSON.stringify([{
            Key: externalUserEmail,
            DisplayText: externalUserEmail,
            IsResolved: true,
            Description: externalUserEmail,
            EntityType: '',
            EntityData: {
                SPUserID: externalUserEmail,
                Email: externalUserEmail,
                IsBlocked: 'False',
                PrincipalType: 'UNVALIDATED_EMAIL_ADDRESS',
                AccountName: externalUserEmail,
                SIPAddress: externalUserEmail,
                IsBlockedOnODB: 'False'
            },
            MultipleMatches: [],
            ProviderName: '',
            ProviderDisplayName: ''
        }]),
        roleValue: 'group:6', // where `6` is a GroupId
        sendEmail: true,
        url: siteUrl,
        useSimplifiedRoles: true
    })
})
    .then(r => r.json())
    .then(console.log);

This is a result of reverse engineering of Share site functionality in SPO.

All 6 comments

Hi there,

This is supposed to be possible using REST API.

You can try:

import { sp, SharingRole } from "@pnp/sp";

sp.web.shareWith('[email protected]', SharingRole.View, {
    body: 'Message body'
}).then(console.log);

Depending on sharing role it can be default Visitors, Members groups.

Also, there is a chance to add a user to a specific custom group consuming /_api/SP.Web.ShareObject endpoint. It's not automated within PnPjs, but here is a custom implementation.

import { SPHttpClient } from '@pnp/sp';

const siteUrl = 'https://spnode.sharepoint.com/sites/hug/projects';

const emailBody = 'Welcome to site';
const externalUserEmail = '[email protected]';

const client = new SPHttpClient();
client.post(`${siteUrl}/_api/SP.Web.ShareObject`, {
    body: JSON.stringify({
        emailBody,
        includeAnonymousLinkInEmail: false,
        peoplePickerInput: JSON.stringify([{
            Key: externalUserEmail,
            DisplayText: externalUserEmail,
            IsResolved: true,
            Description: externalUserEmail,
            EntityType: '',
            EntityData: {
                SPUserID: externalUserEmail,
                Email: externalUserEmail,
                IsBlocked: 'False',
                PrincipalType: 'UNVALIDATED_EMAIL_ADDRESS',
                AccountName: externalUserEmail,
                SIPAddress: externalUserEmail,
                IsBlockedOnODB: 'False'
            },
            MultipleMatches: [],
            ProviderName: '',
            ProviderDisplayName: ''
        }]),
        roleValue: 'group:6', // where `6` is a GroupId
        sendEmail: true,
        url: siteUrl,
        useSimplifiedRoles: true
    })
})
    .then(r => r.json())
    .then(console.log);

This is a result of reverse engineering of Share site functionality in SPO.

Your second example looks like what I need. I will test it tomorrow and give feedback. Thanks :)

Going to close this as answered, please _reopen_ should you need to continue the conversation. Thanks!

Quick question regarding this. I'm attempting to do this in a Flow. I've copied your code verbatim except for hardcoded values for email address. email message, and siteurl. The request is good, and I get back a 200, but in the body I get:

I've got external sharing on and I can share the site with the user via the gui just fine...

{
"odata.metadata": "https://mytenant.sharepoint.com/sites/subsite/_api/$metadata#SP.ApiData.SharingResults/@Element",
"odata.type": "SP.SharingResult",
"odata.id": "https://mytenant.sharepoint.com/sites/subsite/_api/SP.Web.ShareObject",
"odata.editLink": "SP.Web.ShareObject",
"ErrorMessage": "Couldn't resolve the users.",
"IconUrl": null,
"InvitedUsers": null,
"Name": null,
"PermissionsPageRelativeUrl": null,
"StatusCode": -1,
"UniquelyPermissionedUsers": null,
"Url": null,
"UsersAddedToGroup": null
}

@koltyakov

Hi Andrew,

Apparently the "peoplePickerInput" parameter type has been changed to string and now I don't have any clue on how make the call. Here's the body of my call :

{
   "url":"https://example.sharepoint.com/sites/mySite/Shared Documents/example.docx",
   "emailBody":"Example body",
   "emailSubject":"Example subject",
   "peoplePickerInput":"[email protected]",
   "includeAnonymousLinkInEmail":false,
   "roleValue":"0",
   "sendEmail":true,
   "groupId":"0",
   "useSimplifiedRoles":false,
   "propagateAcl":false
}

I receive the following message : "Couldn't resolve the users."

@CaffeLatte0 please create a new issue, the closed are mostly not revisited and almost not visible for the team.

Was this page helpful?
0 / 5 - 0 ratings