Edit HTML files in SharePoint Library

In Modern Office 365, one of the neat new features is the ability to edit HTML files by opening up a file from a Modern library. The HTML contents would load in the browser and were able to edit the HTML without having to download the file or edit the file using SharePoint Designer — this was a very powerful capability for many people.

Well, recently Microsoft has removed this capability (not sure why) as noted by Mark Rackley. Please vote in the UserVoice!

 What to do?

This wasn’t the first occurrence of this issue. I have found others posted about this topic on other social forums such as Reddit and Facebook. Because of this, I have decided to just build a simple little CommandSet button that will run on SharePoint libraries, which bypasses the new Microsoft changes and allows you to edit an HTML file in the browser. In order to do this, all we need to do is append &p=5 into the browser URL.

The Solution

The solution was quite simple. Build a SPFx CommandSet button, which allows the user to select an HTML file and then navigate to the view page with &p=5 appended to it.

 

COmmandSet

The code is quite simple. When a user selects a file with a file type of “html”, show the command button. When the button is clicked, grab the FileName, file path and folder path. Construct a new URL, and append &p=5.

What it looks like

Select an HTML file, the CommandSet button shows up

Library

 

Click “Edit HTML”, the page will redirect to the modern list view display form, with &p=5 to load the default edit view of the file.

TestFile

Try it out

If you’d like to try it out, please do. I have uploaded the source and the sppkg file to my GitHub Repository.  The sppkg file will be in the sppkg folder. Upload this to your app catalog. Subsequently, you can deploy the app via Site Contents or using PowerShell.  Below is a sample PowerShell command for you. (This is old code, there are other ways to provision this)

 Connect-PnPOnline -Url https://test.sharepoint.com/sites/MySite
 
 $context = Get-PnPContext
 $web = Get-PnPWeb
 $context.Load($web)
 Execute-PnPQuery
 
 $ca = $web.UserCustomActions.Add()
 $ca.RegistrationType = "List"
 $ca.RegistrationId = "101"
 $ca.ClientSideComponentId = "6599b3cc-7631-4a55-962e-43d1757977ec"
 $ca.ClientSideComponentProperties = "{""sampleTextOne"":""Edit HTML""}" 
 $ca.Location = "ClientSideExtension.ListViewCommandSet.CommandBar"
 
 $ca.Name = "EditHTML CommandButton"
 $ca.Title = "EditHTML CommandButton"
 $ca.Description = "EditHTML CommandButton"
 
 $ca.Update()
 
 $context.Load($web.UserCustomActions)
 Execute-PnPQuery

 

PnP-Starter-Kit for Standard Release Tenants

I am going to preface this blog post with — if you have an issue with the solution setup I have below, please do not add issues to the PnP-Starter-Kit issues list. This solution below, is a replication of the pnp-starter-kit and not managed by the PnP Team.

I’d also like to say, that you don’t need this post. The PnP-Starter-Kit should be deployed into First-Release tenants. I’ve had reservations on whether or not to post this.

What is it?

The PnP-Starter-Kit is an open sourced initiative that was released by the PnP-Team to show all of the capabilities that currently exist around provisioning in Office 365 using the SharePoint Framework and PnP Provisioning. For more information on the project, the GitHub repository can be found here.

One of the requirements of the PnP-Starter-Kit, is that it requires your tenant to be configured for “First Release for everyone”. This is because some of the web parts utilize the new AadHttpClient and MsGraphClient. These are currently in preview, but require permissions to be requested and approved under “API Management” in the admin center.

What’s in this my solution?

This solution is essentially an exact replica of the PnP repository, except that I have removed all of the web parts that require First Release and I have modified the deploy scripts and PnP Provisioning XML files to also support standard release tenants. What this means is that you will not have the LOB application or any of the web parts that generally show up on the personal page (except the people directory).

Why do this?

You may be wondering why I would do this. Since all of this is brand new and uses experimental features, it should only be deployed into a test/demo tenant, where turning on First Release shouldn’t be a problem. However, through some of the issues lists and comments I’ve seen on social media, it appears many people want to deploy the web parts into standard release tenants to test out the web parts, so I figured “why not?”.

You can get a demo tenant for free and set that tenant to first-release. However, if you really feel you want to deploy this into Standard Release… here you go.

How to deploy

The deployment of this is exactly the same as the full PnP Starter Kit, except you don’t have to configure any of the API management stuff.

GitHub Repository Here

  1. Make sure you’ve updated your PowerShell module to the latest version.
  2. Make sure your development environment is configured correctly here.
  3. Make sure your tenant is configured following this page for the User Profile property for the Portal Footer.
  4. Make sure your tenant has an app catalog
    1. Wait 24 hours after creating one
  5. Make sure your account is a Term Store Administrator

To deploy the starter kit, you’ll need to upload the sharepoint-portal-showcase.sppkg from the /package folder to your app catalog.  This will provision the web parts to all of your site collections for use. If you’d like to also include the 3 demo sites, and pre-built pages included in the full starter kit, then open up Powershell and navigate to the provisioning folder. Once inside the folder, execute the following command to start the provisioning process.

.\deploy.ps1 -TenantUrl https://YourTenant.sharepoint.com -SitePrefix Demo

 

Questions?

If you have any questions, just ask me. Please note, that I will not really be maintaining this repository, so it will not be receiving any of the new bug fixes and updates from the PnP Team.

Please do not add issues about this package to the PnP-Starter-Kit GitHub repository issues list.

PnP-SP-Starter-Kit Hints and Tips

If you are like me, you followed all of the updates at SharePoint Conference North America 2018 and you heard about an awesome new starter pack created by the PnP Team, and you couldn’t wait to install it and deploy it into your environment.  You can find the Starter Kit here.

Please note, this blog post isn’t official documentation,  nor am I a part of the PnP team… but these are just some issues I have seen as I’ve started exploring.

Before going any further

Before you follow the rest of this blog, please note that the PnP team has updated some of the deployment documentation. I highly recommend running through this documentation to make sure your environment is prepared for the PnP Starter Kit.

Documentation found here and tenant settings here.

Foreword

What is important to know is that this solution is an all encompassing solution. It provides you with web parts, customizers, site designs, MSGraphClient configuration, a hub site, lists, content types, a sample LOB application and more. This might be the most exhaustive example of modern SharePoint provisioning I have seen. There really is an impressive amount of work that was put into this! Kudos to the team and community for putting this together.

A few Hints

Downloading the Project

Downloading the project is quite simple, assuming you’ve developed SPFx before. If you haven’t, you may want to spend some time setting up your development environment.

To download the project, navigate to the starter kit from the link above. You can clone this repository to your local machine using the Github UI.

PnpStarterKit

Or, you can use the following git commands

git clone https://github.com/SharePoint/sp-starter-kit/

 

If you’d like to know how the starter kit gets provisioned, you’d want to look into the provisioning folder. You’ll find a collection of PowerShell scripts which will help you deploy the starter-kit. Before you go any further, please read the documentation on GitHub and make sure you have configured your environment and followed the deployment procedures.

Update your PnPPowerShell module

You’ll want to update your PnPPowerShell module to the latest version. In order to do this, open up Powershell and execute the following command.

Update-Module SharePointPnPPowerShell*

Deploy the starter kit

  1. Make sure you’ve updated your PowerShell module above.
  2. Make sure your development environment is configured correctly here.
  3. Make sure your tenant is configured following this page.
  4. Make sure your tenant is “targeted release for everyone”.
    1. Wait 24 hours after enabling this
  5. Make sure your tenant has an app catalog
    1. Wait 24 hours after creating one
  6. Make sure you are a term store administrator.

To deploy the starter kit, you’ll need to upload the sharepoint-portal-showcase.sppkg from the /package folder to your app catalog. To learn how to upload files to your app catalog, follow this link.

Then open up Powershell and navigate to your provisioning folder. Once inside the folder, execute the following command to start the provisioning process.

.\deploy.ps1 -TenantUrl https://YourTenant.sharepoint.com -SitePrefix Demo

The SitePrefix parameter is for the prefix of the newly created site collections.

When you execute this command, you’ll notice a ton of actions happen. The solution is going to create a couple demo sites for you. “Demo_Portal”, “Demo_hr”, “Demo_Marketing”. The script is going to create the Demo_Portal site as a hub site, and connect the other two sites to it.

The script will install the app package you previously uploaded in the app catalog into the sites that are being created. It will then create two site designs for Team sites and for a Communication site, and then create new navigation elements for your hub sites.

The deployment script will also create a handful of pages for your Portal site and automatically provision the web parts to the page. This is just the start… the deployment script actually provisions and exhaustive set of functionality, however this post isn’t about all of the functionality. 🙂 Have a look yourself and see what it provisions!

Deployment Failed

When upload the .sppkg file to the AppCatalog, you may receive an error stating “Deployment failed”. Just delete the .sppkg file and re-upload the package to the app catalog!

Error in application

When uploading the .sppkg file into the App Catalog, you may receive this error. If you had just recently created your App Catalog, you may need to wait up to 24 hours for the change to fully propagate successfully. So wait 24 hours and try to upload the .sppkg file again!

Apply-PnPProvisioningTemplate Error #1

Apply-PnPProvisioningTemplate : Object reference not set to an instance of an object. At C:\Users\beaucameron\spfx\sp-starter-kit\provisioning\deploy.ps1:96 char:5

This will likely be the first error you see when trying to deploy the solution. Make sure the .sppkg file is already in the term store. If you haven’t manually deployed it, please do so now by following in the instructions at the top of this blog. If you haven’t created an App Catalog in your tenant, you will also need to do this.

If you just recently created the App Catalog, you may have to wait up to 24 hours for it to be completely functional.

App Package Not Valid

Apparently some people have been unable to deploy the solution to the app catalog and receive an error that the app package is not valid. Garry Trinder mentioned this issue, and so far others have confirmed that it is because API Management configuration is missing from your admin center. If this error happens for you and you are also missing API Management from the new Admin Center could you let me know (confirmation)?

In order to add API Management to your admin center, you must enable “Targeted Release” for your tenant, instead of specific users. It may not be immediate and may take a few hours for the changes to propagate to your tenant. When doing this, you should do it on a non-production tenant.

New-PnPSite Error

If you are receiving  the following error:

New-PnPSite : {"d":{"Create":{"__metadata":{"type":"SP.Publishing.CommunicationSiteCreationResponse"},"SiteStatus":3,"SiteUrl":""}}}

Could you confirm if you have had previously deployed the starter-kit and then subsequently deleted the site collections and re-deployed? If so, could you let me know. It appears that there may be an issue with the New-PnPSite command when executing against a deleted communication site.

I have repro’d this error by running the deploy.ps1 using no -SitePrefix parameter. I then deleted the sites that were created, and re-ran the deploy.ps1. Even the though the sites do not exist, the New-PnPSite command cannot create a new site.

Try re-executing the command but modifying it by adding a new SitePrefix.
 .\deploy.ps1 -TenantUrl https://YourTenant.sharepoint.com -SitePrefix NewSitePrefix

If you’d like to re-use an existing SitePrefix, you can a couple PowerShell commands to clean up the deleted sites. Per @Andreas (thanks!)

//permanently delete all deleted sites
Connect-SPOService -Url “SharePoint Admin Tenant Url”
Get-SPODeletedSite | foreach { Remove-SPODeletedSite -Identity $_.Url }

//Remove hubsite associations
Get-SPOHubSite | ? {$_.Title -eq “PnP SP Starter Kit”} | Unregister-SPOHubSite

A service principal with the name SPFx-LOB-Function could not be found / a service principal with the name SPFx-LOB-WebAPI could not be found.

Inside API management in the admin center, you may find that when you are trying to approve the API permissions you receive this error on the LOB-Service application. I believe the reason is because the LOB service API doesn’t get provisioned with the deploy.ps1 script. So these service principals aren’t a part of your tenant’s AAD.

If you look inside your repo/folder structure, you’ll see a folder named sample-lob-service, which holds a Visual Studio solution for a sample WebAPI project. This solution needs to be deployed to your Azure environment, and two service principals have to be created with two permissions scopes:

  • a default “user_impersonation” scope
  • a custom “Customers.Read” scope

The service principals needs to be created and given permission to your sample-lob-service API.

Long story short – You need to register a resource in Azure for this WebAPI, subsequently deploy it and configure the service principals. There isn’t any documentation yet about setting this up. As this is a new starter-kit… documentation is still being created by the PnP Team. I suspect more information on how to setup/configure/deploy this will be coming in the future.

My navigation isn’t showing up

One of things you may notice after deploying the starter kit, is that the hub navigation doesn’t work. In general, hub navigation is a bit finicky but a simple way to make the navigation show up is by editing the navigation and cancelling out… or just wait for it to fix itself (caching).

navigation.png

Personal Web Parts aren’t loading

If you navigate to the personal page in the navigation, it brings you to a really awesome page with a bunch of SPFx web parts on it that utilize Microsoft Graph. These web parts include:

  • Upcoming Meetings,
  • My Tasks
  • Personal Contacts
  • Personal Email
  • Recent Documents
  • Recent Sites

You may find the personal web parts aren’t loading correctly, here are some possible reasons.

 

Access is denied. Check credentials and try again.

accessdenied.png

You may find that your personal web parts, aren’t loading at all and showing you that your credentials are invalid. This is because (at least in my environment), you need to explicitly give permission to the SPFx web parts to have access to the Microsoft Graph. If you navigate to the new admin center, https://yoursite-admin.sharepoint.com/_layouts/15/online/AdminHome.aspx on the left hand side, select API Management under Advanced. You’ll see a list of pending approvals for the new applications. Approve this request and your web parts will start working!

APIManagement.png

 

User login is required

If you’ve configured the permissions in API management, you may find that the web parts still aren’t loading.

User login is required

It appears there is a bug (race condition?) that has effected acquiring a token for the AadHttpClient/MSGraphClient. Creating a new page and/or workbench, may allow you to see the web part temporarily. However, the bug/issue still exists and is linked below.

Emails.png

I found there is a support ticket on this issue which can be found on GitHub, issue 1810.

I have no Alerts

One of the cool application customizers deployed with this starter pack is a top header that shows configurable alerts. This is one of the only lists that doesn’t pre-populate with data through the deployment process. If you’d like to test it out, navigate to site contents and add some alerts to the Alerts list!

Alerts

Once you have, you should be able to refresh your page see the alerts show up on your modern pages!

AlertDialog

 

More…

If you’d like to know more of the features that are included in the starter kit, Robert Shouten has made a great post outlining the web parts and functionality here.

Disclaimer

Once again, I’d like to re-iterate that this isn’t any official documentation and that more documentation is linked at the top of this post.

Thanks to the PnP team and Vesa for all their continued effort. Sharing is caring.

Security Trimmed Hub Site Navigation Updates!

I had recently posted a blog  about a SharePoint Framework (SPFx) application customizer I built for use with hub sites in Office 365. The problem I currently have with Hub navigation is that all of the links are static links. This means, that whoever creates the common navigation must be cognizant of what links end users have access to. With that in mind, I built a new navigation the replaces the out-of-the-box hub site navigation elements with a security trimmed listing of sites within a Hub.

For brevity, this isn’t an all inclusive tutorial. It’s just discussing the high level changes to the original implementation of the application customizer.

How it works

The new navigation uses a combination of the SharePoint Search API and the new Hub sites API. When a user lands on a site collection that belongs with in a hub, I grab the hub site information to find all of the sites that exist within the hub using the Search API. The benefit of this, is that I can security trim a listing of sites with in the navigation. That means I can have sites in a hub that a user may not have access to, yet include them as elements in the global hub site navigation.

HubNavSite

What’s wrong with V1?

While this approach is great and I have had quite a bit of success building out security trimmed navigation, the default implementation lacks the ability to use the existing hub site navigation elements. This is why I have released v2 which is available on GitHub for download.

The new application customizer still implements the “Sites” listing node in the navigation.
NewNav

Adding existing Hub navigation

Version 2 is almost identical to version 1 with the exception that I am now pulling in the static elements from the default hub navigation. To do this, I’ve made some modifications to the base ReactHubsiteNavbarApplicationCustomizer.ts file, to set the Navigation property equal to the navigation array that returns from the _api/web/hubsitedata endpoint.

this._currentHubSiteData = await  searchService.getHubSiteData().then((hubData: IAssociatedSite) => {
if(hubData){
return searchService.getHubID(hubData).then((hub: IHubSiteData) => {
//add existing hub navigation
hub.Navigation = hubData.navigation;
return hub;
});
}
else{
return null;
}
});

Now that I have the elements as part of the _currentHubSiteData object, I just have to modify my HubNavBar.tsx component to accommodate loading the existing navigation nodes. One thing that isn’t working currently with the commandbar, is the ability to have a navigation element with children, in which the parent link is clickable.

SiteTrimmedMenuCode

You may notice the methods are almost identical. Rather than refactoring… I’ve separated them out because I have some future changes I’d like to make specifically to the rendering of the trimmed sites listing.

Ability to edit navigation

Now that we are showing the existing navigation elements and hiding the existing Hub navigation html…I needed a way to allow users to add and edit the original Hub navigation. There is an endpoint at _api/Navigation/SaveMenuState for saving changes to the menu, however it is undocumented by Microsoft so I will avoid using it for now. Instead, I am going to re-use the out-of-the-box Hub site navigation control to manage adding/editing elements.

To do this, I am checking if the user has ManageWeb permission, and then I will show an edit button which will show/hide the original Hub navigation.
//if user has manage web permissions, show edit button
const hasPermission:boolean = this.props.context.pageContext.web.permissions.hasPermission(SPPermission.manageWeb);


if(hasPermission){
commandBarItems.push({
key:"editButton",
name:"Edit",
itemType:ContextualMenuItemType.Header,
onClick:this._editOnClick,
className:"editButton",
href:"#"
});
}

Caveat: The out of the box permission for managing Hub navigation is the new “Edit” permission. Which means that users in the Member group have access to change the navigation. I think this is a bit too much permission, so I am restricting the permissions to “ManageWeb”.

The Result

hubnavnew

The end result is a combination of security trimmed sites listing within the hub and the existing hub site navigation. User’s with ManageWeb permissions on the Hub site will be able to see the edit button to modify the existing navigation elements.

Notes

Because of the caching in the customizer, it may take 15 minutes for changes to appear!

GitHub – https://github.com/bcameron1231/SharePoint-Framework/releases/tag/v2.0

Code is in pre-release and subject to change (refactoring).