Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

SharePoint Quick Start FAQ Part 2

4.92/5 (54 votes)
3 Mar 2014CPOL15 min read 256.4K   1.1K  
In the previous session of SharePoint article we had discussed about the basics of SharePoint. In this session

Table of contents

In case you are new to SharePoint, please start with the below video Learn Sharepoint in 8 hours: -

Image 1

Introduction

In the previous part of this SharePoint series, we discussed the basics of SharePoint. In this session, we will:

  • Create site / site collection
  • Understand the readymade functional reusable modules
  • Learn how we can display a simple page and later apply master pages of SharePoint
  • Host inline code and behind code page in SharePoint
  • Understand the concept of features and understand step by step how to enable / disable a feature
  • How to display a feature in Admin

How can I create my first site in SharePoint?

When you want to create a site in SharePoint, you need to prepare a site collection and define the site inside the site collection. That’s a simple four step procedure and can be easily achieved by using the SharePoint central administration.

Step 1: Click on Start and you should find the SharePoint central administration menu.

Image 2

Step 2: Once you click on the central administration menu, you need to click on the ‘Application Management’ tab and then click ‘Create site collection’.

Image 3

Step 3: Once you click ‘Create site collection’, you should see a form which needs all the necessary details to create a site. All details are almost self-understandable. We have just stressed on the three points: the site name, the template, and the username in which SharePoint will run. All sites in SharePoint need to be inherited from some master, they can not stand on their own.

Image 4

Step 4: Once you have clicked ‘OK’, you can now run the URL with your SharePoint name and you should see something as shown below. As we have created from a blank site, currently we do not have anything. Below is how a blank site looks like.

Image 5

What is the Quick Launch menu?

The figure below shows the quick launch menu. We just want to make sure we know the terminology here and where it maps to. In future sections, we will be using this terminology for the left hand menu shown in the figure below.

Image 6

We have heard that we can customize SharePoint sites, how do we do that?

We will continue with the same example of our blank site which we discussed previously. On the left hand side corner, you will see a site actions menu. Click on the site action menu and click on the site settings menu link. Once you click on the link, you will be popped with different settings by which you can do customization. We will not discuss right now what every setting is, but in later sections, we will understand some important settings by which we can achieve customization.

To increase your confidence, you can play around with the ‘Look and feel’ settings. But this will be the landing page when we want to implement customization in SharePoint.

Image 7

We have heard it has readymade functional modules for collaboration

Oh, yes you have heard it loud, right, and clear. The best part about SharePoint is collaboration. Collaboration has four major entities: people, task, data, and communication.

Image 8

So below are some key points of enterprise:

  • We have people in our organization.
  • People are assigned tasks.
  • To complete a task we need to exchange data.
  • We also need to plan/monitor tasks.
  • To communicate data, we need a communication channel like email, Wiki, etc.

Image 9

SharePoint has lot of readymade functions which can help us accommodate all the requirements of the collaboration. To see those readymade functionalities, click on site actions and click on create. You will be popped with a list of reusable functionality pages which you can pick up and achieve collaboration.

Image 10

Now let’s map the ready made functionalities with the collaboration requirements.

Readymade functionalities in SharePoint

People Contacts You can get this in the communication section of the create page. It creates a page which can help us maintain contact information about people.
Task Tasks You can get this in the tracking section of the create page. It helps us track a task which your team member needs to complete.
Data Document library Its helps to share, edit, and version documents.
  Picture library Helps to share picture documents.
  Translation management Helps to create documents in multiple languages.
  Data connection library It helps to share files that contain information about external data.
Monitoring Project task You can get this in the tracking section of the create page. It gives a Gantt chart view of tasks.
  Issue tracking You can get this reusable functionality in the tracking section of create page. It helpss to manage / assign and prioritize issues from start to finish.
  Calendar Helps to create a calendar for deadlines and events.
Communication Wiki Helps to create an interconnected system like Wiki.
  Announcement Helps to share news and status through announcements.
  Discussion board Helps to create a discussion board like the newsgroup.

How to enable these reusable components in my site?

OK, now that we are familiar with the reusable components and how they map to the collaboration requirements, it is time add a reusable functionality in your website. We will add a link inside the documents menu called ‘SharePoint tutorial’. In this section, the team can upload tutorials for SharePoint in Word document format. It’s a simple three step process so let’s understand it step by step.

Image 11

Step 1: Click on site actions and click on Create link from the menu. You will be displayed with the reusable functionalities provided by SharePoint. Select ‘Document library’ from the libraries section.

Image 12

Step 2: Give the link a name like ‘SharePoint tutorial’ and select the document types to be unloaded in Word format. For this, you need to select the document template to be of type ‘Microsoft Word’.

Image 13

Step 3: Bravo! You can now see the SharePoint tutorial link and you can click on Upload to push in a Word document inside this library collection.

Image 14

You can see from the figure below how we have uploaded a Word document, i.e., “SharePoint tutorial”. You can right click and experiment with more functionalities like check out, versioning, workflow, send alerts when a document is modified, etc.

Image 15

Note: You can try adding other functionalities like Wiki, announcement board, picture library, and a lot more. So feel free to experiment and see how you can easily leverage the reusable functionalities to meet the collaboration requirements.

How can we display a simple custom page in SharePoint?

First, there is no concept of a simple page in SharePoint. There are two types of pages in SharePoint as we discussed in the previous article: Application page and Site page.

Application page is a generic page while site page are custom to a website. To just cool you off, let us display a simple Application page first.

Some points to be noted about Application pages:

  • They are pages which are shared across sites like ‘settings.aspx’, which will help us set generic properties across sites in a site collection.
  • The second important part is that we need to save application pages in the ‘C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS’ folder. If you browse to the folder, you will find SharePoint’s own application pages.

Image 16

OK, what we will do is, to build confidence, let’s make a simple page called SimplePage.aspx. We are not doing anything great in this, we will just write this simple sentence inside the page:

<b> Ohhhh I never Knew SharePoint is so easy </b>

Once you have saved the page, just browse to the IIS application and browse to the _layouts folder as shown in the figure below. If you open the page in a browser, you should see your display message.

Image 17

Note: For the next question, you need to understand the concept of master pages. If you have not, please read it again. A consistent look and feel is one of the most important factors in an enterprise portal and SharePoint achieves this using Master pages.

The above page does not look like a standard SharePoint page

In order to get the SharePoint look and feel, we need to inherit from a SharePoint master page. As a rule, you should always inherit from a SharePoint master page so that your sites have a consistent look and feel. So let’s modify our SimplePage.aspx. To get the SharePoint style, we need to inherit from the SharePoint master page Application.Master.

Image 18

We have now tailored the ‘simplepage.aspx’ source code as shown below. We need to do the following:

  • First refer to the assembly using the Assembly directive.
  • Refer to the master page file as Application.master.
  • Import the SharePoint namespace. If we had used the behind code, we would have imported this in the behind code itself.
  • There are three placeholders: one for title, one for center area, and one for the page title. We need to define the placeholders in the child page.
XML
<!— First refer the assembly using the Assembly directive ->
<%@ Assembly Name="Microsoft.SharePoint,Version=12.0.0.0, Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>
<!— Refer the master page from the _layouts directory ->
<%@ Page Language="C#" MasterPageFile="~/_layouts/application.master" 
    Inherits="Microsoft.SharePoint.WebControls.LayoutsPageBase" %>
<!—Import the sharepoint namespace ->
<%@ Import Namespace="Microsoft.SharePoint" %>
<!—This is the content holder for page title ->
<asp:Content ID="Content3" ContentPlaceHolderId="PlaceHolderPageTitle" runat="server">
Let's learn SharePoint....
</asp:Content>
<!- This is the place holder for data for main page area ->
<asp:Content ID="Content1" ContentPlaceHolderId="PlaceHolderMain" runat="server">
Oh its Damn Simple.....
</asp:Content>
<!- This is the placeholder for page title area ->
<asp:Content ID="Content2" ContentPlaceHolderId="PlaceHolderPageTitleInTitleArea" runat="server">
SharePoint is Simple..... 
</asp:Content>

Your SharePoint page now looks like a page:

Image 19

You can get the source of the simple inline ASPX file attached at the end of the article.

The above code is completely inline, how can we implement behind code in SharePoint?

A couple of points we need to take care of regarding implementing behind code in ASP.NET are:

  • The first and foremost requirement is that behind code should be registered in the GAC assembly. In other words, we need to code the behind code in a separate assembly / namespace and then compile the same in a DLL. Later we need to register the DLL in the GAC.
  • Second, we need to use the assembly directive to refer to the behind code.

Step 1: The first step is to make two solution files: the behind code which goes in a separate assembly ‘ClassLibrary1’ namespace, and the ASP.NET web project which has ‘SimplePageCodeBehind.aspx’. We also need to register this DLL in a GAC. So you need to generate a strong name given to the assembly and register that using ‘GACUTIL’ provided by the .NET Framework.

Image 20

Step 2: The behind code is in a separate assembly as we need to register it in the GAC. We have kept the behind code simple. We have created two label objects and set the value. One of the important points to be noted is that we have referenced the System.Web.UI namespace DLL and the Microsoft.SharePoint namespace DLL. The other point to be noted is that the class inherits from the LayoutsPageBase class which belongs to the Microsoft.SharePoint namespace.

C#
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;

// need to refer the UI objects of ASP.NET
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

// Need to refer the SharePoint DLL
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace ClassLibrary1
{

// Inherit the behind code from ‘LayoutsPageBase’ class of SharePoint
public partial class _Default : LayoutsPageBase
{
    protected Label lblSiteQuestion;
    protected Label lblSiteAnswer;

    protected override void OnLoad(EventArgs e)
    {
        lblSiteQuestion.Text = " How can we implement behind code in SharePoint ?";
        lblSiteAnswer.Text = " We need to register the behind DLL in GAC";
    }
}
}

We need to also register the above DLL in the GAC. So we need to generate a strong name and register it using GACUTIL.

Step 3: Now comes the UI part of ASP.NET, i.e., the ASPX page. Below is the code snippet which shows how the ASP.NET UI looks like.

The first thing to note is that the behind code is not referred as code behind but is referred using the GAC public token key. In order to refer it using the GAC key, we need to use the Assembly attribute.

We have also inherited from the master page file, i.e., Application.Master, so that we have a consistent look and feel.

XML
<!—Refer the sharepoint assembly ->
<%@ Assembly Name="Microsoft.SharePoint,Version=12.0.0.0, Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>

<!—Refer the behind code, Note that the behind code is coded in a different assembly and registered in the GAC -> 
<%@ Assembly Name="ClassLibrary1, Version=1.0.0.0, Culture=neutral,PublicKeyToken=af6d081bf267e17e" %>

<!— In order to maintain consistent look and feel we need to inherit from the Application.Master page ->
<%@ Page Language="C#" MasterPageFile="~/_layouts/application.master" 
   Inherits="ClassLibrary1._Default" EnableViewState="false" EnableViewStateMac="false" %>

Once we have referred to the Assembly and set the page attributes, it is time to fill the content in the placeholders defined in the master page Application.Master.

XML
<asp:Content ID="Main" contentplaceholderid="PlaceHolderMain" runat="server">
<table border="1" cellpadding="4" cellspacing="0" style="font-size:12">
<tr>
<td>Question</td>
<td><b><asp:Label ID="lblSiteQuestion" runat="server" /></b></td>
</tr>
<tr>
<td>Answer</td>
<td><asp:Label ID="lblSiteAnswer" runat="server" /></td>
</tr>
</table>
</asp:Content>

<asp:Content ID="PageTitle" runat="server"
contentplaceholderid="PlaceHolderPageTitle" >
SharePoint Behind code implementation
</asp:Content>

<asp:Content ID="PageTitleInTitleArea" runat="server"
contentplaceholderid="PlaceHolderPageTitleInTitleArea" >
When we want to implement behind code we need to register the same in GAC.
</asp:Content>

Note: Do not try to compile the project in VS.NET IDE. You can only compile the class assembly. The ASPX file you need to later paste to the ‘_layout’ directory, i.e., ‘C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS’.

Once you have copied the ASPX file and registered the behind code assembly in GAC, you can run and find out how it looks like.

Image 21

If you are thinking that behind code page implementation is a lot of pain in SharePoint, hang on. As we move ahead, you will see better ways of implementation. Second, we can not rule out the benefits as compared to the pain incurred in implementing behind code pages in ASP.NET.

What is the concept of features in SharePoint?

Whenever we think about SharePoint, think in terms of collaboration. One of the much needed features in collaboration is on-demand functionality / feature activation. Features make it easier to activate and deactivate functionality in SharePoint.

Some points which you should note about features:

Image 22

Image 23

  • All features needs to be copied to the “C:\Program Files\Common Files\Microsoft Shared\Web server extensions\12\Template\FEATURES\” directory. Microsoft SharePoint reads features from this directory. If you open the directory, you can find pre-installed features by SharePoint, as shown below.
  • Every feature will have two XML files: ‘Feature.xml’ and ‘ElementManifest.xml’.
  • SharePoint understands there is a feature by reading the feature XML file from the features folder.
  • All features are identified by a unique GUID number.
  • Features emit events which can be captured to write custom code. These events are captured in an assembly which needs to be registered in the GAC.
  • Summarizing what we discussed, we have a ‘feature.xml’ which points to an assembly which captures the feature events for custom code. There is also another XML file which defines what exactly this feature will do.

Image 24

Can you explain the broader steps for deploying / activating a feature?

In order to understand the feature concepts, let's deploy a simple application page, i.e., SimplePageCodeBehind.aspx as feature. So when the user activates this feature, he will be able to browse SimplePageCodeBehind.aspx.

Step 1: Let’s create a project ‘SharePointFeature’. You can find the source code attached with this article. Below is the project tree which has two XML files and a class file which will process the four events: FeatureInstalled, FeaturesUnInstalling, FeatureActivated, and FeatureDeactivating.

Image 25

Let’s understand these three files first.

XML
<Feature Id="48DEF2C4-33F9-4885-B0DE-6FE82E9FDCD8"
Title="Go to Custom Pages"
Description="This features enables us to goto Custom Page"
Scope="Web"
Hidden="FALSE"
ImageUrl="menuprofile.gif"
ReceiverAssembly="SharePointFeature, Version=1.0.0.0, Culture=neutral, PublicKeyToken=af83741e324f585c"
ReceiverClass="SharePointFeature.clsFeatureReceiver" 
xmlns="http://schemas.microsoft.com/sharepoint/" >
<ElementManifests>
<ElementManifest Location="ElementManifest.xml" />
</ElementManifests>
</Feature>

To generate a new GUID, click on Tools -> Create GUID and click ‘New GUID’. The Tools menu can be got from within the IDE. We have marked the GUID value which you will need to copy and paste in the feature.xml file.

Image 26

Other than feature description and title, there are two important things in the XML file. First, it points towards some other XML file, and second, it points to an assembly which captures events.

XML
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
Id="SiteActionsToolbar"
GroupId="SiteActions"
Location="Microsoft.SharePoint.StandardMenu"
Sequence="10"
Title="Display Custom Pages"
Description="This links helps to display Custom Page"
ImageUrl="_layouts/SharePoint2/menuprofile.gif">
<UrlAction Url="_layouts/SimplePageCodeBehind.aspx"/>
</CustomAction>
</Elements>

In other words, ElementManifest.xml specifies the location of the feature and which page it should redirect to.

We need to first refer to the SharePoint namespace as shown in the below code snippet.

C#
using System;
using System.Collections.Generic;
using System.Text;
// Refer the SharePoint namespace
using Microsoft.SharePoint;

We need to implement the SPFeatureReceiver class and implement all the events.

C#
namespace SharePointFeature
{
    // Inherit from the 'SPFeatureReceiver" class
    public class clsFeatureReceiver : SPFeatureReceiver
    {
        // Implement the four events of SPFeatureReceiver class
        public override void FeatureInstalled(SPFeatureReceiverProperties properties){}
        public override void FeatureUninstalling(SPFeatureReceiverProperties properties) { }
        // This event fires when the feature is activated
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            ....
            ....
            ....
            ....
        }
        // This event fires when the feature is deactivated
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties) 
        {
        ....
        ....
        ....
        ...
        }
    }
}

As a sample, in the FeatureActivated event, we have set the description and title of the website.

C#
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    // get the object of SharePoint Web
    SPWeb site = (SPWeb)properties.Feature.Parent;
    // Set the description ,properties , titile and update the SpWeb object
    site.Description = "Click on the SiteActions to See how the custom page display";
    site.Properties["OriginalTitle"] = "Display CustomPage";
    site.Properties.Update();
    site.Title = "This Site now has the custom page display";
    site.Update();
}

In FeatureDeactivating, we have reverted back to the title and description.

C#
public override void FeatureDeactivating(SPFeatureReceiverProperties properties) 
{
    // Get hold of the SharePoint web object and reset back the values
    SPWeb site = (SPWeb)properties.Feature.Parent;
    site.Description = "Custom Page display is disabled";
    site.Title = site.Properties["OriginalTitle"];
    site.Update();
}

Image 27

Image 28

>cd C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN

To install the feature, run the below command using STSADM. Please note that you need to specify the relative directory path and not the physical path of the Feature.xml file.

>stsadm -o installfeature -filename DisplayCustomPage\Feature.xml

To ensure that SharePoint registers this feature, run IISRESET on the machine.

Image 29

Now you can see your feature enabled in the site actions menu. If you click on the feature, i.e., ‘Display Custom Pages’, it will redirect you to SimplePageCodeBehind.aspx.

Image 30

The other point to be noted is that the events have fired and set the title and description as described in the code.

Image 31

Try to experiment and deactivate the feature and you will see the title and description changing.

Note: You can get the source for the feature in the Zip file provided with the article.

  • Feature.XML: This is the most important file because it helps SharePoint identify the feature. All features in SharePoint are identified by the GUID key.
  • ElementManifest.XML file: This file actually specifies how the implementation will look like. There are two important points to be noted for the ElementManiFest.XML file. The custom action tag specifies on which control the feature will be activated. The control we are targeting at this moment is SiteActionsToolBar. This tool bar is the one which you see on the right hand side corner of your SharePoint portal. There is also a URLaction property which specifies which URL it redirects to.
  • FeatureReceiver.cs: This class listens and implements custom actions of the feature.
  • Step 2: We need to register the compiled assembly in GAC and provide the token value in the Feature.XML file. You need to use GACUTIL to register. You can get the token by browsing to ‘c:\Windows\Assembly’ and then viewing the properties of the assembly.
  • Step 3: Copy the two XML files, Feature.xml and ElementManisfest.xml in the C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\DisplayCustomPage directory.
  • Step 4: Now we need to install the feature using STSADM.exe. So go to the DOS prompt, and go to the ‘C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN’ directory.
  • Step 5: Now click on Site Action -> Site Settings -> Site Features and Activate the feature.

I want that the feature should be only displayed to the admin

If you want only administrators to view the features, set RequireSiteAdministrator="True" as shown in the below ElementManifest.XML file.

XML
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
Id="SiteActionsToolbar"
GroupId="SiteActions"
RequireSiteAdministrator="True"
Location="Microsoft.SharePoint.StandardMenu"
Sequence="10"
Title="Display Custom Pages"
Description="This links helps to display Custom Page"
ImageUrl="_layouts/SharePoint2/menuprofile.gif">
<UrlAction Url="_layouts/SimplePageCodeBehind.aspx"/>
</CustomAction>
</Elements>

Source code

The source code has the following things:

  • Simple SharePoint behind code
  • Inline SharePoint behind code
  • SharePoint feature code

You can download the source code from here.

Prerequisites

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)