Monday, February 20, 2012

Deploy Master Page Programmatically


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


Saturday, February 11, 2012

Creating a Preview Pane in a List View

What is Preview Pane?

Using Preview Pane you can see a List Item details on the same page. When you will move your mouse cursor on a List Item it will show you the details of that Item.

Let see how to use Preview Pane.

1) Open the List on which you want to use Preview Pane. Select "Modify View" from the List tools




2) Under the "Style" select "Preview Pane" and press OK.




You will see that your view has been changed.

Now move your mouse cursor to any item and that item's detail will be shown on the right side in the preview pane.




Friday, February 10, 2012

Create SharePoint List from Excel

You can create a SharePoint List from excel spreadsheet.

Let see how:

1) Create a spreadsheet in excel and specify column headers and put some data.

2) Select the data and on the toolbar select format as Table and choose any style like below





3) This will format your data like a table. Now select "Export Table to SharePoint List" under
the "Export" menu on the toolbar.





specify the location of your SharePoint Site, Name and Description(optional) for the List that will
be created from this data.

4) After specifying above information a pop up message will appear confirming that your list has
been created. List will be created having a datasheet view as a default view.