Copy JS files to Library using Module

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.

Scenario

We are trying to deploy a JS file into an existing library.

Steps

Please follow the steps below to do the deployment.

Step 1: Create new project

Create a new project and name it as ModuleTest.

image

Choose the farm solution option.

image

Step 2: Add Module

Choose Solution Explorer > Add New Item > Module

image

Enter the name of the module as ModuleName for ease of understanding.

You will get the following module inside Solution Explorer.

image

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.

image

You can also delete the sample.txt file. Now your Solution Explorer looks like below:

image

Step 4: Modify Elements.xml

We need to modify the Elements.xml now. Following is the current contents.

image

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.

image

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.

image

Step 6: Deploy

Right click on the Solution Explorer and click the Deploy option.

image

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.

image

Inside the folder you can find the file.

image

image

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.

References

http://msdn.microsoft.com/en-us/library/ee231567.aspx

http://bit.ly/1f0TsWY

Summary

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.

Leave a Reply

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