Professional Documents
Culture Documents
10 Lab Share Point Framework and React
10 Lab Share Point Framework and React
10 Lab Share Point Framework and React
c:\Labs>yo @microsoft/sharepoint
Now let’s step through the wizard. Add the following details:
1. Solution name: SitePermissionsSolution
2. Files location: Pick “Create a subfolder with solution name” (use the arrow keys)
3. Baseline packages: SharePoint Online only
4. Feature deployment: y
5. Require permissions to access web APIs: N
6. Client-side component type: WebPart
7. Webpart name: ListSitePermissions
8. Webpart description: Displays the current site permissions
9. Framework: Pick “React”
Check that there are no errors when creating the web part. Open up Visual Studio Code and run the project by
running the following commands within the newly created folder:
code .
gulp serve
Copyright 2000-2010 by U2U nv/sa, Belgium – For use in U2U courses only. Visit www.u2u.net
2 Error! No text of specified style in document.
LISTSITEPERMISSIONSWEBPART.TS
import {
BaseClientSideWebPart,
IPropertyPaneConfiguration
PropertyPaneTextField
} from '@microsoft/sp-webpart-base';
import * as strings from 'listSitePermissionsStrings';
LISTSITEPERMISSIONSWEBPART.TS
Now, let’s remove the default description property from the web part interface
LISTSITEPERMISSIONSWEBPART.TS
As you can see, this web part loads a single React component ListSitePermissions and injects it into the DOM. In that
main component however we do want to use the web part context. We need it to get the current site url and the http
client. We will modify the component properties, so we can pass it in, and then set it from the web part code.
COMPONENTS/ILISTSITEPERMISSIONSPROPS.TS
import {
IWebPartContext
} from '@microsoft/sp-webpart-base';
import {
IListSitePermissionsWebPartProps
} from '../ListSitePermissionsWebPart';
In the web part code, you should now see that our element cannot be initialised properly. We need to modify the
passed in properties. Remove description, add context.
LISTSITEPERMISSIONSWEBPART.TS
Modify the render method of the main component so that we can build the project.
Copyright 2000-2010 by U2U nv/sa, Belgium – For use in U2U courses only. Visit www.u2u.net
Error! No text of specified style in document. 3
LISTSITEPERMISSIONS.TSX
We will not change this web part any more during this lab. Build your project and check for errors.
Copyright 2000-2010 by U2U nv/sa, Belgium – For use in U2U courses only. Visit www.u2u.net
4 Error! No text of specified style in document.
Let’s create interfaces to store this data. It’s important to have interface members with exactly the same name and
casing as in the http response, this will make things a lot easier in our code. Instead of mapping everything manually
we will just send the original json response to our components to render our data.
First, create a new folder under src called interfaces.
Create a new file in this folder called PermissionInterfaces.ts and add the following code:
/SRC/INTERFACES/PERMISSIONINTERFACES.TS
Copyright 2000-2010 by U2U nv/sa, Belgium – For use in U2U courses only. Visit www.u2u.net
Error! No text of specified style in document. 5
Now that we have our data structures, we can use them to create the state for our main component. The first thing
we will need again is a state interface. That interface will contain our permission set.
Make sure you use the interface when defining the component.
Add the following:
LISTSITEPERMISSIONS.TSX
Let’s add a constructor to set the initial state. Let’s also implement the componentDidMount method to load the site
permission set when our component is loaded. The IWebPartContext interface needs to be imported.
LISTSITEPERMISSIONS.TSX
Run or build the project and check that everything builds fine.
Hope everything works just fine? Impressive, isn’t it .
Copyright 2000-2010 by U2U nv/sa, Belgium – For use in U2U courses only. Visit www.u2u.net
6 Error! No text of specified style in document.
PERMISSIONSHTTPSERVICE.TS
import {
IWebPartContext
} from '@microsoft/sp-webpart-base';
constructor(context:IWebPartContext){
this._context = context;
}
}
Let’s first implement a method that will give us the role assignment collection. This method will return a collection of
ISPRoleAssignment, so add the import for the interface as well.
PERMISSIONSHTTPSERVICE.TS
import {
SPHttpClient, SPHttpClientResponse
} from '@microsoft/sp-http';
…
Copyright 2000-2010 by U2U nv/sa, Belgium – For use in U2U courses only. Visit www.u2u.net
Error! No text of specified style in document. 7
…
private _roleAssignmentUrl =
"/_api/web/roleassignments?$expand=Member,Member/Users,RoleDefinitionBindings&$sele
ct=PrincipalId,Member/Title,Member/ID,Member/PrincipalType,Member/Users/Title,RoleD
efinitionBindings/Id,RoleDefinitionBindings/Name";
}
}
Notice in this case we return the json as is, we’re not mapping anything manually. Be aware this will include the odata
metadata and send it to our components. If you want to avoid this overhead, map everything manually!
Great! Now let’s get back to our component!
LISTSITEPERMISSIONS.TSX
constructor(props:{context:IWebPartContext}){
super(props);
//set initial state
this.state = {permissions: [] };
//set service
this._permissionsHttpService = new PermissionsHttpService(this.props.context);
Good good. Let’s see if it works! Open your SharePoint Online Workbench and check the console output!
This will not work in your local workbench!
Copyright 2000-2010 by U2U nv/sa, Belgium – For use in U2U courses only. Visit www.u2u.net
8 Error! No text of specified style in document.
We could show all that glorious data in our main component, but this lab is about react components right . Let’s
create a component that will display the permission set.
Add a new folder Permissions inside the components folder. In that folder create a new component file, named
PermissionList.tsx.
Let’s add the react component code and the property interface we will need! In this component we will also use css
and react css tools, so let’s import them already!
PERMISSIONLIST.TSX
PERMISSIONLIST.TSX
Copyright 2000-2010 by U2U nv/sa, Belgium – For use in U2U courses only. Visit www.u2u.net
Error! No text of specified style in document. 9
return (
<div>{roleAss.Member.Title}</div>
);
}
);
return (
<div className="ms-Grid ms-fontColor-black">
<div className="ms-Grid-row ms-font-xl ms-fontColor-themePrimary">Who
has access to this site?</div>
{roles}
</div>
);
}
That should be enough for now. Yes, we will make it look pretty in a moment!
LISTSITEPERMISSIONS.TSX
LISTSITEPERMISSIONS.MODULE.SCSS
@import '~@microsoft/sp-office-ui-fabric-core/dist/sass/SPFabricCore.scss';
.listSitePermissions {
.container {
margin: 0px auto;
}
.row {
padding:3px;
cursor: pointer;
}
Copyright 2000-2010 by U2U nv/sa, Belgium – For use in U2U courses only. Visit www.u2u.net
10 Error! No text of specified style in document.
.element{
margin-right: 2px;
padding:8px 10px 2px;
color:white;
min-height:35px;
vertical-align: middle;
}
.inline{
margin-right: 5px;
padding: 2px 5px;
border:1px dashed white;
}
.icon{
font-size: 20px;
margin-right: 10px;
}
}
Modify the render method to make each role assignment look good. We will add an icon (office-ui-fabric) to show if it
is a user or a group. Also, we’ll get the list of role bindings and render them in html.
PERMISSIONLIST.TSX
Test!
Aaaaah better…
Copyright 2000-2010 by U2U nv/sa, Belgium – For use in U2U courses only. Visit www.u2u.net
Error! No text of specified style in document. 11
Copyright 2000-2010 by U2U nv/sa, Belgium – For use in U2U courses only. Visit www.u2u.net
12 Error! No text of specified style in document.
PERMISSIONLIST.TSX
Let’s trigger the property from a function. Add it to the component class
PERMISSIONLIST.TSX
Final step: call the function when somebody clicks a data row!
PERMISSIONLIST.TSX
LISTSITEPERMISSIONS.TSX
Copyright 2000-2010 by U2U nv/sa, Belgium – For use in U2U courses only. Visit www.u2u.net
Error! No text of specified style in document. 13
Now, let’s implement the handleRoleAssignmentSelect method. We will store the selected object in the state, so let’s
extend that first.
LISTSITEPERMISSIONS.TSX
GROUPDETAILS.TSX
Copyright 2000-2010 by U2U nv/sa, Belgium – For use in U2U courses only. Visit www.u2u.net
14 Error! No text of specified style in document.
GROUPDETAILS.TSX
LISTSITEPERMISSIONS.TSX
Test!
Copyright 2000-2010 by U2U nv/sa, Belgium – For use in U2U courses only. Visit www.u2u.net
Error! No text of specified style in document. 15
Copyright 2000-2010 by U2U nv/sa, Belgium – For use in U2U courses only. Visit www.u2u.net