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

Content editor web part missing in SharePoint/Modern SharePoint.

Media and content editor web part missing in SharePoint online. At Some Point, you need to add content editor web part into the modern site but the content editor web part did not appear on the page, to appear content editor web part on page you need to follow the below steps.
1 Log in as the SharePoint Global Administrator or SharePoint Administrator.
2.  Log in and navigate to your site collection ("https://xyz.sharepoint.com") and under the site settings activate the below features. Site settings – site features – SharePoint Server Publishing.Site Settings – Site Actions – Manage site features- SharePoint Server Publishing Infrastructure.             For this, you also need to enable Custom site scripts on your site collection(But In Modern SharePoint Its not recommended to enabling custom site scripting) For this go to the  SharePoint Admin center – Settings -  Classic settings page (option mentioned on page)- Scroll the page you found an option for the Custom Script.  





How to create list from template in sharepoint

In this article, we are going to learn how to create a list from list template or using(.stp) file or we can say create list from existing list. Once you save the list as a template, It appears into List template option under the web designer gallery on site setting page.

Steps to create list from list template:

1 Move to the site setting section where you have saved the list template.

2. When you click on the List templates, you will redirect to the List template gallery and from the list template gallery.
3. Now click on the File in ribbon and click on the upload document.



4. Browse the location where your list template is downloaded. I am uploading the template of an address list.

5.  After browsing the template, now you can give the name what you want to give to the list template and click on save.


6. Now the list template file appears into the list template gallery. Now click on the gear  icon and click on the Add an app.
7. On clicking add an app, you will redirect to site cont…

SharePoint Designer workflow error : server side activities have been updated. you need to restart sharepoint designer to use the updated version of activities.

While working with SharePoint designer at the time of creating custom forms and workflows, on editing the workflow or creating new one, error occurred and stated as :

"server side activities have been updated. you need to restart sharepoint designer to use the updated version of activities."

to resolve this error follow steps provided below.

Before moving on the steps mentioned below some time it's needed to clear the cache.

First clear the cache from locations mentioned below:
%APPDATA%\Microsoft\Web Server Extensions\Cache %APPDATA%\Microsoft\SharePoint Designer\ProxyAssemblyCache %USERPROFILE%\AppData\Local\Microsoft\WebsiteCache %APPDATA%\Microsoft\SharePoint Designer
Steps to resolve this problem:
To resolve this issue you need to update the SharePoint designer and to do so you have to follow this
two steps :

1. Check whether the Microsoft SharePoint 2013 service pack 1 is installed or not in your system.If not Please download it and install from URL mentioned below : 
F…