Share this page

Sunday, April 5, 2009

Custom setting section in Site Setting WSS 3.0 and MOSS 2007

I want share a solution to create our custom section setting in our portals in WSS 3.0 and MOSS 2007. As can see in the image 1 we are going to add a new link in the “Site Administration” section and when we selected this link we should navigate to custom page created by us.

[Image 1]
clip_image002

Before to start to talk about how we are going to create the solution I will like comment which is my development environment.

1) WSPBuilder – Add-In to Visual Studio to create WSP package.

2) Sharepont Solution Installer – Solution to install package WSP in Sharepoint 2007

3) Visual Studio 2008 Sp1

4) Windows Server 2008

5) Microsoft Office Sharepoint Server 2007

We are going to crate this solution using “Feature” to activate or deactivate our custom section setting in our portal.
The first thing is create a new project into our Visual Studio 2008 and to do this we will use a template named “WSPBuilder Project” that was installed when we installed the WSPBuilder Solution. We need to load a name and path where the project will be create in the file systems, in the image 2 we can see the windows opened by Visual Studio to do this.

[Image 2]
clip_image004

Once the project was created the next step is to create a feature using a template installed by WSPBuilder. To do this we are going to add a new item into the project and in the new window opened we will select WSPBuilder in the category section and Blank Feature in the project template section as we can see in the image 3.

[Image 3]
clip_image006

When we confirm the new item a new window will be opened and we will have to finish with the configuration. In this new window we can to set the value for our feature, name, description and scope. In our project has been configured the new feature as we can see in the image 4.

[Image 4]
clip_image008

Now we need to add 2 new folders in WSP project, one named GAC where we will put the assembly with the code behind of our custom page configuration and the other folder named LAYOUTS where we will put our custom setting page (ASPX). In the image 5 we can see the project configured.

[Image 5]
clip_image010

We need create one more project in our solution. This project will be use to add the code behind of our custom page (ASPX) and we are going to develop the events, method or everything that our page needed. In the image 6 we can see the solution with the 2 projects created.

[Image 6]
clip_image011

We are going to add the page that is load when the users select the link in the site setting of the portal. We can copy an existing page from layouts folder of our server and we must paste it in the folder named layouts in our WSP project. We must rename the page to be sure that when will be installed we won’t have problem. In this case I named “HelloWordSiteSettings.aspx”. In the image 7 we can see how will be the page that we are going to create.

[Image 7]
clip_image013

Open the page in the Visual Studio and in the declarative code (ASPX) we are going to do some modification needed. The first change is to modify the directive page named Assembly to register our assembly. The directive should be:
“<%@ Assembly Name="Siderys.Blog.Sharepoint.Configuration.CustomSiteSettings, Version=1.0.0.0, Culture=neutral, PublicKeyToken=711eed342842acee"%>”.
Whit this change we are setting the assembly that should be load when the page will be load.
Now we should change the @Page register to set the class with the code behind that should be invoked when the user select the button “OK” for example. The directive should be:
“<%@ Page Language="C#" Inherits="Siderys.Blog.Sharepoint.Configuration.CustomSiteSettings.HelloWordSiteSettingsPage" MasterPageFile="~/_layouts/application.master"%>”.
This page has other register to include other components of SharePoint or User Controls that are used to render the sections. In the section 1 we can see the complete code of our page.

[Section 1]

<%@ Assembly Name="Siderys.Blog.Sharepoint.Configuration.CustomSiteSettings, Version=1.0.0.0, Culture=neutral, PublicKeyToken=711eed342842acee"%> 
<%@ Page Language="C#" Inherits="Siderys.Blog.Sharepoint.Configuration.CustomSiteSettings.HelloWordSiteSettingsPage" MasterPageFile="~/_layouts/application.master"%> 
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register TagPrefix="wssuc" TagName="InputFormSection" src="~/_controltemplates/InputFormSection.ascx" %>
<%@ Register TagPrefix="wssuc" TagName="InputFormControl" src="~/_controltemplates/InputFormControl.ascx" %>
<%@ Register TagPrefix="wssuc" TagName="ButtonSection" src="~/_controltemplates/ButtonSection.ascx" %>
<%@ Register Tagprefix="wssawc" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<script language="C#" runat="server">
</script>
<asp:Content contentplaceholderid="PlaceHolderPageTitle" runat="server">
		<SharePoint:EncodedLiteral runat="server" text="Hola Mundo Page Settings" />
</asp:Content>
<asp:Content contentplaceholderid="PlaceHolderPageTitleInTitleArea" runat="server">
	<asp:literal text='Hola Mundo' runat="server"/>
</asp:Content>
<asp:Content contentplaceholderid="PlaceHolderPageDescription" runat="server"/>
<asp:Content contentplaceholderid="PlaceHolderAdditionalPageHead" runat="server">
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderMain" runat="server">
 <table class=propertysheet border="0" width="100%" cellspacing="0" cellpadding="0" id="diidProjectPageOverview">
 <wssuc:InputFormSection Title="Hola Mundo Settings" Description="Custom Configuration Section" runat="server">
	   <Template_InputFormControls>
			<wssuc:InputFormControl LabelText="Hello Word Custom Configuration Section" runat="server">
			    <Template_Control>
			        <wssawc:InputFormTextBox Title="CustomSettingValue" class="ms-input"  ID="TxtCustomSettingValue" Runat="server" />
				</Template_Control>
			</wssuc:InputFormControl>
	   </Template_InputFormControls>
 </wssuc:InputFormSection>
 <wssuc:ButtonSection runat="server">
	   <Template_Buttons>
		  <asp:Button UseSubmitBehavior="false" runat="server" class="ms-ButtonHeightWidth" OnClick="BtnOk_Click" Text="Ok" id="BtnOk" />
	   </Template_Buttons>
 </wssuc:ButtonSection>
 </table>
</asp:Content>


Our setting page could have different section, it depends our development. Each section is created using the tag InputFormSection and inside this tag we should add our controls. We can use the standard controls of Asp.Net to design the page or use the template control of Sharepoint. We are going to add the class where we will write the code behind of the ASPX page. The name of this class must be the same name that we registered in the directive @Page in the ASPX. This class must inherit of the base class WebAdminPageBase of Sharepoint because added additional action. In the section 2 we can see the complete code used to create the class.



[Section 2]



using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.ApplicationPages;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
namespace Siderys.Blog.Sharepoint.Configuration.CustomSiteSettings
{
    public class HelloWordSiteSettingsPage : WebAdminPageBase 
    {
        protected TextBox TxtCustomSettingValue;
        public HelloWordSiteSettingsPage()
        {
        }
        protected void BtnOk_Click(object sender, EventArgs e)
        {
            SPUtility.Redirect("settings.aspx", SPRedirectFlags.RelativeToLayoutsPage, this.Context);
        }
    }
}




To finish with the development is needed finish to configure our project, configure the feature XML, compile, generate the WSP and install all in our server.

If we want install our assembly with the code behind in the GAC of the server when the WSP will be install then we need put it into the folder “GAC” of our WSP project. We have 2 ways to do this, manually, copy and paste the assembly through file system or we can configure the project and when the compilation is ready the assembly will be copied to this folder. To configure the project we need open the properties pages, go to the Build Events section and copy this command into the section “Post-build event command line” xcopy /Y "$(TargetPath)" "$(SolutionDir)GAC". In the image 8 we can see the property page of our code behind project.



[Image 8]

clip_image002



Ready, now we are going to configure the feature of Sharepoint to install and create our custom section. We have 2 files, Feature.xml and Elements.xml. The first is not necessary to do anything because has the default value. In the second file, Elements.xml, we must add a new node in the XML. This node is CustomAction and will be the node that defines our custom section. In the section 3 we can see the code for the feature file.



[Section 3]



<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction
    Id="AAEA7FC4-080D-4551-B8A2-AA8A9D0B3340"
    Title="Hola Mundo Settings"
    Location="Microsoft.SharePoint.SiteSettings"
    GroupId="SiteAdministration"
    Sequence="107"
    RequireSiteAdministrator="TRUE"
    >
    <UrlAction Url="_layouts/HelloWordSiteSettings.aspx"/>
  </CustomAction>
</Elements>


For this example we will add the ID (guid), title (the name that will be show in the site settings), the Location (the section that we want show the link), in the following link http://msdn.microsoft.com/en-us/library/bb802730.aspx we can get additional configuration for this attribute. In the attribute GroupID must load the section where the link must be showed and in the attribute RequireSiteAdministrator if is necessary had permission as site administrator.


Inside this tag (CustomAction) we must add a second tag named “UrlAction” and must load the URL to access the custom page. We finished configuring our project, feature and now we are going to compile and create the WSP.


First, compile the project with the code behind and check that the assembly was copied to the folder GAC in the WSP project. Second, compile the WSP project to generate the Assembly.


Thirdly, generate the WSP file using the contextual menu of the project and selecting the option WSPBuilder and after “Build WSP” In the image 9 we can see the Output window of Visual Studio where can check if the WSP was created.



[Image 9]

clip_image002



We are ready to install the solution in our server. To do this we are going to use a bat file. In the section 4 we can see the code of this bat file named “Installsolution.bat”.



[Section 4]



@echo off



set URL=http://lanouse/


set STSADM="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\BIN\stsadm.exe"



set SOLUTION_NAME=WspAddSectionWebSettings.wsp

set FEATURE_NAME=AddSectionToSiteSettings



echo *

echo * Installing solution... '%SOLUTION_NAME%'


echo *


%STSADM% -o addsolution -filename %SOLUTION_NAME%



echo *

echo * Activating solution ... '%SOLUTION_NAME%' in  %URL%


echo *


%STSADM% -o deploysolution -name %SOLUTION_NAME% -url %URL%  -immediate -allowGacDeployment -force


%STSADM% -o execadmsvcjobs


iisreset



rem ***********************************************



echo *

echo * Activating feature... '%FEATURE_NAME1%' in '%URL%'


echo *


%STSADM% -o activatefeature -name %FEATURE_NAME% -url %URL% -force



echo *

echo * Install Completed.


echo *



When the installation is ready we can access to the portal and we can go to the site setting to view our link in the “Site Administration” section. If we select it we will navigate to our custom page setting.



clip_image001 
Download solution with a Sharepoint installer



Fabián Imaz

5 comments:

Andrew said...

WebAdminPageBase can be referenced from C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\CONFIG\BIN\Microsoft.SharePoint.ApplicationPages.dll

Staygreen Academy said...

Information was good,i like your post.
Looking forward for more on this topic
Lahir

Rakesh Parval said...

Wooh this is very informative article. Thank you so much..!
Rakesh

Kiran Joshya said...

It is incredible and informative knowledge. Impressive.
Kiran

Manideep Agarwal said...

This article is very good.informative style.. brilliant piece of advice.
Manideep