0

I am getting an '“Input string was not in a correct format.” error after submitting the following POST call: any idea what I might be doing wrong here?

const addUserURL: string = newWeb + `/_api/web/sitegroups('${groupName}')/users`;     
const userOpts: ISPHttpClientOptions = {
      body: `{
        "@odata.type": "SP.User",
        "loginName": "${userKey}" // a.k.a.: "i:0#.f|membership|[email protected]"
    }`
    }; 

     const addUserToGroupResponse: SPHttpClientResponse = this.props.context.spHttpClient.post(
      addUserURL,
      SPHttpClient.configurations.v1,
      userOpts);

     console.log("User has been successfully added to the security group.");
4
  • 1
    Modify it to "@odata.type": "#SP.User", and check Apr 25, 2018 at 16:51
  • have tested that as well, doesn't make a difference : / Apr 25, 2018 at 16:52
  • Also, change to "loginName": "${encodeURIComponent(userKey)}" and check ? Ensure that the odata.type value is as in the previous comment Apr 25, 2018 at 16:56
  • edited both fields as per your recommendation, still the same error : / Apr 25, 2018 at 17:01

1 Answer 1

0

You are missing the GetByName after the SiteGroups endpoint.

Modify your code as :

const addUserURL: string = newWeb + `/_api/web/sitegroups/GetByName('${groupName}')/users`;
const userOpts: ISPHttpClientOptions = {
  body: `{
    "@odata.type": "#SP.User",
    "LoginName": "${userKey}"
}`
};
4
  • @lachezarStefanov - any luck with this ? Have tested the above code in my tenant and works fine. Apr 26, 2018 at 6:26
  • Hello Gautam, Thank you, adding the GetByName is a game changer here indeed. Pity too many code samples on the internet have omitted it : / I currently struggle with adding the user to the group; I get an error "The user does not exist or is not unique." (-2130575276, Microsoft.SharePoint.SPException) Have experimented with different user properties (ex.: the DisplayText and the Description), so far no luck. Tried also adding the encodeURIComponent function, no change : / Apr 26, 2018 at 6:46
  • 1
    can you try with another user for testing purposes ? I faced this issue for couple of times and after a few mins it vanished :) Apr 26, 2018 at 6:57
  • Hello Gautam, That did the trick indeed : ) entered another user and it got added successfully. Then experimented with the user, originally displaying the error and it worked with him as well. (I was just about to use 'good old' JSOM as a workaround... thanks for keeping my code clean from deprecated mess : ) ) Apr 26, 2018 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.