Click here to Skip to main content
Click here to Skip to main content
Go to top

SharePoint Quick Start FAQ Part III

, 12 Mar 2012
Rate this:
Please Sign up or sign in to vote.
SharePoint Quick Start FAQ Part III

Table of contents

This video shows how we can deploy a web part using SharePoint:

Introduction

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.

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>
</asp:Content>

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 Module and 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.

The type attribute has two values: Ghostable and 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 Ghostable.

A note: the path attribute has the page in the ‘MyPath’ folder.

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Path="MyPage" Url="SitePages">
<File Url="MyPage.aspx" Name="PageA.aspx" Type="Ghostable" />
</Module>
</Elements>

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"
Scope="Web"
Description="This features enables us to goto Custom Page"
Hidden="FALSE"
ImageUrl="menuprofile.gif"
ReceiverAssembly="ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=52bd1db23825a2e4"
ReceiverClass="ClassLibrary1.clsFeatureActivator" 
xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="ElementManifest.xml" />
</ElementManifests>
</Feature>

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");

From the SpSite object, we get the SpWeb object.

SPWeb ObjSpWeb = ObjQuickLaunch.OpenWeb();

From the 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.

QuickLauncNodes.AddAsFirst(objMenuItem);
ObjSpWeb.Update();

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.

<SharePoint>
<SafeMode ... >
<PageParserPaths>
<PageParserPath
  VirtualPath="/sites/MySite/SitePages/*"
  IncludeSubFolders="true"
  CompilationMode="Always"
  AllowServerSideScript="true" />
</PageParserPaths>
</SafeMode>
</SharePoint>

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 Microsoft.SharePoint.WebControls.

<SafeControls>
<SafeControl
Assembly="Microsoft.SharePoint"
Namespace="Microsoft.SharePoint.WebControls"
TypeName="*"
AllowRemoteDesigner="True" />
</SafeControls>

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 object.
  • SPFarm will have a collection of server objects, i.e., SPServer objects.
  • SPServer objects further have SPDatabaseServiceInstance objects.
  • SPDatabaseServiceInstance has a SPContentDatabase object instance.
  • From SPFarm, you can also browse to services on the farm level. SPFarm has a SPService collection.
  • SPService in turn allow us to browse through the IIS application, i.e., SPWebApplication.
  • SPWebApplication has a site collection which in turn has a site object which can be browsed using the SPSite object.

Now let’s understand the object hierarchy below SPSite. SPSite / 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.

// Get The local farm object
SPFarm oFarm = SPFarm.Local;

// Get all the services from the farm object
foreach (SPService objService in oFarm.Services)
{
    // Check if this is a WebService type of object
    if (objService is SPWebService)
    {
        // Cast it to a WebService object
        SPWebService oWebService = (SPWebService)objService ;

        // Get all Webapplications from the WebApplications collection
        foreach (SPWebApplication oWebApplication in oWebService.WebApplications)
        {
            // Loop through all the sites
            foreach (SPSite oSite in oWebApplication.Sites)
            {
                // Get reference of the Root object
                SPWeb oWebRoot = oSite.RootWeb;

                // Any kind of processing

                // Do not forget to dispose
                oWeb.Dispose();
                oSite.Dispose();
            }
        }
    }
}

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.

using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;

namespace NameSpaceCustomControl
{
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("<br/>");
        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=1.0.0.0,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=1.0.0.0,Culture=neutral,PublicKeyToken=4adae03f3c0d5b8e" 
   Namespace="NameSpaceCustomControl" TagPrefix="CustomSitePages" %>

<asp:Content runat="server" ContentPlaceHolderID="PlaceHolderMain">

<h3>Hi this is a Site Page</h3>
<CustomSitePages:CustomControl1 ID="cc1" runat="server" />

</asp:Content>

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.

Change:

<SafeMode MaxControls="200" CallStack="false" ...> 

to:

<SafeMode MaxControls="200" CallStack="true" ...> 

Step 2: Change the customErrors tag mode from On to Off.

Change:

<customErrors mode="On" /> 

to

<customErrors mode="Off" /> 

Step 3: Change ‘Debug’ from ‘false’ to ‘true’ in the Compilation attribute.

Change:

<compilation batch="false" debug="false"> 

to

<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 setText and ClearText, one which sets the label to a value and the other that clears the label. Both these methods setText and clearText are called by the event click of two buttons cmdSet and cmdClear, respectively.

<%@ Control Language="C#" %>
<script runat="server">
protected void SetText(object sender, EventArgs e) 
{
    lblDisplay.Text = "This is a user control";
}
protected void ClearText(object sender, EventArgs e) 
{
    lblDisplay.Text = "";
}
</script>
<asp:Button ID="cmdSet" runat="server" Text="Click Me" OnClick="SetText" />
<br/>
<asp:Button ID="cmdSetClear" runat="server" Text="Click to clear text" OnClick="ClearText" />
<br/>
<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 TagPrefix and TagName. Below is the code snippet which highlights this in bold.

<%@ Assembly Name="Microsoft.SharePoint,Version=12.0.0.0, 
    Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>

<%@ Page Language="C#" MasterPageFile="~/_layouts/application.master" 
   Inherits="Microsoft.SharePoint.WebControls.LayoutsPageBase" %>

<%@ Register TagPrefix="MyUserControlPrefix" TagName="MyUserControlName" 
   src="~/_controltemplates/MyUserControl/MyUserControl.ascx" %>

<%@ Import Namespace="Microsoft.SharePoint" %>

<asp:Content ID="Content3" ContentPlaceHolderId="PlaceHolderPageTitle" runat="server">
This page displays Custom Control in Action
</asp:Content>

<asp:Content ID="Content1" ContentPlaceHolderId="PlaceHolderMain" runat="server">
<MyUserControlPrefix:MyUserControlName ID="id2" runat="server" />
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderId="PlaceHolderPageTitleInTitleArea" 
  runat="server">
We never knew custom control is so easy to display.
</asp:Content>

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" 
    TagName="MyUserControlName" 
    src="~/_controltemplates/MyUserControl/MyUserControl.ascx" %>

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 WebPartZoneControl.

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.

Source code

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.

License

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

Share

About the Author

Shivprasad koirala
Architect http://www.questpond.com
India India

I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small
E-learning company in India. We are very much active in making training videos ,
writing books and corporate trainings. Do visit my site for 
.NET, C# , design pattern , WCF , Silverlight
, LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server  training 
and Interview questions and answers


Comments and Discussions

 
QuestionMy vote is 5 Pinmembermuhamd yusuf26-Dec-11 20:55 
GeneralLink is not working PinmemberAnjum.Rizwi18-Feb-09 17:38 
From Part II page, Link for Part III is not working.
 
Hope It will help.
 
Thank You,
Anjum Rizwi

GeneralThanks... PinmemberSuranyi6-Feb-09 2:02 
GeneralWaiting 4 next Pinmemberwaelali832-Feb-09 2:01 
GeneralOooo. pretty good... PinmemberRajesh Pillai17-Jan-09 22:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140905.1 | Last Updated 12 Mar 2012
Article Copyright 2009 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid