What is SPFx Solutions and its Creation?

SharePoint Creation Nd Solutions
27Sep, 2019

Another name for SharePoint Framework is SPFx. It is a supreme platform especially for developers as it enables them to create client-side applications. SPFx has integrated an alluring feature commencing from release version 1.8. This feature enables SPFx webparts to be employed in MS Teams as a tab. This way, the same webpart can be employed in the SharePoint site along with MS teams. The end result is precise SharePoint app development.

In the forthcoming sections of this blog, we have provided certain steps to create this kind of SPFx web part. Through this write-up, we want to showcase the best way to deploy a prevailing webpart and employ the updated web part in MS Teams. On top of this, we will also let you know the best way to utilize the site collection’s libraries and list by executing the CRUD operation.

This piece of content has specifically been divided into two parts:

  1. Executing CRUD operation to list
  2. Redeploy webpart in MS teams

Let’s look at the two in detail.

Executing CRUD Operation to List

Executing CRUD operation to the list will interlink your webpart to the list of any site collection of your tenant. This webpart will be utilized in the MS Teams tab which will result in an established connection between MS Teams and SharePoint site collection.

Let’s now divert our attention to the steps to execute CRUD operation to SharePoint list:
You need to employ PnP js to execute CRUD operation. The very first step is to install PnP js libraries. Insert npm install sp-pnp-js -save in command prompt. Ensure that you are present inside the project directory.

CRUD operation

Import the added library in the .ts file of your webpart.

Import the added library

This will enable us to execute CRUD employing PnP js. 

Note: It is not mandatory for you to employ PnP js, in case it is not consumed, then there will be no need to install its library.

Next, add the text boxes and buttons for executing the operations.

<br></br>
              <div>
                <input id="LanguageName" placeholder="Enter any language"></input>
                <input id="LanguageId" placeholder="Enter any language id"></input>
              </div>
              <br></br>
              <div>
                <button class="create-Button"><span>Create Item</span></button>
                <button class="retrieve-Button"><span>Retrieve Item</span></button>
                <button class="update-Button"><span>Update Item</span></button>
                <button class="delete-Button"><span>Delete Item</span></button>
              </div>
              <div>
                <table id="tblLanguages" class="table-responsive">
                <thead>
                  <td>	
                    <tr>ID</tr>
                    <tr>Name</tr>
                  </td>
                </thead>
                <tbody id="bodyTableLang">
                </tbody>
                </table>
              </div>

You will see two text boxes, four buttons, and a table added. The above code is integrated inside the render() method.

Once the rendering of the HTML part is over, the two methods are called as:

this.setButtonEventHandlers();
this.RenderLanguagesEntries();

this.setButtonEventHandlers() method will secure the methods to the buttons.

private setButtonEventHandlers(): void
  {
    const webpart: TeamsTabWebPart = this;
 
    this.domElement.querySelector('button.create-Button').addEventListener('click', () => {webpart.createItem();});
    this.domElement.querySelector('button.retrieve-Button').addEventListener('click', () => {webpart.retrieveItem();});
    this.domElement.querySelector('button.update-Button').addEventListener('click', () => {webpart.updateItem();});
    this.domElement.querySelector('button.delete-Button').addEventListener('click', () => {webpart.deleteItem();});
  }

On the other hand, this.RenderLanguagesEntries() will secure the default table by fetching the data from the Language list.

private RenderLanguagesEntries(): void
{
  this.table =  <HTMLTableElement> document.getElementById("bodyTableLang");
  this.table.innerHTML = "";
  pnp.sp.web.lists.getByTitle('Language').items.get().then((items) => {    
    for(var i=0; i< items.length; i++)
    {
      var id = items[i].ID;
      var langName = items[i].Title;
      var row = this.table.insertRow(i);
      var cell1 = row.insertCell(0);
      cell1.innerHTML = id;
      var cell2 = row.insertCell(1);
      cell2.innerHTML = langName;
    }
  }).catch((error) => {
    alert("Something went wrong " + error);
  });
}

Here, “table” variable is already defined on a global basis inside the main class.

Table variable

The output of the webpart will be displayed in the form mentioned below.

Output of the webpart

You can even add a new item in the list by inserting the language name inside the textbox. For example, let’s assume that you wish to add “French” language.

New Item

Once you have inserted the item, click on the “Create item” button. This will prompt the user with an alert of successful integration.

This way, the language will be integrated inside the list and the below-mentioned table will be updated. 

Note: We have already provided the code to add an item below.

private createItem(): void
{
  let langName: string = document.getElementById('LanguageName')["value"];
  let web = new Web(this.context.pageContext.web.absoluteUrl);
  pnp.sp.web.lists.getByTitle('Language').items.add({
    Title: langName
  }).then(() =>{
      document.getElementById('LanguageName')["value"] = "";
      document.getElementById('LanguageId')["value"] = "";
      alert("Item added successfully");
      this.RenderLanguagesEntries();
  }).catch((error) =>{
    alert("Something went wrong" + error);
    document.getElementById('LanguageName')["value"] = "";
    document.getElementById('LanguageId')["value"] = "";
  });
}

Note: In case of any error while adding the language, the user will get an alert with the error message.

If you wish to retrieve the language, insert the ID for which you wish to get the details. Here, we have added id6 in the textbox. By clicking on the “Retrieve Item” button, the user will get an alert with the language name.

retrive item 2

We have even provided the code to retrieve the item given below.

private retrieveItem(): void{
  let langID = document.getElementById('LanguageId')["value"];
    let web = new Web(this.context.pageContext.web.absoluteUrl);
    pnp.sp.web.lists.getByTitle('Language').items.getById(langID).get().then((result) =>{
      alert("Retrieved Item: "+ result.Title);
      document.getElementById('LanguageName')["value"] = "";
      document.getElementById('LanguageId')["value"] = "";
      this.RenderLanguagesEntries();
    }).catch((error) => {
      alert("Something went wrong" + error);
      document.getElementById('LanguageName')["value"] = "";
      document.getElementById('LanguageId')["value"] = "";
    });
}

If you wish to update the language, enter the new language name and ID of the language for which the language needs to be updated. 

For example, let’s consider you wish to update from the “Bengali” language to the “Croatian” language.

Update the language

Once that is done, the user will receive an alert of the successful update of language. Once he/she clicks on “OK” the change will reflect on the table. 

Change in the table

We have even provided the code to update the item given below:

private updateItem(): void
{
  let langID = document.getElementById('LanguageId')["value"];
  let langName: string = document.getElementById('LanguageName')["value"];
  let web = new Web(this.context.pageContext.web.absoluteUrl);
 
  pnp.sp.web.lists.getByTitle('Language').items.getById(langID).update({
    Title: langName
  }).then(() =>{
    alert("Item updated successfully");
    document.getElementById('LanguageName')["value"] = "";
    document.getElementById('LanguageId')["value"] = "";
    this.RenderLanguagesEntries();
  }).catch((error) => {
    alert("Something went wrong" + error);
    document. getElementById('LanguageName')["value"] = "";
    document.getElementById('LanguageId')["value"] = "";
  });
}

If you wish to delete the item, the ID for the language to be deleted for should be entered. For example, you wish to delete the newly added language “French”.

delete the item

Once you enter the ID of the language and click the “Delete Item” button, it will ask the user for confirmation.

If you click on “OK” it will delete the language and the table will be updated.

Delete the language

We have even provided the code to delete the item given below.

private deleteItem(): void
{
  let boolDelete: boolean = confirm("Are you sure you want to delete item?");
    if(boolDelete)
    {
      let langID = document.getElementById('LanguageId')["value"];
      let web = new Web(this.context.pageContext.web.absoluteUrl);
 
      pnp.sp.web.lists.getByTitle('Language').items.getById(langID).delete().then(() =>{
        alert("Item deleted successfully");
        document.getElementById('LanguageName')["value"] = "";
        document.getElementById('LanguageId')["value"] = "";
        this.RenderLanguagesEntries();
      }).catch((error) => {
        alert("Something went wrong" + error);
        document.getElementById('LanguageName')["value"] = "";
        document.getElementById('LanguageId')["value"] = "";
        this.RenderLanguagesEntries();
      });
    }
    else{
      return;
    }
}

Redeploy webpart in MS teams

Let’s now divert our attention to the different ways to make alterations to the code of webpart and redeploy the webpart to Teams. There are a few steps to be taken in order to get these updates in the already present webpart in MS Teams.

We are not altering any version here. The only thing that we are doing is updating the .ts file code.

These are some of the steps to be taken once the changes have been made to the code:
Create a GUID and alter “libraryId” property value in .yo-rc.json file to newly created GUID.

Generate a GUID

Alter the id property value to newly created GUID.

Change the id

Insert gulp build in command prompt. Ensure that you are present inside the project directory.
Then insert gulp bundle -ship in the command prompt.
Next, insert gulp package-solution -ship in the command prompt. After these steps, .sppkg file will get updated which is present in the solution folder.

Update sppkg file

Next, upload the package in the Apps for SharePoint list of App Catalog site. This will alert the user to replace the already present package.

Click on the “replace” option.

Click on the “Deploy” option.

Deploy

Now, select the solution and then click the “Sync to Teams” option. This will result in an error titled “Failed to sync solution to teams”. The reason being, webpart is already present in MS Teams with the same name.

Failed to sync

First of all, try to uninstall and delete it in the MS Team. Then, upload the updated solution again.

Go to Teams, and get rid of the tab that consists of webpart by selecting the “Remove” option.

Remove the tab

Next, click on “Remove” button.

Click on the Remove

Now if you try to sync the updated package again in the Apps for SharePoint list, you are likely to get the same error since we have removed the tab. It is, therefore, important to uninstall the package from Teams.

Go to Apps in Teams.

Navigate to Apps

Hit on the “delete” icon for the webpart.

Click on delete icon

Next, hit on the “Uninstall” button.

Click on uninstall

This will get rid of the webpart from the Apps tab.

Once it has been successfully deleted, again go to the Apps for SharePoint list and try to sync the package again. It will once again throw an error. The reason being, we have already removed the webpart from apps, but the webpart still exists in MS Teams store of our tenant.

Go to the store by clicking the “Store” option which is located at the bottom left corner in Teams.

Navigate to Store

Next, the webpart deployed will be found under the tenant name.

webpart deployed

Hit the “Delete” option available on the webpart.

Then click on the “Delete app” button.

Delete app

Once you have successfully deleted the webpart, go to App Catalog site of the tenant once again and try to sync the updated package.

Synced to Teams

The webpart will sync to Teams successfully.

To see the updated webpart in Teams, simply add the webpart by clicking the “+” sign in tabs in any of the teams created. Then opt the webpart that is updated.

Note:There is no need to manually deploy the webpart to Teams in case “Sync to Teams” alternative is fully developed.

Add a Tab

The updated changes will get reflected in Teams.

New Updates

Note: It is possible to get rid of the webpart from Teams by removing it from the store. There is no need to delete it from tabs and Apps.

The CRUD operation will function after the successful integration of webpart to Teams. Ensure that you have created the list in the site collection where the team is created. 

Final Words

These are some of the ways to get the most out of MS teams in a more efficient manner. In order to get the most of SPFx solutions, it is important to affiliate with a reputed Microsoft SharePoint development company and hire SharePoint developers who are talented enough to take your SharePoint development solutions to the pinnacle of success!

Sources For Images and Coding 

Leave a Reply

Your email address will not be published. Required fields are marked *

6 + eleven =