Saturday, February 1, 2014

SharePoint 2013 - Developing a feature using visual studio 2012

Overview
In the previous post we saw the main components and properties of a feature. In this post we will take a closer look into features by creating a simple feature using Visual studio 2012 which deploys a custom site page.

Steps for creating a SP 2013 feature using VS 2012
Following are the steps for creating, deploying and activating a SharePoint 2013 feature using Visual Studio 2012.

1) Create a new empty project named SharePoint2013FeatureDemo.

2) Right click on the Features folder and add a new feature

3) A new feature named Feature1 will be added with supporting files as shown in the figure below.

4) In SharePoint 2013, there is a Feature manifest design view, where you can add the titlescope and items (element manifest, modules etc) in the feature.

5) Also, if you click on the manifest tab, you can see the feature manifest in xml format.

6) Now right click on the solution and add a new module named MyCustomModule.

7) By default, 2 helper files will be included when the module is added. First one is the Elements.xml (which includes the path and virtual url for Sample.txt) and second is the sample.txt which is just a simple text file.

8) Rename the Sample.txt to MyCustomSitePage.aspx and add the following mark-up inside it.

MyCustomSitePage.aspx Markup
<%@ Page MasterPageFile="~masterurl/default.master" meta:progid="SharePoint.WebPartPage.Document" %> 
<asp:Content ID="title" runat="server" ContentPlaceHolderID="PlaceHolderPageTitle"> 
   My Custom Site Page 
</asp:Content> 
<asp:Content ID="addhead" runat="server" ContentPlaceHolderID="PlaceHolderAdditionalPageHead"> 
</asp:Content> 
<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="PlaceHolderMain"> 
   <h3>Welcome to My Custom Site Page</h3> 
</asp:Content>

9) Rename the Feature title to FeatureDemo, change the scope to Site and add the module to the feature via the design view.

The same activities mentioned in the above step can also be done through the Feature manifest tab. Edit the feature manifest in xml view and add the Element Manifest inside Feature tag as follows.

Feature.xml Markup
<Feature xmlns="http://schemas.microsoft.com/sharepoint/" Title="FeaturesDemo" Id="75de0c0a-9f9f-4033-a708-c168bf14ba96" Scope="Site">
  <ElementManifests>
    <ElementManifest Location="MyCustomModule\Elements.xml" />
    <ElementFile Location="MyCustomModule\MyCustomSitePage.aspx" />
  </ElementManifests>
</Feature>

10) Make the changes as follows in the Module.
i) Add Url attribute in the Module element which specifies the location (SitePages) of the custom site page.
ii) Add Url attribute in the File element which specifies the file name (MyCustomSitePage.aspx) of the custom site page.

From the following figure you can easily figure out the url that will be formed. 
(http://sp2013home:10001/SitePages/MyCustomSitePage.aspx)

Elements.xml Markup
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="MyCustomModule" Url="SitePages">
    <File Path="MyCustomModule\MyCustomSitePage.aspx" Url="MyCustomSitePage.aspx" Type="Ghostable" />
  </Module>
</Elements>

11) Click on Build->Deploy to deploy the feature. By default activateOnDefault attribute will be true and the feature will automatically get activated. Click on the following url and you can see the custom master page that we deployed. (http://sp2013home:10001/SitePages/MyCustomSitePage.aspx).

What changes happened after deploying the feature?

1) 15 Hive - A new folder in 15 Hive will be created as ProjectName_FeatureFileName. In our case following is the folder created. You can change the naming convention in the feature properties window.
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\TEMPLATE\FEATURES\SharePoint2013FeaturesDemo_Feature1

2) Feature.xml - Inside the Feature folder there will be Feature.xml which is the feature manifest file and MyCustomModule folder.

3) Module - MyCustomModule folder will consist of 2 files.
    i) Elements.xml which is the element manifest file.

    ii) MyCustomSitePage.aspx which is the custom site page that we deployed.

4) GAC - Just like any other SharePoint server side component, the project dll is deployed to the GAC.
Note:- Unlike previous versions of SharePoint, the DLL is NOT deployed in "C:\Windows\assembly" folder. Instead it is deployed in the new GAC location with the following path.
C:\Windows\Microsoft.NET\assembly\GAC_MSIL\SharePoint2013FeatureDemo\v4.0_1.0.0.0__c5bbb59c11b25d00. 
(Read this article for more info on the new GAC location.)

5) Site Collection - Go to Site Settings page and click on Site Collection Features link.

6) Feature Installed and Activated - You can see that the feature named FeatureDemo has been installed and activated in the Site Collection.

So here we have discussed how to create and feature and deploy it through visual studio 2012. In the next post we will deploy, install and activate feature using the STSADM command and also discuss about solution packages.

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hi Mukundan,
    Yours articles are very good.But i have one doubt.How can i add my own page(.aspx) as a link under site administration in site settings page.Please help me in that

    ReplyDelete

Note: Only a member of this blog may post a comment.