Table of contents
In case you are new to SharePoint, please start with the below video Learn Sharepoint in 8 hours: -
This is my third series of SharePoint Quick Start FAQ. We will cover page templates, page instances, WSS model, understand safe mode processing, deploy custom controls, and understand WebParts. So let’s drink the SharePoint wine series by series, slowly, maintain the hangover, and enjoy this great product.
For the past few days, I have been writing and recording videos in Design Patterns, UML, FPA, Enterprise Blocks, and you can watch the videos at http://www.questpond.com.
You can download my 400 .NET FAQ eBook from http://www.questpond.com/SampleDotNetInterviewQuestionBook.zip.
For more resources on sharepoint tutorial videos visit http://www.learnsharepointtrainingtutorial.net.in
How can we provision page templates and page instances?
Before we move ahead with this question, let’s first define provisioning. Provisioning is a fancy name for making something available. Let’s first create a page template using Default.Master and see how we can provision this page on the website. Below is a code snippet where we can create a master page from Default.Master.
<%@ Page MasterPageFile="~masterurl/default.master"%>
<asp:Content runat="server" ContentPlaceHolderID="PlaceHolderMain">
<h3>Hi this is a Page made from Page template</h3>
In order to use the template we need to create an instance in the SharePoint runtime. Creating an instance of the template in the SharePoint runtime is termed as provisioning.
We need to use the
File tags to provision an instance of the template page. This is done in the ElementManifest.XML file. Below is a code snippet of the ElementManifest.XML file which has the
module tag with a
file tag specifying what the URL page name is. This is the same template which we had shown on the top. PageA.aspx is the instance name and MyPage.aspx is the physical page name of the template page.
type attribute has two values:
GhostableInLibrary. If you want to provision a resource inside a document library, then you need to specify
GhostableInLibrary. If you do not want to provision a resource inside a document library, then you need to specify the
Ghostable value. For the current exercise, we have given the value as
A note: the
path attribute has the page in the ‘MyPath’ folder.
<Module Path="MyPage" Url="SitePages">
<File Url="MyPage.aspx" Name="PageA.aspx" Type="Ghostable" />
As said previously, we also need to define the feature.xml file which has the receiver class and the element manifest pointing to the elementmanifest.xml file.
<Feature Id="701B7EA3-0816-4a5f-8FFE-AD15F0E5B562" Title="Provision"
Description="This features enables us to goto Custom Page"
ReceiverAssembly="ClassLibrary1, Version=22.214.171.124, Culture=neutral, PublicKeyToken=52bd1db23825a2e4"
<ElementManifest Location="ElementManifest.xml" />
In the receiver class, we will add menu items in the quick launch nodes. So in the activation event, we will add a link which will activate this feature and in the deactivation event, we will remove this feature.
In the activation event, we first need to get a reference to the
SpSite object using the URL.
SPSite ObjQuickLaunch = new SPSite("http://mum2815dz:2/sites/LearnSharePoint");
SpSite object, we get the
SPWeb ObjSpWeb = ObjQuickLaunch.OpenWeb();
SpWeb object, we get reference to all node collections of the quick launch menu.
SPNavigationNodeCollection QuickLauncNodes = ObjSpWeb.Navigation.QuickLaunch;
Now we create a menu of the page.
SPNavigationNode objMenuItem = new SPNavigationNode("Quest", "SitePages/PageA.aspx");
Add the menu to the quick launch node.
In the deactivation event, we will remove the link.
Now that we have done with creating the two XML files and the class which will handle the feature events, use the stsadm utility to register the feature. Once you have registered it, you should see the feature in the feature list display. We have named the feature ‘Provision’ so you can see the ‘Provision’ feature.
Once you activate the feature, you should see the ‘Quest’ link which points to the template instance page PageA.aspx. If you disable the feature, you will not be able to browse the page instance.
Why are customized pages parsed using no-compile mode
We had discussed previously the two types of pages: site pages and application pages. Site pages can be customized while application pages are standard pages like settings.aspx which are common across sites.
When a user customizes a site page, a customized version of the site page is saved in the content database. This provides a lot of flexibility but it has its own disadvantages. We can customize site pages using ‘SharePoint Designer’.
Now let’s try to understand how a customized site page which saves content is processed. Customized site pages are processed in six steps:
- Step 1: The user requests a customized page.
- Step 2: SharePoint HTTP handler, i.e.,
SPVirtualPathProvider, picks the request and shoots a query to the content database to fetch the page.
- Step 3: Site page is retrieved and sent to the SharePoint handler.
- Step 4: The site page data is given to the ASP.NET parser. Please note: this ASP.NET parser is not the one which IIS uses. This is made especially for SharePoint.
- Step 5: Parser parses and gives the output to the handler.
- Step 6: Handler finally gives the output back to the client.
Below is the reason why non-compiled pages are more efficient than compiled pages.
Once the compiled ASP page DLL is loaded, it gets unloaded only when the AppDomain gets recycled. It can be loaded and unloaded. In this scenario, memory management is more efficient.
What is safe mode processing and Safe controls?
Any customized page is parsed using safe mode processing. This parsing brings in security. Safe mode processing guarantees that there is no inline script in the customized page. In other words, safe mode processing disallows in-line scripts because a hacker can mount an attack using in-line script. If you try to run an in-line script on a customized page, you will get the error: ‘Code blocks are not allowed in this file’.
In case you still want to run an in-line script in customized pages, you need to specify
AllowServerSideScript=true in the
SafeMode tag section in the web.config file.
<SafeMode ... >
Safe controls help us define which controls the customized pages will have. Customized pages can only have controls which are defined in the web.config file in the
SafeControls tag. For instance, in the below code snippet, we have defined that customized pages can use controls from
Can you explain the WSS model?
In order to understand the WSS model, we need to first understand the different types of services used by SharePoint. SharePoint uses two major services in IIS, non-data service and SQL Server, i.e., database related services.
SharePoint was designed from the view point that it can be used in web farms. So let's first visualize web farms and then we will try to understand how SharePoint visualizes web farms. The figure below shows two aspects: a normal visualization of a web farm and the SharePoint view point. In our normal view (i.e., the left hand side of the image), we can think of farms having servers and servers having two services (i.e., IIS and DB service). When SharePoint visualizes the farm infrastructure, it’s a bit different.
SharePoint visualizes the farm infrastructure in terms of database services and non-database services. So it visualizes database services attached to servers and non-database services like IIS differently.
Note: We are not sure why there is a change in visualization.
So let’s visualize how the WSS object model of SharePoint is seen.
- The parent object in the SharePoint hierarchy is the
SPFarm will have a collection of server objects, i.e.,
SPServer objects further have
SPDatabaseServiceInstance has a
SPContentDatabase object instance.
SPFarm, you can also browse to services on the farm level.
SPFarm has a
SPService in turn allow us to browse through the IIS application, i.e.,
SPWebApplication has a site collection which in turn has a site object which can be browsed using the
Now let’s understand the object hierarchy below
Site belongs to site collection. In other words, one site collection can have many sites inside it. A site will have lists and a list will have fields.
Below is the code snippet which takes the
Farm object, browses through all its services, using the service object browses through all web applications, and then uses a web application to browse through all sites.
SPFarm oFarm = SPFarm.Local;
foreach (SPService objService in oFarm.Services)
if (objService is SPWebService)
SPWebService oWebService = (SPWebService)objService ;
foreach (SPWebApplication oWebApplication in oWebService.WebApplications)
foreach (SPSite oSite in oWebApplication.Sites)
SPWeb oWebRoot = oSite.RootWeb;
How can we use custom controls in SharePoint?
Step 1: Create a custom control by inheriting from the
WebControl class and the override the
RenderContents method with your implementation. In the below code snippet, we are just writing the site title and site URL to the browser.
public class CustomControl1 : WebControl
protected override void RenderContents(HtmlTextWriter output)
SPWeb site = SPContext.Current.Web;
output.Write("The Site title is " + site.Title);
output.Write("The URL of the site is " + site.Url);
Step 2: Compile the custom control class and generate the DLL. Once the compiled DLL is generated, register it in GAC.
Step 3: Refer to the custom control assembly in your ASPX page using the
Register attribute. Please remember to specify the public key token of the GAC.
<%@ Register Assembly="CustomControl,Version=126.96.36.199,Culture=neutral,PublicKeyToken=4adae03f3c0d5b8e"
Namespace="NameSpaceCustomControl" TagPrefix="CustomSitePages" %>
Specify the custom control with a proper unique ID.
<CustomSitePages:CustomControl1 ID="cc1" runat="server" />
Below is the complete code of the page:
<%@ Page Language="C#" MasterPageFile="~masterurl/default.master" meta:progid="SharePoint.WebPartPage.Document"%>
<%@ Register Assembly="CustomControl,Version=188.8.131.52,Culture=neutral,PublicKeyToken=4adae03f3c0d5b8e"
Namespace="NameSpaceCustomControl" TagPrefix="CustomSitePages" %>
Now if you run the page, you can see the custom control in action.
How can you view a detail error in SharePoint?
SharePoint does not give errors in a user-friendly manner. For instance, the below figure shows it has shown File not found, but the root cause is something different. Developers would like to get the exact function / method name in which the error has occurred.
There are four steps, in other words there are three changes we need to make to the config file and do an IIS reset. So let’s go through the four steps to get the actual cause of the above error. As said previously, we need to change the web.config file in three different places. You can get the web.config file from the C:\Inetpub\wwwroot\wss\VirtualDirectories\80 folder.
Step 1: Change the callstack value from false to true in the
SafeMode attribute as shown below.
<SafeMode MaxControls="200" CallStack="false" ...>
<SafeMode MaxControls="200" CallStack="true" ...>
Step 2: Change the
customErrors tag mode from On to Off.
<customErrors mode="On" />
<customErrors mode="Off" />
Step 3: Change ‘Debug’ from ‘false’ to ‘true’ in the
<compilation batch="false" debug="false">
<compilation batch="true" debug="true">
Step 4: Restart IIS.
You can now see a detailed stack error which shows which method exactly has the issue. For instance, in this scenario, the
FeatureDeactivation event has the error.
The error in the
FeatureDeactivation event happened because of the wrong URL specified for the
SPSite. If you saw the previous error, it showed as ‘File not found’, but actually, it was due to the wrong URL name.
How can we display an ASCX control in SharePoint pages?
Custom controls in ASP.NET provide a huge level of reusability in ASP.NET. They are normally simple controls with ASCX extensions which can be reused in different ASPX pages. We will not be getting into the details of what an ASCX control is. In case you are not familiar with the basics, please read any ASP.NET basic book and it’s not rocket science.
To load an ASCX control in the SharePoint environment is a simple five step process.
Step 1: We need to create an ASCX. Below is a simple ASCX file which has a label
lblDisplay. There are two functions
ClearText, one which sets the label to a value and the other that clears the label. Both these methods
clearText are called by the event click of two buttons
<%@ Control Language="C#" %>
protected void SetText(object sender, EventArgs e)
lblDisplay.Text = "This is a user control";
protected void ClearText(object sender, EventArgs e)
lblDisplay.Text = "";
<asp:Button ID="cmdSet" runat="server" Text="Click Me" OnClick="SetText" />
<asp:Button ID="cmdSetClear" runat="server" Text="Click to clear text" OnClick="ClearText" />
<asp:Label ID="lblDisplay" runat="server" Text="" />
Step 2: We need to refer this ASCX file in an ASPX page using the
Register attribute and refer to it using
TagName. Below is the code snippet which highlights this in bold.
<%@ Assembly Name="Microsoft.SharePoint,Version=184.108.40.206,
The below figure shows the overall view, interaction, and explanation of MyUserControl.ascx and PageUsingCustomControl.aspx.
Step 3: We need to copy the ASCX control in the C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES directory. For this sample, we have created a simple directory called MyUserControl and uploaded MyUserControl.ascx to it. If you remember, we had referred the ASCX file using the virtual path sign ‘~’ with the ‘MyUserControl’ folder.
<%@ Register TagPrefix="MyUserControlPrefix"
Step 4: We also need to paste the ASP.NET page PageUsingCustomControl.aspx in the layouts folder located at C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS.
Step 5: This is the easiest step we need to run the ASPX page and enjoy how the user control runs under the SharePoint environment. Below is how the user controls looks like. If you click the button ‘Click me’, the label is set with the text ‘This is a user control’. If you click on ‘Click to clear text’, it will just clear the values of the label.
Note: If you are using the behind code, you need to register the behind code into GAC and refer it in the ASCX. In this example, we have just used in-line code for simplicity.
What are WebParts and how do they interact with SharePoint?
WebParts help build reusable components which can be customized and personalized according to a business user. We can either make our own webpart or reuse existing ones from SharePoint.
The following WebParts are available with WSS:
- Data View Web Part: Displays data with rich design support through Microsoft SharePoint Designer.
- List View Web Part: Helps us display list content for any list in the SharePoint site.
- Image Web Part: Helps us display image files.
- Content Editor Web Part: Use this to display static HTML content using a WYSIWYG editor or to link to a text file.
- Members Web Part: Helps us display members of the site.
- Page Viewer Web Part: Displays a web page in an iFrame.
WebPart is nothing but a simple class which inherits from
System.Web.UI.WebControls.WebParts. In other words, you can say WebPart is kind of a WebControl which can be deployed in a
WebPart manager control is like a controller which maintains instances of WebParts. It adds WebParts to the WebPartZone when ASP.NET pages get initialized.
In my QuickStart fourth series, I will make a point to cover WebParts in more detail.
Feel free to download and use the source code.
Previous SharePoint QuickStart FAQ
- Quick Start FAQ Part 1: 11 basic FAQs, a must for every newcomer. It's the basic Quick Start FAQ tutorial which talks about what is SharePoint, WSS, MOSS, Site/Site collection, Virtual Path Provider, and then ends with explaining SitePages and Application pages. To view SharePoint Part 1, click here.
- Quick Start FAQ Part 2: This is the second part in the series which explains the readymade functionalities, custom pages, and deploying/activating/deactivating features. To view SharePoint Part 2, click here.
- Quick Start FAQ Part 4:-This is the fourth series of the SharePoint Quick Start FAQ. In this series, the theme is WebPart, WebPart, and WebPart. To view SharePoint Part 4, click here.
- Quick Start FAQ Part 5: This is the fifth series of the SharePoint Quick Start FAQ. In this series, the theme mainly concentrates on custom columns, content types, and document list library. I am sure once your read this article, your thinking of how SharePoint organizes document centralization will change, click here.
- Quick Start FAQ Part 6: In this article, we will concentrate mainly on SharePoint workflows. The SharePoint workflow sits on top of the Windows workflow and adds content centric workflow features. So we will first start with some basics on Windows workflow and then move towards SharePoint workflows, click here.
For more information on Sharepoint 2016 visit our Questpond website.