Tuesday 16 August 2011

How to copy files to App_GlobalResources folder of Web application using wsp



Many times we come across the situation where we need to put some resource files in our web application folders (port no. present in ~\inetpub\wwwroot\wss\VirtualDirectories\<portno.>\).
This cannot be done by making an entry in manifest.xml file (using manifest.xml we can only access the folders inside 12/14 hive). but it doesn't mean that this cannot be done.

We can make use of feature to achieve the target.

Let's get started with our feature development.

In this example i am replacing a resource file in App_GlobalResources folder of my web application.

We will start with development of feature receiver class.
  1. Open visual studio and create a project of type "class library".
  2. Give class a meaningful name ("CopyToApp_GlobalResourcesFeature.cs" in this case).
  3. Add the reference of Microsoft.Sharepoint.
  4. Extend the class to "SPFeatureReceiver" as shown in fig.
     5.  Now, override FeatureActivated method and write the code as shown below (CopyToApp_GlobalResources_Feature1 is my feature name).

    6.  Here is the code for GetWebAppURL function (highlighted above in red).
    7.  Now we are done with our feature receiver class.

Now, next step is to create a feature.xml file for this feature receiver class.
Add xml file to the solution, name it as feature.xml and make entries as shown in below fig.
Change "Public Key token", "Id" and "Scope" of the document accordingly (highlighted)



Next Step is to create manifest.xml file
Add xml file to the solution, name it as manifest.xml and make entries as shown in below fig.
Change "SolutionId" of the document(highlighted)



Next Step is to create the ddf file for creating the wsp.
open a notepad, enter the below text. Give meaningful wsp name and destinationDir should be name of the feature.
Test.resx is the resource file which has to be moved.

 
Now from the solution folder copy manifest.xml, feature.xml, CopyToApp_GlobalResources.dll and the resource file to the folder where we have created our ddf file.

now in command prompt enter "makecab \f <ddffilename.ddf>"
It'll create the wsp file. use the wsp for deployment.
Once feature "CopyToApp_GlobalResources_Feature1" is activated, the resource file would be replaced/copied.