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.

SharePoint 2013 - Features Overview

Overview
Feature is a server side functional component which can be installed and activated at various scopes throughout a SharePoint instance. Additionally, a feature can be used to replace a standard control or functionality with your own custom functionality.
For example, the search box which appears in the upper-right corner of every standard SharePoint layout can be replaced with a custom search control.

Scope of a feature
Every feature has an activation scope that can assume one of the following values:
1) Farm - The feature will target the entire SharePoint farm.
2) WebApplication - The feature targets a single web application and all the contained Site Collections.
3) Site - The feature will target a single Site Collection and all of its websites.
4) Web - The feature targets a single website.

Components of feature
1) Feature Manifest (Feature.xml) - Each time you develop a feature, SharePoint at minimum creates an XML file, named Feature.xml and called the feature manifest, and stores it on every front-end web server of the farm in a sub-folder of the SharePoint 15_Root\TEMPLATE\FEATURES directory.
2) Feature Elements (Elements.xml) - These elements correspond to zero or more ElementManifest tags, which are still defined through XML files, and zero or more ElementFile tags, which declare files supporting the feature.

The feature manifest file structure
Following is the structure of a Feature manifest file.
<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
ActivateOnDefault = "TRUE" | "FALSE"
AlwaysForceInstall = "TRUE" | "FALSE"
AutoActivateInCentralAdmin = "TRUE" | "FALSE"
Creator = "Text"
DefaultResourceFile = "Text"
Description = "Text"
Hidden = "TRUE" | "FALSE"
Id = "Text"
ImageUrl = "Text"
ImageUrlAltText = "Text"
ReceiverAssembly = "Text"
ReceiverClass = "Text"
RequireResources = "TRUE" | "FALSE"
Scope = "Text"
SolutionId = "Text"
Title = "Text"
UIVersion = "Text"
Version = "Text" >
</Feature>

Properties of Feature Manifest
1) ActivateOnDefault - An optional Boolean attribute with a default value of True. It applies only to Farm-scoped or WebApplication-scoped features anddetermines whether the feature will be activated by default during installation. For WebApplication-scoped features, if this attribute is set to True, the feature will also be activated when a new web application is created.
2) AlwaysForceInstall - This is an optional Boolean attribute with a default value of False. When set to True, it forces the feature to be installed—even if it is already installed.
3) AutoActivateInCentralAdmin - This is an optional Boolean attribute with a default value of False. It defines whether the feature will be activated by default in the Administrative website hosting the SharePoint Central Administration. It does not apply to Farm-scoped features.
4) Creator - This is an optional description of the feature’s creator.
5) DefaultResourceFile - This is optional text that defines the name of a common resource file, usually shared with other features released by the same creator. By default, SharePoint will look for resources in a file in the path SharePoint15_Root\TEMPLATE\FEATURES\FeatureName\Resources, with a file name such as Resources.Culture.resx (the Culture value can be any of the standard culture names defined by the Internet Engineering Task Force (IETF), such as en-US, it-IT, fr-FR, and so on.
6) Description - This is optional text that describes the feature in the features’ management UI. You can define it using a resource string in the form $Resources:ResourceName. For example, if the feature description is a resource item with a key value of “FeatureDescription,” the corresponding value should be $Resources: FeatureDescription.
7) Hidden - This is an optional Boolean attribute with a default value of False. When set to True, the feature will be hidden from the UI and can be activated or deactivated only through the command line tools or by using the Object Model.
8) Id - This is a required attribute of type text, which must contain an ID (GUID) that uniquely identifies the feature.
9) ImageUrl - This is optional text that defines the site-relative URL of an image used to render the feature in the UI.
10) ImageUrlAltText - This is optional text that defines alternate text for the image representing the feature in the UI (see ImageUrl). You can define this using a resource string, just like the Description property.
11) ReceiverAssembly - This is optional text that defines the strong name of an assembly that SharePoint will search for in the Global Assembly Cache (GAC) and that provides a receiver class to handle the feature’s events.
12) ReceiverClass - This is optional text that defines the full class name of a receiver class to handle the feature’s events. SharePoint will search for the receiver class name in the ReceiverAssembly.
13) RequireResources - This is an optional Boolean attribute with a default value of False. It determines whether SharePoint requires that resources exist for the language of the current website or Site Collection to make the feature visible in the UI. This attribute does not affect the capability to activate and manage the feature from the command line or from the object model.
14) Scope - This is a required text attribute. It defines the scope within which the feature can be activated. The possible values are: Farm, WebApplication, Site, and Web. SolutionId This is optional text that defines the ID of the solution to which the features belongs.
15) Title - This is optional text that defines the title of the feature and that is visible in the features’ management UI. It is limited to a maximum length of 255 characters. You can define it using a resource string, as described in the Description property.
16) UIVersion - This is optional text that declares the UI version supported by the feature.
17) Version - This is optional text that defines the version of the feature. It can be made of up to four numbers, delimited by periods. For example, it might be 1.0.0.0, 1.0.0.1, and so on.

The feature elements file structure
<Feature xmlns="http://schemas.microsoft.com/sharepoint/" Title="My Sample Web Part" Description="Deploys a custom Web Part." Id="c46c270e-e722-4aa0-82ba-b66c8dd61f4e" Scope="Site" Version="1.0.0.0">
<ElementManifests>
<ElementManifest Location="SampleWebPart\Elements.xml" />
<ElementFile Location="SampleWebPart\SampleWebPart.webpart" />
</ElementManifests>
</Feature>

Feature Element Types
Following are the available features elements in SharePoint 2013:
1) ContentTypeBinding - Used to provision a content type on a list defined in a site template. Can be scoped to Site.
2) ContentType - Defines a content type, ready to be used in lists or libraries. Can be scoped to Site.
3) Control - Used to customize the configuration of an existing delegate control, or to declare a new delegate control to override SharePoint’s standard controls. Can be scoped to Farm, WebApplication, Site, and Web.
4) CustomAction - Defines an extension to the standard user interface. For example, you can use CustomAction to define a new button on a ribbon bar, a new menu item on a standard menu, or a new link on a site settings page. Can be scoped to Farm, WebApplication, Site, and Web.
5) CustomActionGroup - Groups custom actions. Can be scoped to Farm, WebApplication, Site, and Web.
6) DocumentConverter - Declares a document converter that can convert a document from a type X to a type Y. Requires some custom development, to implement the converter. Can be scoped to WebApplication.
7) FeatureSiteTemplateAssociation - Allows associating a feature to a specific site template definition for the purpose of provisioning the feature together with the site definition, when you create a new site with that definition. Can be scoped to Farm, WebApplication, and Site.
8) Field - Declares a Site Column definition. Can be scoped to Site.
9) HideCustomAction - Hides an existing custom action defined by another custom action or implemented by default in SharePoint. Can be scoped to Farm, WebApplication, Site, and Web.
10) ListInstance - Provisions an instance of a list definition, together with a specific configuration. Can be scoped to Site and Web.
11) ListTemplate - Defines a list template for the purpose of provisioning a custom lists’ definitions. Can be scoped to Web.
12) Module - Allows provisioning custom pages or files to a site. Module can also be used to deploy configured Web Parts, ListView Web Parts over existing or provisioned lists, NavBar links, custom Master Pages, and to configure properties of the target feature. Can be scoped to Site and Web.
13) PropertyBag - Assigns properties and metadata to items (File, Folder, ListItem, Web) through features. Can be scoped to Web.
14) Receivers - Defines a custom event receiver. Can be scoped to Web.
15) WebTemplate - Allows deploying a website template, even through a sandboxed solution so that it can create site instances based on that template. Can be scoped to Site.
16) Workflow - Deploys a workflow definition on a target site. Can be scoped to Site.
17) WorkflowActions - Defines custom workflow actions for SharePoint Designer 2013. Can be scoped to Farm.
18) WorkflowAssociation - Associates a workflow with its target. Can be scoped to Site, Web.

Features and Solutions Deployment
To deploy a feature, you need to copy the feature’s folder to the SharePoint15_Root\TEMPLATE\FEATURES path of every target server for the feature. When this is complete, you can use the PowerShell commands to install and later activate the feature.

Hope this article gave you an overview of the components of a feature. In the upcoming posts we will figure out the steps involved in the development and deployment of a SharePoint 2013 feature.