Skip to main content

Create document library using SPFx solution

  •  In this article, we are going to learn how to create(provision) document library programmatically. We can create a list or document libraries programmatically with the SPFx solution. 

In my previous article, I wrote about the Provisioning of list with SPFx web part solution. SPFx provides flexibility to provision SharePoint assets with the solution. In this article, we are going to provision a document library.

Create a client-side web part solution

 Follow the steps for creating a client-side web part solution, run the commands using command prompt.

  • Create a directory md test-webpart
  • Move to the directory cd test-webpart 
  • Run yo @microsoft/sharepoint command to create a client-side web part.
  • Once you run the command mentioned above, you need to put some information related to the web part solution 
  •  What is your solution name? Give the name to your solution.
  •  Which baseline packages do you want to target for your component(s)?  On which environment you want to use this in our case we select "SharePoint Online Only(latest)".
  • Where do you want to place the files?  Select  Use the current folder
  • Do you want to allow the tenant admin the choice of being able to deploy the solution to all sites immediately without running any feature deployment or adding apps in sites?  No
  • Which type of client-side component to create? There are two types of client-side components, In this case, we are creating a web part, so select the web part.
  •  What is your Web part name? Give the name to the web part
  •  What is your Web part description? Describe the web part or you can skip this.
  •  Which framework would you like to use? Choose an option, as per your selection it will create a client-side component solution.
After this your solution will create time, it will take a few minutes.
  • Now the solution is ready.
  • Use code . the command to open the solution in Visual Code.

To provision SharePoint asset, we require folder structure and XML files:

  • Create a folder named "sharepoint" at the root level.
  • Create a folder named "assets" inside the "sharepoint" folder.
  • Now create new files elements.xml and schema.xml file under the "assets" folder.


  • Now copy the code mentioned below to elements.xml file.
  <?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
   <ListInstance 
           FeatureId="00bfea71-e717-4e80-aa17-d0c71b360101"
           Title="MyDocLibrary" 
           Description="document library"
           TemplateType="101"
           Url="Lists/MyDocLibrary">
   </ListInstance> 
</Elements>

  • Now copy the code into the schema.xml
List xmlns:ows="Microsoft SharePoint" Title="MyDocLibrary" EnableContentTypes="TRUE" FolderCreation="FALSE" Direction="$Resources:Direction;" Url="Lists/MyDocLibrary" BaseType="1" xmlns="http://schemas.microsoft.com/sharepoint/">
 <MetaData>
   <ContentTypes>
   </ContentTypes>
   <Fields></Fields>
   <Views>
     <View BaseViewID="1" Type="HTML" WebPartZoneID="Main" DisplayName="$Resources:core,objectiv_schema_mwsidcamlidC24;" DefaultView="TRUE" MobileView="TRUE" MobileDefaultView="TRUE" SetupPath="pages\viewpage.aspx" ImageUrl="/_layouts/images/generic.png" Url="AllItems.aspx">
       <XslLink Default="TRUE">main.xsl</XslLink>
       <JSLink>clienttemplates.js</JSLink>
       <RowLimit Paged="TRUE">30</RowLimit>
       <Toolbar Type="Standard" />
       <ViewFields>
         <FieldRef Name="LinkTitle"></FieldRef>
       </ViewFields>
       <Query>
         <OrderBy>
           <FieldRef Name="ID" />
         </OrderBy>
       </Query>
     </View>
   </Views>
   <Forms>
     <Form Type="DisplayForm" Url="Forms/DispForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
     <Form Type="EditForm" Url="Forms/EditForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
     <Form Type="NewForm" Url="Forms/NewForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
   </Forms>
 </MetaData>
</List>

Now we need to make changes in the package-solution.json file.

  • By default the package-solution.json file looks like the image shown below:


  • Now move to the package-solution.json file under the config folder and replace the code with the below code. We are making changes into the file to include features in the package-solution.json, which is responsible for provisioning(creating) of the library or any other SharePoint asset.  
    {
  "$schema": "https://developer.microsoft.com/json-schemas/spfx-build/package-solution.schema.json",
  "solution": {
    "name": "assetdeployment-client-side-solution",
    "id": "8cd172e3-a886-4705-9458-324a3d5fa9d8",
    "version": "1.0.0.0",
    "includeClientSideAssets": true,
    "features": [{
      "title": "assetdeployment-client-side-solution",
      "description": "assetdeployment-client-side-solution",
      "id": "a6c8b716-4613-4cc9-a39e-e2412d7d08d1",  
      "version": "1.0.0.0",
      "assets": {
        "elementManifests": [
          "elements.xml"
        ],
        "elementFiles":[
          "schema.xml"
        ]
      }
    }]
  },
  "paths": {
    "zippedPackage": "solution/assetdeployment.sppkg"
  }
}

  • Replace the id with the new guid. You can create new guid using any online tool.

Create the package and deploy it on sharepoint :

To create a package we need to run some npm commands, follow the steps mentioned below:
  • Run the command gulp bundle --ship 
  • Run the command gulp package-solution --ship this command create .sppkg file in the solution folder under the sharepoint folder. 
 

  • Now login into sharepoint tenant and redirect to your app catalog site.
  • Upload the .sppkg package on site.
  • When we upload the package on the app catalog site, it prompts a dialogue box.


  • Click on the deploy and then your package will be deployed to your Sharepoint tenant.
  • Go to site collection where you want to add this app.
  • Now move to the setting and then go on Add an app.
  • Go to apps for sharepoint and select app and click on it. The app will install in some time.
  • Once the app is installed, Refresh the page and newly create document library will show.
Thank You!!

Comments

Post a Comment

Popular posts from this blog

SPFx Interview Questions With Answers

In this article, we are going to discuss interview questions related to the SPFx (SharePoint Framework Development ). It will help you to prepare for the interview and in getting clarity on the core concepts of SPFx development. What is SPFx? SPFx, short for SharePoint Framework,  SPFx  allows us for client-side SharePoint development, we can easily connect with SharePoint data and it also supports for open source tools. What is the major difference between SPFx web part and the SharePoint app? Both the development model supports client-side development but the major difference between these two is that the SharePoint app(add-in) runs on the iframe. What you can build with SPFx? Using SPFx framework we can customize the SharePoint pages. We can build things mentioned below using the SharePoint Framework(SPFx). Web parts, Extensions, Adaptive Card, Library Component How many types of extensions we can create using SPFx? SPFx has three extension types: Application Customizers: U...

How to register an app in SharePoint

In this article, we are going to learn how to register an app/add-in in SharePoint online. If you have created a Sharepoint provider-hosted add-in or other SharePoint solution and to access the SharePoint site or list using the app/add-in you need to register the add-in on SharePoint and to grant permission. Register app in SharePoint 1. Login into SharePoint and Navigate to register an app page. Use the URL mentioned below:          [Sitecollection URL]/_layouts/15/appregnew.aspx 2.  Once you navigate to the Register app page, a form will open. You need to fill all the fields on the form. Client Id and Client Secret  generated when we click on the generate button available next to the fields.  Title : Name of the add-in which you want to give. App Domain : Where your domain is hosted, for the local environment you can place             "www.localhost.com". Do not include HTTP or HTTPS...

SharePoint Framework SPFx Node Version Compatibility

How to check the compatibility between the SPFx version and the Node.js version The SharePoint Framework (SPFx) is a client-side development platform used to develop web parts and extensions for SharePoint. The SharePoint Framework (SPFx) is compatible with the Node.js versions. Existing SPFx solutions may have different SPFx versions as compared to the current solutions. To make the SPFx solution run, Node.js is required, and in this blog, I am sharing steps to check the compatible node module for SPFx.  Find the SPFx version from the solution:  Open the project solution and navigate to the "package.json" file. In package.json file you will find  "@microsoft/sp-core-library" key and value of this particular key is the version of the SPFx.       Ex:    "@microsoft/sp-core-library": "1.14.0" Another way to find the SPFx version is by command, open the command prompt and run the command mentioned below: npm ls -g --depth=0 @microsoft/generator-s...