- 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.
- 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.
<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!!
You may refer: Provision list using SPFx solution
This comment has been removed by a blog administrator.
ReplyDelete