You can deploy a custom master page using visual studio.
Create your custom master page and custom css in SharePoint designer before proceeding this.
Open visual studio and select "Empty SharePoint Project" as project template.
Right click on the Project and choose add new item and select Module as new item, name it as "CustomMaster"
You will see a text file named "Sample.txt" has been created under this newly created module.
Now copy all html code from your master page and paste it in this text file and rename it with an extension ".master". This will be your master page
Now add again a new Module and name it as "CustomCss"
Again copy all your css code to the text file and rename it with an extension ".css". This will be your css file for the master page.
Open master page code under the Module and replace your css registration under <head> like this
"<SharePoint:CssRegistration name="<% $SPUrl:~sitecollection/Style Library/CustomCSS/yourfilename.css %>" After="corev4.css" runat="server"/>"
OPen the "Element.xml" under "CustomMaster" module and it should look like below
<Module Name="CustomMaster" Url="_catalogs/masterpage">
<File Path="CustomMaster\custommaster.master" Url="custommaster.master" Type="GhostableInLibrary" />
"Url="_catalogs/masterpage"" means this will be deployed to the master page library in SharePoint.
Open the "Element.xml" under "CustomCss" module and it should look like below
<Module Name="CustomCSS" Url="Style Library">
<File Path="CustomCSS\DAVCSS.css" Url="CustomCSS/yourfile.css" Type="GhostableInLibrary" />
"Url="Style Library"" means this will be deployed to the style library in SharePoint.
and "<File Path="CustomCSS\DAVCSS.css" Url="CustomCSS/yourfile.css" Type="GhostableInLibrary"/>" means this file will be deployed to "CustomCSS" folder under style library
Remeber we have already registered our css file in the master page under head tag with the above specified location
Now add the following code in the feature receiver class file that contains above modules. Right click on the feature and click "Add Event Receiver"
public override void featureactivated(spfeaturereceiverproperties properties)
{
spsite currsite = (spsite)properties.feature.parent;
spweb curweb = currsite.rootweb;
uri masteruri = new uri(curweb.url + "/_catalogs/masterpage/custommaster.master");
curweb.masterurl = masteruri.absolutepath;
curweb.custommasterurl = masteruri.absolutepath;
curweb.update();
}
This will apply the master page on activation of the feature.
public override void featuredeactivating(spfeaturereceiverproperties properties)
{
spsite currsite = (spsite)properties.feature.parent;
spweb curweb = currsite.rootweb;
uri masteruri = new uri(curweb.url + "/_catalogs/masterpage/v4.master");
curweb.masterurl = masteruri.absolutepath;
curweb.custommasterurl = masteruri.absolutepath;
curweb.update();
}
This will apply the default master page when feature will be deactivated.
Make sure your feature is scoped at site level before deploying.
Now build and deploy your solution
Intersting sharepoint Tricks.. Keep it up
ReplyDelete