How to set SharePoint item permission using SharePoint Framework

cardinaluk 41 Reputation points
2020-09-24T10:46:57.843+00:00

I'm creating an SP webpart and have a drop down that allows a user to select an item from an SP list. I want to read the current permissions and then allow the user to set the permissions on a selected item in a SharePoint list, either by a button or with a drop down (read/edit).

Here's my current two functions:

private _jeChange = (ev: React.FormEvent<HTMLDivElement>, option?: IDropdownOption, index?: number) => {
    this.setState({ 
      SelectedJE: option.text,
      JE: option.text

    });

    const selJE = this.state.SelectedJE;

    if (selJE && selJE.length > 0) {
      let _item = this.state.JobEvalItems.find((item) => {
        return item.JobRef == selJE;
      });
      this.setState({
        JEJobTitle: _item.JobTitle,
        JEEvalType: _item.EvalType,
        JEId: _item.Id,

      });

    }

  }

  private _jeSelectPermission = async () => {

    const ler = await sp.web.lists.ensure("JEItems");
    const list: IList = ler.list;

    const defs = await sp.web.roleDefinitions();
    const ras = await list.roleAssignments();
    const user = await sp.web.currentUser();
    const r = await list.roleAssignments.add(user.Id, defs[0].Id);
    console.log(user);
  }

I want to get whatever is selected in the drop down for _jeChange(), to be used with the _jeSelectPermission() function. So for example I want the user to select list item Id (2), then they click a button to give read permission to that item. I think I'm close as I can use the _jeSelectePermission() function to add the current user (me logged in!) to the root permissions with Full Control, but as mentioned I want read permissions added to the selected list item.

Microsoft 365 and Office SharePoint Server Development
0 comments No comments
{count} votes

Accepted answer
  1. Baker Kong-MSFT 3,801 Reputation points
    2020-09-25T02:51:03.057+00:00

    Hi @cardinaluk ,

    Please take a reference of below code:

      const ReadroleDefinition = await sp.web.roleDefinitions.getByName("Read").get();  
      console.log("roleDefinition : ", ReadroleDefinition);  
      
      await sp.web.lists.getByTitle('mmm').items.getById(1).breakRoleInheritance(true);  
      
      await sp.web.lists.getByTitle('mmm').items.getById(1).roleAssignments.add(user.Id, ReadroleDefinition.Id);  
    

    More reference:

    Best Regards,
    Baker Kong


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.