Sharepoint 2007 Deploying Styles as Feature

No.of Views2969
Bookmarked0 times
Downloads 
Votes0
By  amalhashim   On  17 Aug 2010 10:08:17
Tag : SharePoint , Enterprise Content Management
In this article,i guide work with Sharepoint 2007 Deploying Styles as Feature.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this article,i guide work with Sharepoint 2007 Deploying Styles as Feature.

Steps 1

Assuming you have WSP Builder installed.
Open Visual Studio, From File –> New Project 

Image Loading

Step 2

Clicking OK will create a solution with the following folder structure 

Image Loading

Step 3

Now right click the Project “Test_Style” –> Add –> New Item 

Image Loading

Step 4

From the Add New Item dialog Select WSPBuilder –> Blank Feature 

Image Loading

Step 5

Clicking Add will bring up the following dialog 

Image Loading

Step 6

If you want the style to be applied on Site Collection level, then select the scope as Site, and Click OK will add feature/element files as shown below 

Image Loading

Step 7

Now add your style file under the folder TestStyle as shown below 

Image Loading

Open feature.xml file and copy paste the following replacing the existing ElementManifests tag

<ElementManifests><ElementManifest Location="elements.xml"/><ElementFile Location="MyStyle.css" />        
</ElementManifests>

Open elements.xml file and copy paste the following replacing existing contents

<?xml version="1.0" encoding="utf-8" ?><Elements xmlns="http://schemas.microsoft.com/sharepoint/"><Module Name="New Style" Url="Style Library" Path="" RootWebOnly="TRUE" ><File Url="MyStyle.css" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE"></File></Module></Elements>

Now build the WSP by Right Clicking the project from Solution Explorer 

Image Loading

Add the solution, and activate the feature.Now, if you notice the feature was suppose to overwrite the file if it exists. But, if the file exists its not overwriting, even if we provide “IgnoreIfAlreadyExists” flag in elements.xml file. As a solution for this, we need to handle the FeatureActivated event and manually checkout the file and overwrite it and checkin the file. To achieve this we need to build a class library, which should go to GAC.

Code

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.IO;
using System.Diagnostics;

namespace UpdateStyleReceiver
{class UpdateStyle : SPFeatureReceiver
    {
        SPWeb web = null;string directoryPath = null;// copy to locationstring url = null;public override void FeatureInstalled(SPFeatureReceiverProperties properties) { }public override void FeatureUninstalling(SPFeatureReceiverProperties properties) { }public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            WriteMessageToEventLog("Feature Activated");try{
                web = ((SPSite)properties.Feature.Parent).RootWeb;// set the directory path on the file system where the feature was activateddirectoryPath = properties.Definition.RootDirectory;// run with elevated permissions so we can overwrite the fileSPSecurity.RunWithElevatedPrivileges(OverwriteFile);
            }catch (Exception ex)
            {
                WriteMessageToEventLog(ex.ToString());
            }
        }public void OverwriteFile()
        {// create a new site object so that elevation works properlySPSite site = new SPSite(web.Site.ID);// copy to locationstring url = null;// get the url to the local filestring[] localFile = System.IO.Directory.GetFiles(directoryPath, "*.css", System.IO.SearchOption.TopDirectoryOnly);// define a fstream object so we can read the contents of the file into a byte arrayFileStream fstream = File.OpenRead(localFile[0]);byte[] contents = new byte[fstream.Length];
            fstream.Read(contents, 0, (int)fstream.Length);
            fstream.Close();// get a handle to the master page gallerySPList styleLibrary = site.OpenWeb().Lists["Style Library"];// get a handle to the folder we want to upload the file toSPFolder editingMenuFolder = styleLibrary.RootFolder;

            SPFile customQuickAccessFile = editingMenuFolder.Files["MyStyle.css"];// build the destination copy urlurl = site.Url + "/" + editingMenuFolder.Url + "/";// check out the file, replace it with the modified one, and check it back in, publish and approvecustomQuickAccessFile.CheckOut();
            customQuickAccessFile.CopyTo(url + "MyStyle_Original.css", true);

            WriteMessageToEventLog(editingMenuFolder.ServerRelativeUrl);
            WriteMessageToEventLog(editingMenuFolder.Url);// check in new filecustomQuickAccessFile = editingMenuFolder.Files.Add(url + "MyStyle.css", contents, true);
            customQuickAccessFile.CheckIn("File over-written by activiating the Published Page View Feature");
            customQuickAccessFile.Publish("File published by activating the Published Page View Feature");
            customQuickAccessFile.Approve("Approved by Published Page View Feature");

            site.Close();
        }public void DeleteFile()
        {// create a new site object so that elevation works properlySPSite site = new SPSite(web.Site.ID);// get a handle to the master page gallerySPList styleLibrary = site.OpenWeb().Lists["Style Library"];// get a handle to the folder we want to upload the file toSPFolder editingMenuFolder = styleLibrary.RootFolder;

            SPFile customQuickAccessFile = editingMenuFolder.Files["MyStyle_Original.css"];// build the destination copy urlurl = site.Url + "/" + editingMenuFolder.Url + "/";

            customQuickAccessFile.MoveTo(url + "MyStyle.css", true);

            customQuickAccessFile.Publish("File published by activating the Published Page View Feature");
            customQuickAccessFile.Approve("Approved by Published Page View Feature");

            site.Close();
        }public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            WriteMessageToEventLog("Deactiviating Feature");try{
                web = ((SPSite)properties.Feature.Parent).RootWeb;// run with elevated permissions so we can overwrite the fileSPSecurity.RunWithElevatedPrivileges(DeleteFile);
            }catch (Exception ex)
            {
                WriteMessageToEventLog(ex.ToString());
            }
        }private void WriteMessageToEventLog(string message)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                EventLog.WriteEntry("Copy Feature Receiver", message);
            });
        }
    }
}

 Modify the feature.xml as follows,

<?xml version="1.0" encoding="utf-8" ?><feature id="20402429-a875-482d-a4c8-e8449f48d04c" title="Update Style" description="Description for Update Style"version="12.0.0.0" hidden="FALSE" scope="Site" defaultresourcefile="core" receiverassembly="UpdateStyleReceiver, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7701372303d9a778"receiverclass="UpdateStyleReceiver.UpdateStyle" xmlns="http://schemas.microsoft.com/sharepoint/"><ElementManifests><ElementManifest Location="elements.xml"/><ElementFile Location="MyStyle.css" /></ElementManifests></feature>

 That's all. enjoy the sharepoint with style as features.

 
Sign Up to vote for this article
 
About Author
 
amalhashim
Occupation-Software Engineer
Company-Aditi Technologies
Member Type-Senior
Location-Not Provided
Joined date-07 Jun 2009
Home Page-http://lamahashim.blogspot.com
Blog Page-http://lamahashim.blogspot.com
I have done my masters in Computer Applications and graduation in Computer Science. I have great passion in working with Microsoft tool and technologies. I am also a Microsoft Most Valuable Professional. Personally my objective is to design/develop applications which eases user experience and performs better in long run.
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top