Often in SharePoint Projects we need to copy our JS / CSS / IMAGE files into a SharePoint Library. We can perform the same using Visual Studio Modules and package in a WSP.
What is a Module?
Modules are containers of files. We can use Modules to deploy files through a WSP file.
We are trying to deploy a JS file into an existing library.
Please follow the steps below to do the deployment.
Step 1: Create new project
Create a new project and name it as ModuleTest.
Choose the farm solution option.
Step 2: Add Module
Choose Solution Explorer > Add New Item > Module
Enter the name of the module as ModuleName for ease of understanding.
You will get the following module inside Solution Explorer.
Please note that there are 2 items inside a Module:
1. Elements.xml which contains information on files to be deployed
2. Sample.txt which is a dummy file to be deployed
Step 3: Add JS file
Right click on the Module & Add a new file. Name it as My.js.
You can also delete the sample.txt file. Now your Solution Explorer looks like below:
Step 4: Modify Elements.xml
We need to modify the Elements.xml now. Following is the current contents.
Please note that the Module contains an inner node for File. We can have multiple files deployed through this tag.
The Path of the File element shows the file path in solution. Url denotes the destination location after deployment.
In our case we need to change the destination to an existing library name. Please make the following changes.
We added the Url tag at the Module level to mention the library name. Specifying Type as Ghostable In Library ensures the file will be available for check in check out operations.
Step 5: Create Library
Create a library named Documents inside the SharePoint site.
Step 6: Deploy
Right click on the Solution Explorer and click the Deploy option.
Step 7: Test
Now we are ready to test the existence of file in the library. Open SharePoint & Navigate to the library.
You can see a new folder name is created.
Inside the folder you can find the file.
If the library name is invalid, you will get a deployment error.
How to copy to a new library?
If you wanted to copy to a new library, I would suggest add a library to the solution & ensure the library is getting deployed first. After that the module deployment should reach the correct library.
In this article we have explored how to deploy JS files using modules. The same knowledge can be used in deploying CSS/HTML/Image files.